From 927b2274828b2eb9a381f2272365e9a9ad1be161 Mon Sep 17 00:00:00 2001 From: Andreas Scherren Date: Sat, 26 Oct 2024 18:28:06 +0200 Subject: [PATCH 1/4] 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 @@ public class TriListElement : TriElement private readonly ReorderableList _reorderableListGui; private readonly bool _alwaysExpanded; private readonly bool _showElementLabels; + private readonly bool _fixDefaultValue; private float _lastContentWidth; @@ -43,12 +45,30 @@ public TriListElement(TriProperty property) 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 @@ private void AddElementCallback(ReorderableList reorderableList, Object addedRef 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 @@ public class ListDrawerSettingsAttribute : Attribute 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 From 6b3849bd5b2080143d040b3adb427cccc6663c93 Mon Sep 17 00:00:00 2001 From: Andreas Scherren Date: Sat, 26 Oct 2024 18:29:34 +0200 Subject: [PATCH 2/4] Removed unneeded method --- Editor/Elements/TriListElement.cs | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/Editor/Elements/TriListElement.cs b/Editor/Elements/TriListElement.cs index a22a234..796a998 100644 --- a/Editor/Elements/TriListElement.cs +++ b/Editor/Elements/TriListElement.cs @@ -52,23 +52,6 @@ public TriListElement(TriProperty property) _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() { From 3c873d04ca94d4f1cd85de008879571a383935a8 Mon Sep 17 00:00:00 2001 From: Andreas Scherren Date: Sat, 26 Oct 2024 18:30:22 +0200 Subject: [PATCH 3/4] Removed unneeded import --- Editor/Elements/TriListElement.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Editor/Elements/TriListElement.cs b/Editor/Elements/TriListElement.cs index 796a998..17e7d60 100644 --- a/Editor/Elements/TriListElement.cs +++ b/Editor/Elements/TriListElement.cs @@ -1,7 +1,6 @@ using System; using System.Collections; using System.Linq; -using System.Reflection; using TriInspectorUnityInternalBridge; using TriInspector.Utilities; using UnityEditor; From 3583f83082a4dac69514c357cec2d2e03bd4ac7d Mon Sep 17 00:00:00 2001 From: Andreas Scherren Date: Sat, 26 Oct 2024 18:31:00 +0200 Subject: [PATCH 4/4] Making the assignment more uniform --- Editor/Elements/TriListElement.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Editor/Elements/TriListElement.cs b/Editor/Elements/TriListElement.cs index 17e7d60..8b7b9c5 100644 --- a/Editor/Elements/TriListElement.cs +++ b/Editor/Elements/TriListElement.cs @@ -31,6 +31,7 @@ public TriListElement(TriProperty property) _property = property; _alwaysExpanded = settings?.AlwaysExpanded ?? false; + _fixDefaultValue = settings?.FixDefaultValue ?? false; _showElementLabels = settings?.ShowElementLabels ?? false; _reorderableListGui = new ReorderableList(null, _property.ArrayElementType) { @@ -44,7 +45,6 @@ public TriListElement(TriProperty property) onRemoveCallback = RemoveElementCallback, onReorderCallbackWithDetails = ReorderCallback, }; - _fixDefaultValue = settings?.FixDefaultValue ?? false; if (!_reorderableListGui.displayAdd && !_reorderableListGui.displayRemove) {