From 927b2274828b2eb9a381f2272365e9a9ad1be161 Mon Sep 17 00:00:00 2001 From: Andreas Scherren Date: Sat, 26 Oct 2024 18:28:06 +0200 Subject: [PATCH] Added a fix default value --- Editor/Elements/TriListElement.cs | 26 ++++++++++++++++++++++++ Runtime/Attributes/ListDrawerSettings.cs | 1 + 2 files changed, 27 insertions(+) diff --git a/Editor/Elements/TriListElement.cs b/Editor/Elements/TriListElement.cs index c75fce3..a22a234 100644 --- a/Editor/Elements/TriListElement.cs +++ b/Editor/Elements/TriListElement.cs @@ -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,12 +45,30 @@ namespace TriInspector.Elements onRemoveCallback = RemoveElementCallback, onReorderCallbackWithDetails = ReorderCallback, }; + _fixDefaultValue = settings?.FixDefaultValue ?? false; if (!_reorderableListGui.displayAdd && !_reorderableListGui.displayRemove) { _reorderableListGui.footerHeight = 0f; } } + + 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() { @@ -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; } diff --git a/Runtime/Attributes/ListDrawerSettings.cs b/Runtime/Attributes/ListDrawerSettings.cs index fd0f368..7117798 100644 --- a/Runtime/Attributes/ListDrawerSettings.cs +++ b/Runtime/Attributes/ListDrawerSettings.cs @@ -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; } } \ No newline at end of file