From 51ab7830eb661f59c3eefc3f4230a0ec452cb33e Mon Sep 17 00:00:00 2001 From: Nigg Date: Mon, 6 Sep 2021 15:58:07 +0200 Subject: [PATCH] #133 fixed expandable attr causing null ref exceptions --- .../Scripts/Editor/NaughtyInspector.cs | 16 +++------------- .../PropertyDrawers/ExpandablePropertyDrawer.cs | 6 ++++-- .../Scripts/Editor/Utility/PropertyUtility.cs | 17 +++++++++++++++++ 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs index 647fdd55..e8cbb088 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs @@ -98,19 +98,9 @@ protected void GetSerializedProperties(ref List outSerializedPr { do { - NaughtyProperty naughtyProperty = new NaughtyProperty(); - naughtyProperty.property = serializedObject.FindProperty(iterator.name); - - naughtyProperty.readOnlyAttribute = PropertyUtility.GetAttribute(naughtyProperty.property); - naughtyProperty.enableIfAttribute = PropertyUtility.GetAttribute(naughtyProperty.property); - - naughtyProperty.showIfAttribute = PropertyUtility.GetAttribute(naughtyProperty.property); - naughtyProperty.validatorAttributes = PropertyUtility.GetAttributes(naughtyProperty.property); - - naughtyProperty.specialCaseDrawerAttribute = - PropertyUtility.GetAttribute(naughtyProperty.property); - - outSerializedProperties.Add(naughtyProperty); + outSerializedProperties.Add( + PropertyUtility.CreateNaughtyProperty( + serializedObject.FindProperty(iterator.name))); } while (iterator.NextVisible(false)); } diff --git a/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ExpandablePropertyDrawer.cs b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ExpandablePropertyDrawer.cs index 5a239eba..d2b13cd5 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ExpandablePropertyDrawer.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ExpandablePropertyDrawer.cs @@ -128,7 +128,7 @@ protected override void OnGUI_Internal(Rect rect, SerializedProperty property, G EditorGUI.EndProperty(); } - + private void DrawChildProperties(Rect rect, SerializedProperty property) { ScriptableObject scriptableObject = property.objectReferenceValue as ScriptableObject; @@ -180,7 +180,9 @@ private void DrawChildProperties(Rect rect, SerializedProperty property) height = childHeight }; - NaughtyEditorGUI.PropertyField(childRect, new NaughtyProperty(){property=childProperty}, true); + //TODO since the depth can go deeper we cant just use one field. - need better caching and mapping here! + //_naughtyProperty ??= PropertyUtility.CreateNaughtyProperty(childProperty); + NaughtyEditorGUI.PropertyField(childRect, PropertyUtility.CreateNaughtyProperty(childProperty), true); yOffset += childHeight; } diff --git a/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs b/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs index 465e5edb..1f57bfbd 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs @@ -26,6 +26,23 @@ public static T[] GetAttributes(SerializedProperty property) where T : class return (T[])fieldInfo.GetCustomAttributes(typeof(T), true); } + public static NaughtyProperty CreateNaughtyProperty(SerializedProperty property) + { + NaughtyProperty naughtyProperty = new NaughtyProperty(); + naughtyProperty.property = property; + + naughtyProperty.readOnlyAttribute = PropertyUtility.GetAttribute(naughtyProperty.property); + naughtyProperty.enableIfAttribute = PropertyUtility.GetAttribute(naughtyProperty.property); + + naughtyProperty.showIfAttribute = PropertyUtility.GetAttribute(naughtyProperty.property); + naughtyProperty.validatorAttributes = PropertyUtility.GetAttributes(naughtyProperty.property); + + naughtyProperty.specialCaseDrawerAttribute = + PropertyUtility.GetAttribute(naughtyProperty.property); + + return naughtyProperty; + } + public static GUIContent GetLabel(SerializedProperty property) { LabelAttribute labelAttribute = GetAttribute(property);