Fix adding items in Array/List editors if element type is reference (eg. class)

This commit is contained in:
2021-03-01 11:43:46 +01:00
parent dbec1389e8
commit b4e09a9d55
2 changed files with 24 additions and 13 deletions
@@ -43,10 +43,18 @@ namespace FlaxEditor.CustomEditors.Editors
// Copy old values
Array.Copy(array, 0, newValues, 0, sharedCount);
// Fill new entries with the last value
for (int i = oldSize; i < newSize; i++)
if (elementType.IsValueType)
{
Array.Copy(array, oldSize - 1, newValues, i, 1);
// Fill new entries with the last value
for (int i = oldSize; i < newSize; i++)
Array.Copy(array, oldSize - 1, newValues, i, 1);
}
else
{
// Initialize new entries with default values
var defaultValue = TypeUtils.GetDefaultValue(new ScriptType(elementType));
for (int i = oldSize; i < newSize; i++)
newValues.SetValue(defaultValue, i);
}
}
else if (newSize > 0)
@@ -54,9 +62,7 @@ namespace FlaxEditor.CustomEditors.Editors
// Initialize new entries with default values
var defaultValue = TypeUtils.GetDefaultValue(new ScriptType(elementType));
for (int i = 0; i < newSize; i++)
{
newValues.SetValue(defaultValue, i);
}
}
SetValue(newValues);
@@ -40,30 +40,35 @@ namespace FlaxEditor.CustomEditors.Editors
// Allocate new list
var listType = Values.Type;
var newValues = (IList)listType.CreateInstance();
var elementType = ElementType;
var sharedCount = Mathf.Min(oldSize, newSize);
if (list != null && sharedCount > 0)
{
// Copy old values
for (int i = 0; i < sharedCount; i++)
{
newValues.Add(list[i]);
}
// Fill new entries with the last value
for (int i = oldSize; i < newSize; i++)
if (elementType.IsValueType)
{
newValues.Add(list[oldSize - 1]);
// Fill new entries with the last value
for (int i = oldSize; i < newSize; i++)
newValues.Add(list[oldSize - 1]);
}
else
{
// Initialize new entries with default values
var defaultValue = Scripting.TypeUtils.GetDefaultValue(elementType);
for (int i = oldSize; i < newSize; i++)
newValues.Add(defaultValue);
}
}
else if (newSize > 0)
{
// Fill new entries with default value
var defaultValue = Scripting.TypeUtils.GetDefaultValue(ElementType);
var defaultValue = Scripting.TypeUtils.GetDefaultValue(elementType);
for (int i = oldSize; i < newSize; i++)
{
newValues.Add(defaultValue);
}
}
SetValue(newValues);