Added a fix default value

This commit is contained in:
Andreas Scherren 2024-10-26 18:28:06 +02:00
parent 67ee3d8ada
commit 927b227482
2 changed files with 27 additions and 0 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Linq;
using System.Reflection;
using TriInspectorUnityInternalBridge;
using TriInspector.Utilities;
using UnityEditor;
@ -19,6 +20,7 @@ namespace TriInspector.Elements
private readonly ReorderableList _reorderableListGui;
private readonly bool _alwaysExpanded;
private readonly bool _showElementLabels;
private readonly bool _fixDefaultValue;
private float _lastContentWidth;
@ -43,6 +45,7 @@ namespace TriInspector.Elements
onRemoveCallback = RemoveElementCallback,
onReorderCallbackWithDetails = ReorderCallback,
};
_fixDefaultValue = settings?.FixDefaultValue ?? false;
if (!_reorderableListGui.displayAdd && !_reorderableListGui.displayRemove)
{
@ -50,6 +53,23 @@ namespace TriInspector.Elements
}
}
private Type GetPropertyElementType(SerializedProperty property)
{
Type type = property.serializedObject.targetObject.GetType();
FieldInfo fieldInfo = type.GetField(property.propertyPath);
if (fieldInfo != null && fieldInfo.FieldType.IsArray)
{
return fieldInfo.FieldType.GetElementType(); // For arrays
}
else if (fieldInfo != null && fieldInfo.FieldType.IsGenericType)
{
return fieldInfo.FieldType.GetGenericArguments()[0]; // For lists
}
return null;
}
public override bool Update()
{
var dirty = false;
@ -134,6 +154,12 @@ namespace TriInspector.Elements
if (_property.TryGetSerializedProperty(out _))
{
ReorderableListProxy.DoAddButton(reorderableList, addedReferenceValue);
if (_fixDefaultValue)
{
reorderableList.serializedProperty.GetArrayElementAtIndex(reorderableList.index).boxedValue = Activator.CreateInstance(_property.ArrayElementType);
}
_property.NotifyValueChanged();
return;
}

View File

@ -12,5 +12,6 @@ namespace TriInspector
public bool HideRemoveButton { get; set; }
public bool AlwaysExpanded { get; set; }
public bool ShowElementLabels { get; set; }
public bool FixDefaultValue { get; set; } = false;
}
}