From 2b0bb4eea8d4b2d8674421a0f8d89d3a6bf2f2cb Mon Sep 17 00:00:00 2001 From: Nigg Date: Mon, 6 Sep 2021 15:11:17 +0200 Subject: [PATCH 01/14] #113 prepare test case with a big data class --- .../Samples/DemoScene/DemoScene.unity | 75 ++++- .../Scripts/Test/CurveRangeTest.cs | 38 ++- .../Scripts/Test/HugeMixPerformanceTest.cs | 304 ++++++++++++++++++ .../Test/HugeMixPerformanceTest.cs.meta | 11 + Packages/manifest.json | 1 + Packages/packages-lock.json | 9 + 6 files changed, 415 insertions(+), 23 deletions(-) create mode 100644 Assets/NaughtyAttributes/Scripts/Test/HugeMixPerformanceTest.cs create mode 100644 Assets/NaughtyAttributes/Scripts/Test/HugeMixPerformanceTest.cs.meta diff --git a/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity b/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity index d4be8e1a..6655bcca 100644 --- a/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity +++ b/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity @@ -38,7 +38,7 @@ RenderSettings: m_ReflectionIntensity: 1 m_CustomReflection: {fileID: 0} m_Sun: {fileID: 0} - m_IndirectSpecularColor: {r: 0.44657874, g: 0.49641258, b: 0.5748172, a: 1} + m_IndirectSpecularColor: {r: 0.44657898, g: 0.4964133, b: 0.5748178, a: 1} m_UseRadianceAmbientProbe: 0 --- !u!157 &3 LightmapSettings: @@ -201,23 +201,32 @@ MonoBehaviour: enable1: 0 enable2: 0 enum1: 0 + enum2: 0 enableIfAll: enableIfAny: enableIfEnum: + enableIfEnumFlag: + enableIfEnumFlagMulti: nest1: enable1: 1 enable2: 0 enum1: 0 + enum2: 0 enableIfAll: 1 enableIfAny: 2 enableIfEnum: 0 + enableIfEnumFlag: 0 + enableIfEnumFlagMulti: 0 nest2: enable1: 1 enable2: 1 enum1: 0 + enum2: 0 enableIfAll: {x: 0.25, y: 0.75} enableIfAny: {x: 0.25, y: 0.75} enableIfEnum: {x: 0.25, y: 0.75} + enableIfEnumFlag: {x: 0, y: 0} + enableIfEnumFlagMulti: {x: 0, y: 0} --- !u!114 &114650326 MonoBehaviour: m_ObjectHideFlags: 0 @@ -233,23 +242,32 @@ MonoBehaviour: disable1: 0 disable2: 0 enum1: 0 + enum2: 0 disableIfAll: disableIfAny: disableIfEnum: + disableIfEnumFlag: + disableIfEnumFlagMulti: nest1: disable1: 1 disable2: 0 enum1: 0 + enum2: 0 disableIfAll: 1 disableIfAny: 2 disableIfEnum: 3 + disableIfEnumFlag: 0 + disableIfEnumFlagMulti: 0 nest2: disable1: 1 disable2: 1 enum1: 0 + enum2: 0 enableIfAll: {x: 0.25, y: 0.75} enableIfAny: {x: 0.25, y: 0.75} enableIfEnum: {x: 0.25, y: 0.75} + disableIfEnumFlag: {x: 0, y: 0} + disableIfEnumFlagMulti: {x: 0, y: 0} --- !u!1 &155697335 GameObject: m_ObjectHideFlags: 0 @@ -538,8 +556,6 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 9c928ea15ae74a44089beb2e534c1a35, type: 3} m_Name: m_EditorClassIdentifier: - layerName: Default - layerIndex: 4 --- !u!1 &732714203 GameObject: m_ObjectHideFlags: 0 @@ -926,6 +942,7 @@ Transform: - {fileID: 1706612702} - {fileID: 369789277} - {fileID: 1463483878} + - {fileID: 1468344835} m_Father: {fileID: 0} m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} @@ -1355,6 +1372,40 @@ MonoBehaviour: int1: 0 nest2: int2: 0 + inheritedNest: + int1: 0 + nest2: + int2: 0 +--- !u!1 &1468344834 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1468344835} + m_Layer: 0 + m_Name: ===== Mixxed Attributes ===== + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &1468344835 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1468344834} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 1148579784} + m_RootOrder: 32 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1524906390 GameObject: m_ObjectHideFlags: 0 @@ -1402,23 +1453,32 @@ MonoBehaviour: show1: 0 show2: 0 enum1: 0 + enum2: 0 showIfAll: showIfAny: showIfEnum: + showIfEnumFlag: + showIfEnumFlagMulti: nest1: show1: 1 show2: 0 enum1: 0 + enum2: 0 showIfAll: 0 showIfAny: 0 showIfEnum: 0 + showIfEnumFlag: 0 + showIfEnumFlagMulti: 0 nest2: show1: 1 show2: 1 enum1: 0 + enum2: 0 showIfAll: {x: 0.25, y: 0.75} showIfAny: {x: 0.25, y: 0.75} showIfEnum: {x: 0.25, y: 0.75} + showIfEnumFlag: {x: 0, y: 0} + showIfEnumFlagMulti: {x: 0, y: 0} --- !u!114 &1524906393 MonoBehaviour: m_ObjectHideFlags: 0 @@ -1434,23 +1494,32 @@ MonoBehaviour: hide1: 0 hide2: 0 enum1: 0 + enum2: 0 hideIfAll: hideIfAny: hideIfEnum: + hideIfEnumFlag: + hideIfEnumFlagMulti: nest1: hide1: 1 hide2: 0 enum1: 0 + enum2: 0 hideIfAll: 0 hideIfAny: 0 hideIfEnum: 0 + hideIfEnumFlag: 0 + hideIfEnumFlagMulti: 0 nest2: hide1: 1 hide2: 1 enum1: 0 + enum2: 0 hideIfAll: {x: 0.25, y: 0.75} hideIfAny: {x: 0.25, y: 0.75} hideIfEnum: {x: 0.25, y: 0.75} + hideIfEnumFlag: {x: 0, y: 0} + hideIfEnumFlagMulti: {x: 0, y: 0} --- !u!1 &1552114399 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/NaughtyAttributes/Scripts/Test/CurveRangeTest.cs b/Assets/NaughtyAttributes/Scripts/Test/CurveRangeTest.cs index 697aaaa8..134abe6c 100644 --- a/Assets/NaughtyAttributes/Scripts/Test/CurveRangeTest.cs +++ b/Assets/NaughtyAttributes/Scripts/Test/CurveRangeTest.cs @@ -1,32 +1,30 @@ using UnityEngine; using NaughtyAttributes; -public class CurveRangeTest : MonoBehaviour +namespace NaughtyAttributes.Test { - [CurveRange(-1, -1, 1, 1, EColor.Red)] - public AnimationCurve curve; + public class CurveRangeTest : MonoBehaviour + { + [CurveRange(-1, -1, 1, 1, EColor.Red)] public AnimationCurve curve; - [CurveRange(EColor.Orange)] - public AnimationCurve curve1; + [CurveRange(EColor.Orange)] public AnimationCurve curve1; - [CurveRange(0, 0, 10, 10)] - public AnimationCurve curve2; + [CurveRange(0, 0, 10, 10)] public AnimationCurve curve2; - public CurveRangeNest1 nest1; + public CurveRangeNest1 nest1; - [System.Serializable] - public class CurveRangeNest1 - { - [CurveRange(0, 0, 1, 1, EColor.Green)] - public AnimationCurve curve; + [System.Serializable] + public class CurveRangeNest1 + { + [CurveRange(0, 0, 1, 1, EColor.Green)] public AnimationCurve curve; - public CurveRangeNest2 nest2; - } + public CurveRangeNest2 nest2; + } - [System.Serializable] - public class CurveRangeNest2 - { - [CurveRange(0, 0, 5, 5, EColor.Blue)] - public AnimationCurve curve; + [System.Serializable] + public class CurveRangeNest2 + { + [CurveRange(0, 0, 5, 5, EColor.Blue)] public AnimationCurve curve; + } } } diff --git a/Assets/NaughtyAttributes/Scripts/Test/HugeMixPerformanceTest.cs b/Assets/NaughtyAttributes/Scripts/Test/HugeMixPerformanceTest.cs new file mode 100644 index 00000000..9291271d --- /dev/null +++ b/Assets/NaughtyAttributes/Scripts/Test/HugeMixPerformanceTest.cs @@ -0,0 +1,304 @@ +using System.Collections; +using UnityEngine; + +namespace NaughtyAttributes.Test +{ + public class HugeMixPerformanceTest : MonoBehaviour + { + #region AnimatorParamTest + + public Animator animator0; + + [AnimatorParam("animator0")] + public int hash0; + + [AnimatorParam("animator0")] + public string name0; + + public AnimatorParamNest1 animatorParamNest1; + + [Button("Log 'hash0' and 'name0'")] + private void TestLog() + { + Debug.Log($"hash0 = {hash0}"); + Debug.Log($"name0 = {name0}"); + Debug.Log($"Animator.StringToHash(name0) = {Animator.StringToHash(name0)}"); + } + + #endregion + + #region BoxGroupTest + + [BoxGroup("Integers")] + public int int0; + [BoxGroup("Integers")] + public int int1; + + [BoxGroup("Floats")] + public float float0; + [BoxGroup("Floats")] + public float float1; + + [BoxGroup("Sliders")] + [MinMaxSlider(0, 1)] + public Vector2 slider0; + [BoxGroup("Sliders")] + [MinMaxSlider(0, 1)] + public Vector2 slider1; + + public string str0; + public string str1; + + [BoxGroup] + public Transform trans0; + [BoxGroup] + public Transform trans1; + + #endregion + + #region ButtonTest + + public int myInt; + + [Button(enabledMode: EButtonEnableMode.Always)] + private void IncrementMyInt() + { + myInt++; + } + + [Button("Decrement My Int", EButtonEnableMode.Editor)] + private void DecrementMyInt() + { + myInt--; + } + + [Button(enabledMode: EButtonEnableMode.Playmode)] + private void LogMyInt(string prefix = "MyInt = ") + { + Debug.Log(prefix + myInt); + } + + [Button("StartCoroutine")] + private IEnumerator IncrementMyIntCoroutine() + { + int seconds = 5; + for (int i = 0; i < seconds; i++) + { + myInt++; + yield return new WaitForSeconds(1.0f); + } + } + + #endregion + + #region CurveRangeTest + + [CurveRange(-1, -1, 1, 1, EColor.Red)] public AnimationCurve curve; + + [CurveRange(EColor.Orange)] public AnimationCurve curve1; + + [CurveRange(0, 0, 10, 10)] public AnimationCurve curve2; + + public CurveRangeNest1 curveRangeNest1; + + [System.Serializable] + public class CurveRangeNest1 + { + [CurveRange(0, 0, 1, 1, EColor.Green)] public AnimationCurve curve; + + public CurveRangeNest2 curveRangeNest2; + } + + [System.Serializable] + public class CurveRangeNest2 + { + [CurveRange(0, 0, 5, 5, EColor.Blue)] public AnimationCurve curve; + } + + #endregion + + #region DisableIfTest + + public bool disableIf1; + public bool disableIf2; + public DisableIfEnum disableIfEnum1; + [EnumFlags] public DisableIfEnumFlag disableIfEnum2; + + [DisableIf(EConditionOperator.And, "disableIf1", "disableIf2")] + [ReorderableList] + public int[] disableIfAll; + + [DisableIf(EConditionOperator.Or, "disableIf1", "disableIf2")] + [ReorderableList] + public int[] disableIfAny; + + [DisableIf("disableIfEnum1", DisableIfEnum.Case0)] + [ReorderableList] + public int[] disableIfEnum; + + [DisableIf("disableIfEnum2", DisableIfEnumFlag.Flag0)] + [ReorderableList] + public int[] disableIfEnumFlag; + + [DisableIf("disableIfEnum2", DisableIfEnumFlag.Flag0 | DisableIfEnumFlag.Flag1)] + [ReorderableList] + public int[] disableIfEnumFlagMulti; + + public DisableIfNest1 disableIfNest1; + + #endregion + + #region DropDownTest + + [Dropdown("dropDownIntValues")] + public int dropDownIntValue; + +#pragma warning disable 414 + private int[] dropDownIntValues = new int[] { 1, 2, 3 }; +#pragma warning restore 414 + + public DropdownNest1 dropdownNest1; + + #endregion + + #region EnableIfTest + + public bool enable1; + public bool enable2; + public EnableIfEnum enableIfEnum1; + [EnumFlags] public EnableIfEnumFlag enableIfEnum2; + + [EnableIf(EConditionOperator.And, "enable1", "enable2")] + [ReorderableList] + public int[] enableIfAll; + + [EnableIf(EConditionOperator.Or, "enable1", "enable2")] + [ReorderableList] + public int[] enableIfAny; + + [EnableIf("enableIfEnum1", EnableIfEnum.Case0)] + [ReorderableList] + public int[] enableIfEnum; + + [EnableIf("enableIfEnum2", EnableIfEnumFlag.Flag0)] + [ReorderableList] + public int[] enableIfEnumFlag; + + [EnableIf("enableIfEnum2", EnableIfEnumFlag.Flag0 | EnableIfEnumFlag.Flag1)] + [ReorderableList] + public int[] enableIfEnumFlagMulti; + + public EnableIfNest1 enableIfNest1; + + #endregion + + #region EnumFlagsTest + + [EnumFlags] + public TestEnum enumFlagsTest0; + + public EnumFlagsNest1 enumFlagsNest1; + + #endregion + + #region ExpandableTest + + [Expandable] + public ScriptableObject obj0; + + public ExpandableScriptableObjectNest1 expandableScriptableObjectNest1; + + #endregion + + #region FoldoutTest + + [Foldout("Integers")] + public int foldoutTestInt0; + [Foldout("Integers")] + public int foldoutTestInt1; + + [Foldout("Floats")] + public float foldoutTestFloat0; + [Foldout("Floats")] + public float foldoutTestFloat1; + + [Foldout("Sliders")] + [MinMaxSlider(0, 1)] + public Vector2 foldoutTestSlider0; + [Foldout("Sliders")] + [MinMaxSlider(0, 1)] + public Vector2 foldoutTestSlider1; + + public string foldoutTestStr0; + public string foldoutTestStr1; + + [Foldout("Transforms")] + public Transform foldoutTestTrans0; + [Foldout("Transforms")] + public Transform foldoutTestTrans1; + + #endregion + + #region HideIfTest + + public bool hide1; + public bool hide2; + public HideIfEnum hideIfEnum1; + [EnumFlags] public HideIfEnumFlag hideIfEnum2; + + [HideIf(EConditionOperator.And, "hide1", "hide2")] + [ReorderableList] + public int[] hideIfAll; + + [HideIf(EConditionOperator.Or, "hide1", "hide2")] + [ReorderableList] + public int[] hideIfAny; + + [HideIf("hideIfEnum1", HideIfEnum.Case0)] + [ReorderableList] + public int[] hideIfEnum; + + [HideIf("hideIfEnum2", HideIfEnumFlag.Flag0)] + [ReorderableList] + public int[] hideIfEnumFlag; + + [HideIf("hideIfEnum2", HideIfEnumFlag.Flag0 | HideIfEnumFlag.Flag1)] + [ReorderableList] + public int[] hideIfEnumFlagMulti; + + public HideIfNest1 hideIfNest1; + + #endregion + + #region ShowIfTest + + public bool showIf1; + public bool showIf2; + public ShowIfEnum showIfEnum1; + [EnumFlags] public ShowIfEnumFlag showIfEnum2; + + [ShowIf(EConditionOperator.And, "showIf1", "showIf2")] + [ReorderableList] + public int[] showIfAll; + + [ShowIf(EConditionOperator.Or, "showIf1", "showIf2")] + [ReorderableList] + public int[] showIfAny; + + [ShowIf("showIfEnum1", ShowIfEnum.Case0)] + [ReorderableList] + public int[] showIfEnum; + + [ShowIf("showIfEnum2", ShowIfEnumFlag.Flag0)] + [ReorderableList] + public int[] showIfEnumFlag; + + [ShowIf("showIfEnum2", ShowIfEnumFlag.Flag0 | ShowIfEnumFlag.Flag1)] + [ReorderableList] + public int[] showIfEnumFlagMulti; + + public ShowIfNest1 showIfNest1; + + #endregion + } +} diff --git a/Assets/NaughtyAttributes/Scripts/Test/HugeMixPerformanceTest.cs.meta b/Assets/NaughtyAttributes/Scripts/Test/HugeMixPerformanceTest.cs.meta new file mode 100644 index 00000000..8222c32a --- /dev/null +++ b/Assets/NaughtyAttributes/Scripts/Test/HugeMixPerformanceTest.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9d0844cc6e62fe54393612902562c4e2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/manifest.json b/Packages/manifest.json index 6c25be29..4aaf27e2 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -3,6 +3,7 @@ "com.unity.2d.sprite": "1.0.0", "com.unity.2d.tilemap": "1.0.0", "com.unity.collab-proxy": "1.3.9", + "com.unity.ide.rider": "3.0.7", "com.unity.ide.visualstudio": "2.0.7", "com.unity.test-framework": "1.1.24", "com.unity.textmeshpro": "3.0.4", diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index f28a4c72..d719c879 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -26,6 +26,15 @@ "dependencies": {}, "url": "https://packages.unity.com" }, + "com.unity.ide.rider": { + "version": "3.0.7", + "depth": 0, + "source": "registry", + "dependencies": { + "com.unity.ext.nunit": "1.0.6" + }, + "url": "https://packages.unity.com" + }, "com.unity.ide.visualstudio": { "version": "2.0.7", "depth": 0, From 1b8001c6a99e8a5c23ac25244ee22ac02c11a353 Mon Sep 17 00:00:00 2001 From: Nigg Date: Mon, 6 Sep 2021 15:24:10 +0200 Subject: [PATCH 02/14] #113 added perf boost with the help of caching the expensive reflection --- .../Scripts/Editor/NaughtyInspector.cs | 109 +++++++++++++----- .../ExpandablePropertyDrawer.cs | 2 +- .../Editor/Utility/NaughtyEditorGUI.cs | 43 ++++--- .../Scripts/Editor/Utility/PropertyUtility.cs | 29 +++-- 4 files changed, 122 insertions(+), 61 deletions(-) diff --git a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs index f1bcbb67..647fdd55 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs @@ -1,21 +1,47 @@ using System.Collections.Generic; +using System; using System.Linq; using System.Reflection; + using UnityEditor; using UnityEngine; namespace NaughtyAttributes.Editor { + public class NaughtyProperty + { + public SerializedProperty property; + public SpecialCaseDrawerAttribute specialCaseDrawerAttribute; + public ShowIfAttributeBase showIfAttribute; + + public EnableIfAttributeBase enableIfAttribute; + + public ReadOnlyAttribute readOnlyAttribute; + + public ValidatorAttribute[] validatorAttributes; + } + [CanEditMultipleObjects] [CustomEditor(typeof(UnityEngine.Object), true)] public class NaughtyInspector : UnityEditor.Editor { - private List _serializedProperties = new List(); + private List _serializedProperties = new List(); private IEnumerable _nonSerializedFields; private IEnumerable _nativeProperties; private IEnumerable _methods; + + private IEnumerable _nonGroupedSerializedProperty; + + private SerializedProperty m_ScriptProperty; + + private IEnumerable> _groupedSerialzedProperty; + + private IEnumerable> _foldoutGroupedSerializedProperty; + private Dictionary _foldouts = new Dictionary(); + private bool _anyNaughtyAttribute; + protected virtual void OnEnable() { _nonSerializedFields = ReflectionUtility.GetAllFields( @@ -26,19 +52,30 @@ protected virtual void OnEnable() _methods = ReflectionUtility.GetAllMethods( target, m => m.GetCustomAttributes(typeof(ButtonAttribute), true).Length > 0); + + GetSerializedProperties(ref _serializedProperties); + + _anyNaughtyAttribute = _serializedProperties.Any(p => PropertyUtility.GetAttribute(p.property) != null); + + _nonGroupedSerializedProperty = GetNonGroupedProperties(_serializedProperties); + NaughtyProperty[] mScripts = _serializedProperties.Where(p => p.property.name.Equals("m_Script")).ToArray(); + + m_ScriptProperty = mScripts.Length > 0 ? mScripts[0].property : null; + + _groupedSerialzedProperty = GetGroupedProperties(_serializedProperties); + + _foldoutGroupedSerializedProperty = GetFoldoutProperties(_serializedProperties); } protected virtual void OnDisable() { ReorderableListPropertyDrawer.Instance.ClearCache(); } + public override void OnInspectorGUI() { - GetSerializedProperties(ref _serializedProperties); - - bool anyNaughtyAttribute = _serializedProperties.Any(p => PropertyUtility.GetAttribute(p) != null); - if (!anyNaughtyAttribute) + if (!_anyNaughtyAttribute) { DrawDefaultInspector(); } @@ -51,8 +88,8 @@ public override void OnInspectorGUI() DrawNativeProperties(); DrawButtons(); } - - protected void GetSerializedProperties(ref List outSerializedProperties) + + protected void GetSerializedProperties(ref List outSerializedProperties) { outSerializedProperties.Clear(); using (var iterator = serializedObject.GetIterator()) @@ -61,7 +98,19 @@ protected void GetSerializedProperties(ref List outSerialize { do { - outSerializedProperties.Add(serializedObject.FindProperty(iterator.name)); + 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); } while (iterator.NextVisible(false)); } @@ -72,26 +121,24 @@ protected void DrawSerializedProperties() { serializedObject.Update(); - // Draw non-grouped serialized properties - foreach (var property in GetNonGroupedProperties(_serializedProperties)) + if (m_ScriptProperty != null) { - if (property.name.Equals("m_Script", System.StringComparison.Ordinal)) + using (new EditorGUI.DisabledScope(disabled: true)) { - using (new EditorGUI.DisabledScope(disabled: true)) - { - EditorGUILayout.PropertyField(property); - } - } - else - { - NaughtyEditorGUI.PropertyField_Layout(property, includeChildren: true); + EditorGUILayout.PropertyField(m_ScriptProperty); } } + // Draw non-grouped serialized properties + foreach (var property in _nonGroupedSerializedProperty) + { + NaughtyEditorGUI.PropertyField_Layout(property, includeChildren: true); + } + // Draw grouped serialized properties - foreach (var group in GetGroupedProperties(_serializedProperties)) + foreach (var group in _groupedSerialzedProperty) { - IEnumerable visibleProperties = group.Where(p => PropertyUtility.IsVisible(p)); + IEnumerable visibleProperties = group.Where(p => PropertyUtility.IsVisible(p.property)); if (!visibleProperties.Any()) { continue; @@ -107,9 +154,9 @@ protected void DrawSerializedProperties() } // Draw foldout serialized properties - foreach (var group in GetFoldoutProperties(_serializedProperties)) + foreach (var group in _foldoutGroupedSerializedProperty) { - IEnumerable visibleProperties = group.Where(p => PropertyUtility.IsVisible(p)); + IEnumerable visibleProperties = group.Where(p => PropertyUtility.IsVisible(p.property)); if (!visibleProperties.Any()) { continue; @@ -190,23 +237,23 @@ protected void DrawButtons(bool drawHeader = false) } } - private static IEnumerable GetNonGroupedProperties(IEnumerable properties) + private static IEnumerable GetNonGroupedProperties(IEnumerable properties) { - return properties.Where(p => PropertyUtility.GetAttribute(p) == null); + return properties.Where(p => PropertyUtility.GetAttribute(p.property) == null && !p.property.name.Equals("m_Script")); } - private static IEnumerable> GetGroupedProperties(IEnumerable properties) + private static IEnumerable> GetGroupedProperties(IEnumerable properties) { return properties - .Where(p => PropertyUtility.GetAttribute(p) != null) - .GroupBy(p => PropertyUtility.GetAttribute(p).Name); + .Where(p => PropertyUtility.GetAttribute(p.property) != null) + .GroupBy(p => PropertyUtility.GetAttribute(p.property).Name); } - private static IEnumerable> GetFoldoutProperties(IEnumerable properties) + private static IEnumerable> GetFoldoutProperties(IEnumerable properties) { return properties - .Where(p => PropertyUtility.GetAttribute(p) != null) - .GroupBy(p => PropertyUtility.GetAttribute(p).Name); + .Where(p => PropertyUtility.GetAttribute(p.property) != null) + .GroupBy(p => PropertyUtility.GetAttribute(p.property).Name); } private static GUIStyle GetHeaderGUIStyle() diff --git a/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ExpandablePropertyDrawer.cs b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ExpandablePropertyDrawer.cs index 2c485b7f..5a239eba 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ExpandablePropertyDrawer.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ExpandablePropertyDrawer.cs @@ -180,7 +180,7 @@ private void DrawChildProperties(Rect rect, SerializedProperty property) height = childHeight }; - NaughtyEditorGUI.PropertyField(childRect, childProperty, true); + NaughtyEditorGUI.PropertyField(childRect, new NaughtyProperty(){property=childProperty}, true); yOffset += childHeight; } diff --git a/Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs b/Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs index 08d77860..b9588c6e 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Linq; using System.Reflection; + using UnityEditor; using UnityEditor.Experimental.SceneManagement; using UnityEditor.SceneManagement; @@ -16,65 +17,63 @@ public static class NaughtyEditorGUI private static GUIStyle _buttonStyle = new GUIStyle(GUI.skin.button) { richText = true }; - private delegate void PropertyFieldFunction(Rect rect, SerializedProperty property, GUIContent label, bool includeChildren); + private delegate void PropertyFieldFunction(Rect rect, NaughtyProperty property, GUIContent label, bool includeChildren); - public static void PropertyField(Rect rect, SerializedProperty property, bool includeChildren) + public static void PropertyField(Rect rect, NaughtyProperty property, bool includeChildren) { PropertyField_Implementation(rect, property, includeChildren, DrawPropertyField); } - public static void PropertyField_Layout(SerializedProperty property, bool includeChildren) + public static void PropertyField_Layout(NaughtyProperty property, bool includeChildren) { Rect dummyRect = new Rect(); PropertyField_Implementation(dummyRect, property, includeChildren, DrawPropertyField_Layout); } - private static void DrawPropertyField(Rect rect, SerializedProperty property, GUIContent label, bool includeChildren) + private static void DrawPropertyField(Rect rect, NaughtyProperty property, GUIContent label, bool includeChildren) { - EditorGUI.PropertyField(rect, property, label, includeChildren); + EditorGUI.PropertyField(rect, property.property, label, includeChildren); } - private static void DrawPropertyField_Layout(Rect rect, SerializedProperty property, GUIContent label, bool includeChildren) + private static void DrawPropertyField_Layout(Rect rect, NaughtyProperty property, GUIContent label, bool includeChildren) { - EditorGUILayout.PropertyField(property, label, includeChildren); + EditorGUILayout.PropertyField(property.property, label, includeChildren); } - private static void PropertyField_Implementation(Rect rect, SerializedProperty property, bool includeChildren, PropertyFieldFunction propertyFieldFunction) + private static void PropertyField_Implementation(Rect rect, NaughtyProperty property, bool includeChildren, PropertyFieldFunction propertyFieldFunction) { - SpecialCaseDrawerAttribute specialCaseAttribute = PropertyUtility.GetAttribute(property); - if (specialCaseAttribute != null) + if (property.specialCaseDrawerAttribute != null) { - specialCaseAttribute.GetDrawer().OnGUI(rect, property); + property.specialCaseDrawerAttribute.GetDrawer().OnGUI(rect, property.property); } else { // Check if visible - bool visible = PropertyUtility.IsVisible(property); + bool visible = PropertyUtility.IsVisible(property.showIfAttribute, property.property); if (!visible) { return; } - + // Validate - ValidatorAttribute[] validatorAttributes = PropertyUtility.GetAttributes(property); - foreach (var validatorAttribute in validatorAttributes) + foreach (var validatorAttribute in property.validatorAttributes) { - validatorAttribute.GetValidator().ValidateProperty(property); + validatorAttribute.GetValidator().ValidateProperty(property.property); } - + // Check if enabled and draw EditorGUI.BeginChangeCheck(); - bool enabled = PropertyUtility.IsEnabled(property); - + bool enabled = PropertyUtility.IsEnabled(property.readOnlyAttribute, property.enableIfAttribute, property.property); + using (new EditorGUI.DisabledScope(disabled: !enabled)) { - propertyFieldFunction.Invoke(rect, property, PropertyUtility.GetLabel(property), includeChildren); + propertyFieldFunction.Invoke(rect, property, PropertyUtility.GetLabel(property.property), includeChildren); } - + // Call OnValueChanged callbacks if (EditorGUI.EndChangeCheck()) { - PropertyUtility.CallOnValueChangedCallbacks(property); + PropertyUtility.CallOnValueChangedCallbacks(property.property); } } } diff --git a/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs b/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs index 8a9b2c59..465e5edb 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs @@ -71,17 +71,23 @@ public static void CallOnValueChangedCallbacks(SerializedProperty property) public static bool IsEnabled(SerializedProperty property) { ReadOnlyAttribute readOnlyAttribute = GetAttribute(property); + EnableIfAttributeBase enableIfAttribute = GetAttribute(property); + + return IsEnabled(readOnlyAttribute, enableIfAttribute, property); + } + + public static bool IsEnabled(ReadOnlyAttribute readOnlyAttribute, EnableIfAttributeBase enableIfAttribute, SerializedProperty property) + { if (readOnlyAttribute != null) { return false; } - - EnableIfAttributeBase enableIfAttribute = GetAttribute(property); + if (enableIfAttribute == null) { return true; } - + object target = GetTargetObjectWithProperty(property); // deal with enum conditions @@ -118,10 +124,16 @@ public static bool IsEnabled(SerializedProperty property) return false; } } - + public static bool IsVisible(SerializedProperty property) { ShowIfAttributeBase showIfAttribute = GetAttribute(property); + + return IsVisible(showIfAttribute, property); + } + + public static bool IsVisible(ShowIfAttributeBase showIfAttribute, SerializedProperty property) + { if (showIfAttribute == null) { return true; @@ -142,7 +154,8 @@ public static bool IsVisible(SerializedProperty property) return matched != showIfAttribute.Inverted; } - string message = showIfAttribute.GetType().Name + " needs a valid enum field, property or method name to work"; + string message = showIfAttribute.GetType().Name + + " needs a valid enum field, property or method name to work"; Debug.LogWarning(message, property.serializedObject.targetObject); return false; @@ -152,12 +165,14 @@ public static bool IsVisible(SerializedProperty property) List conditionValues = GetConditionValues(target, showIfAttribute.Conditions); if (conditionValues.Count > 0) { - bool enabled = GetConditionsFlag(conditionValues, showIfAttribute.ConditionOperator, showIfAttribute.Inverted); + bool enabled = GetConditionsFlag(conditionValues, showIfAttribute.ConditionOperator, + showIfAttribute.Inverted); return enabled; } else { - string message = showIfAttribute.GetType().Name + " needs a valid boolean condition field, property or method name to work"; + string message = showIfAttribute.GetType().Name + + " needs a valid boolean condition field, property or method name to work"; Debug.LogWarning(message, property.serializedObject.targetObject); return false; From 489339d02982a467b39267049433d6120afacc34 Mon Sep 17 00:00:00 2001 From: Nigg Date: Mon, 6 Sep 2021 15:30:10 +0200 Subject: [PATCH 03/14] attributes still work rough test --- .../Samples/DemoScene/DemoScene.unity | 257 ++++++++++++++++++ 1 file changed, 257 insertions(+) diff --git a/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity b/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity index 6655bcca..49224701 100644 --- a/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity +++ b/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity @@ -513,6 +513,262 @@ MonoBehaviour: minMaxSlider1: {x: 0.25, y: 0.75} nest2: minMaxSlider2: {x: 6, y: 11} +--- !u!1 &645779700 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 645779701} + - component: {fileID: 645779702} + m_Layer: 0 + m_Name: HugeMixPerformanceTest + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &645779701 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 645779700} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 1148579784} + m_RootOrder: 33 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &645779702 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 645779700} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 9d0844cc6e62fe54393612902562c4e2, type: 3} + m_Name: + m_EditorClassIdentifier: + animator0: {fileID: 1178133862} + hash0: -392453409 + name0: Float1 + animatorParamNest1: + animator1: {fileID: 1178133862} + hash1: 726565755 + name1: Float1 + nest2: + animator2: {fileID: 1178133862} + hash1: 0 + name1: Trigger0 + int0: 25 + int1: 0 + float0: 0 + float1: 0 + slider0: {x: 0, y: 0} + slider1: {x: 0, y: 0} + str0: dfgdfg + str1: + trans0: {fileID: 0} + trans1: {fileID: 0} + myInt: 15 + curve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: -1 + value: -1 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0 + outWeight: 0 + - serializedVersion: 3 + time: 1 + value: 1 + inSlope: 2 + outSlope: 2 + tangentMode: 0 + weightedMode: 0 + inWeight: 0 + outWeight: 0 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 + curve1: + serializedVersion: 2 + m_Curve: [] + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 + curve2: + serializedVersion: 2 + m_Curve: [] + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 + curveRangeNest1: + curve: + serializedVersion: 2 + m_Curve: [] + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 + curveRangeNest2: + curve: + serializedVersion: 2 + m_Curve: [] + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 + disableIf1: 0 + disableIf2: 0 + disableIfEnum1: 2 + disableIfEnum2: 0 + disableIfAll: + disableIfAny: + disableIfEnum: + disableIfEnumFlag: + disableIfEnumFlagMulti: + disableIfNest1: + disable1: 0 + disable2: 0 + enum1: 0 + enum2: 0 + disableIfAll: 1 + disableIfAny: 2 + disableIfEnum: 3 + disableIfEnumFlag: 0 + disableIfEnumFlagMulti: 0 + nest2: + disable1: 0 + disable2: 0 + enum1: 0 + enum2: 0 + enableIfAll: {x: 0.25, y: 0.75} + enableIfAny: {x: 0.25, y: 0.75} + enableIfEnum: {x: 0.25, y: 0.75} + disableIfEnumFlag: {x: 0, y: 0} + disableIfEnumFlagMulti: {x: 0, y: 0} + dropDownIntValue: 1 + dropdownNest1: + stringValue: + nest2: + vectorValue: {x: 0, y: 0, z: 0} + enable1: 0 + enable2: 0 + enableIfEnum1: 0 + enableIfEnum2: 0 + enableIfAll: + enableIfAny: + enableIfEnum: + enableIfEnumFlag: + enableIfEnumFlagMulti: + enableIfNest1: + enable1: 0 + enable2: 0 + enum1: 0 + enum2: 0 + enableIfAll: 0 + enableIfAny: 0 + enableIfEnum: 0 + enableIfEnumFlag: 0 + enableIfEnumFlagMulti: 0 + nest2: + enable1: 0 + enable2: 0 + enum1: 0 + enum2: 0 + enableIfAll: {x: 0.25, y: 0.75} + enableIfAny: {x: 0.25, y: 0.75} + enableIfEnum: {x: 0.25, y: 0.75} + enableIfEnumFlag: {x: 0, y: 0} + enableIfEnumFlagMulti: {x: 0, y: 0} + enumFlagsTest0: 0 + enumFlagsNest1: + flags1: 0 + nest2: + flags2: 0 + obj0: {fileID: 0} + expandableScriptableObjectNest1: + obj1: {fileID: 0} + nest2: + obj2: {fileID: 0} + foldoutTestInt0: 27 + foldoutTestInt1: 0 + foldoutTestFloat0: 0 + foldoutTestFloat1: 0 + foldoutTestSlider0: {x: 0, y: 0.46343613} + foldoutTestSlider1: {x: 0, y: 1} + foldoutTestStr0: + foldoutTestStr1: + foldoutTestTrans0: {fileID: 0} + foldoutTestTrans1: {fileID: 0} + hide1: 0 + hide2: 0 + hideIfEnum1: 0 + hideIfEnum2: 0 + hideIfAll: + hideIfAny: + hideIfEnum: + hideIfEnumFlag: + hideIfEnumFlagMulti: + hideIfNest1: + hide1: 0 + hide2: 0 + enum1: 0 + enum2: 0 + hideIfAll: 0 + hideIfAny: 0 + hideIfEnum: 0 + hideIfEnumFlag: 0 + hideIfEnumFlagMulti: 0 + nest2: + hide1: 0 + hide2: 0 + enum1: 0 + enum2: 0 + hideIfAll: {x: 0.25, y: 0.75} + hideIfAny: {x: 0.25, y: 0.75} + hideIfEnum: {x: 0.25, y: 0.75} + hideIfEnumFlag: {x: 0, y: 0} + hideIfEnumFlagMulti: {x: 0, y: 0} + showIf1: 0 + showIf2: 0 + showIfEnum1: 0 + showIfEnum2: 0 + showIfAll: + showIfAny: + showIfEnum: + showIfEnumFlag: + showIfEnumFlagMulti: + showIfNest1: + show1: 0 + show2: 0 + enum1: 0 + enum2: 0 + showIfAll: 0 + showIfAny: 0 + showIfEnum: 0 + showIfEnumFlag: 0 + showIfEnumFlagMulti: 0 + nest2: + show1: 0 + show2: 0 + enum1: 0 + enum2: 0 + showIfAll: {x: 0.25, y: 0.75} + showIfAny: {x: 0.25, y: 0.75} + showIfEnum: {x: 0.25, y: 0.75} + showIfEnumFlag: {x: 0, y: 0} + showIfEnumFlagMulti: {x: 0, y: 0} --- !u!1 &706445688 GameObject: m_ObjectHideFlags: 0 @@ -943,6 +1199,7 @@ Transform: - {fileID: 369789277} - {fileID: 1463483878} - {fileID: 1468344835} + - {fileID: 645779701} m_Father: {fileID: 0} m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} From 51ab7830eb661f59c3eefc3f4230a0ec452cb33e Mon Sep 17 00:00:00 2001 From: Nigg Date: Mon, 6 Sep 2021 15:58:07 +0200 Subject: [PATCH 04/14] #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); From b35d73ca41f5f5a65a506006f816a201158f7513 Mon Sep 17 00:00:00 2001 From: Nigg Date: Mon, 6 Sep 2021 16:13:11 +0200 Subject: [PATCH 05/14] #133 some refactorings in the naming --- .../Scripts/Editor/NaughtyInspector.cs | 34 ++++++------------ .../Scripts/Editor/NaughtyProperty.cs | 17 +++++++++ .../Scripts/Editor/NaughtyProperty.cs.meta | 11 ++++++ .../Editor/Utility/NaughtyEditorGUI.cs | 36 +++++++++---------- .../Scripts/Editor/Utility/PropertyUtility.cs | 14 ++++---- 5 files changed, 64 insertions(+), 48 deletions(-) create mode 100644 Assets/NaughtyAttributes/Scripts/Editor/NaughtyProperty.cs create mode 100644 Assets/NaughtyAttributes/Scripts/Editor/NaughtyProperty.cs.meta diff --git a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs index e8cbb088..82e39b4b 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs @@ -8,19 +8,6 @@ namespace NaughtyAttributes.Editor { - public class NaughtyProperty - { - public SerializedProperty property; - public SpecialCaseDrawerAttribute specialCaseDrawerAttribute; - public ShowIfAttributeBase showIfAttribute; - - public EnableIfAttributeBase enableIfAttribute; - - public ReadOnlyAttribute readOnlyAttribute; - - public ValidatorAttribute[] validatorAttributes; - } - [CanEditMultipleObjects] [CustomEditor(typeof(UnityEngine.Object), true)] public class NaughtyInspector : UnityEditor.Editor @@ -55,12 +42,13 @@ protected virtual void OnEnable() GetSerializedProperties(ref _serializedProperties); - _anyNaughtyAttribute = _serializedProperties.Any(p => PropertyUtility.GetAttribute(p.property) != null); + _anyNaughtyAttribute = _serializedProperties.Any(p => PropertyUtility.GetAttribute(p.serializedProperty) != null); _nonGroupedSerializedProperty = GetNonGroupedProperties(_serializedProperties); - NaughtyProperty[] mScripts = _serializedProperties.Where(p => p.property.name.Equals("m_Script")).ToArray(); - m_ScriptProperty = mScripts.Length > 0 ? mScripts[0].property : null; + //.First(...) doesnt work for some reason because the m_Script field isnt loaded yet I assume + NaughtyProperty[] mScripts = _serializedProperties.Where(p => p.serializedProperty.name.Equals("m_Script")).ToArray(); + m_ScriptProperty = mScripts.Length > 0 ? mScripts[0].serializedProperty : null; _groupedSerialzedProperty = GetGroupedProperties(_serializedProperties); @@ -128,7 +116,7 @@ protected void DrawSerializedProperties() // Draw grouped serialized properties foreach (var group in _groupedSerialzedProperty) { - IEnumerable visibleProperties = group.Where(p => PropertyUtility.IsVisible(p.property)); + IEnumerable visibleProperties = group.Where(p => PropertyUtility.IsVisible(p.serializedProperty)); if (!visibleProperties.Any()) { continue; @@ -146,7 +134,7 @@ protected void DrawSerializedProperties() // Draw foldout serialized properties foreach (var group in _foldoutGroupedSerializedProperty) { - IEnumerable visibleProperties = group.Where(p => PropertyUtility.IsVisible(p.property)); + IEnumerable visibleProperties = group.Where(p => PropertyUtility.IsVisible(p.serializedProperty)); if (!visibleProperties.Any()) { continue; @@ -229,21 +217,21 @@ protected void DrawButtons(bool drawHeader = false) private static IEnumerable GetNonGroupedProperties(IEnumerable properties) { - return properties.Where(p => PropertyUtility.GetAttribute(p.property) == null && !p.property.name.Equals("m_Script")); + return properties.Where(p => PropertyUtility.GetAttribute(p.serializedProperty) == null && !p.serializedProperty.name.Equals("m_Script")); } private static IEnumerable> GetGroupedProperties(IEnumerable properties) { return properties - .Where(p => PropertyUtility.GetAttribute(p.property) != null) - .GroupBy(p => PropertyUtility.GetAttribute(p.property).Name); + .Where(p => PropertyUtility.GetAttribute(p.serializedProperty) != null) + .GroupBy(p => PropertyUtility.GetAttribute(p.serializedProperty).Name); } private static IEnumerable> GetFoldoutProperties(IEnumerable properties) { return properties - .Where(p => PropertyUtility.GetAttribute(p.property) != null) - .GroupBy(p => PropertyUtility.GetAttribute(p.property).Name); + .Where(p => PropertyUtility.GetAttribute(p.serializedProperty) != null) + .GroupBy(p => PropertyUtility.GetAttribute(p.serializedProperty).Name); } private static GUIStyle GetHeaderGUIStyle() diff --git a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyProperty.cs b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyProperty.cs new file mode 100644 index 00000000..f71a2bb8 --- /dev/null +++ b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyProperty.cs @@ -0,0 +1,17 @@ +using UnityEditor; + +namespace NaughtyAttributes.Editor +{ + public class NaughtyProperty + { + public SerializedProperty serializedProperty; + public SpecialCaseDrawerAttribute specialCaseDrawerAttribute; + public ShowIfAttributeBase showIfAttribute; + + public EnableIfAttributeBase enableIfAttribute; + + public ReadOnlyAttribute readOnlyAttribute; + + public ValidatorAttribute[] validatorAttributes; + } +} diff --git a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyProperty.cs.meta b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyProperty.cs.meta new file mode 100644 index 00000000..cbb03379 --- /dev/null +++ b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyProperty.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 12288d0262da08245af3b1fef6421c75 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs b/Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs index b9588c6e..6709f4c1 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs @@ -17,63 +17,63 @@ public static class NaughtyEditorGUI private static GUIStyle _buttonStyle = new GUIStyle(GUI.skin.button) { richText = true }; - private delegate void PropertyFieldFunction(Rect rect, NaughtyProperty property, GUIContent label, bool includeChildren); + private delegate void PropertyFieldFunction(Rect rect, NaughtyProperty naughtyProperty, GUIContent label, bool includeChildren); - public static void PropertyField(Rect rect, NaughtyProperty property, bool includeChildren) + public static void PropertyField(Rect rect, NaughtyProperty naughtyProperty, bool includeChildren) { - PropertyField_Implementation(rect, property, includeChildren, DrawPropertyField); + PropertyField_Implementation(rect, naughtyProperty, includeChildren, DrawPropertyField); } - public static void PropertyField_Layout(NaughtyProperty property, bool includeChildren) + public static void PropertyField_Layout(NaughtyProperty naughtyProperty, bool includeChildren) { Rect dummyRect = new Rect(); - PropertyField_Implementation(dummyRect, property, includeChildren, DrawPropertyField_Layout); + PropertyField_Implementation(dummyRect, naughtyProperty, includeChildren, DrawPropertyField_Layout); } - private static void DrawPropertyField(Rect rect, NaughtyProperty property, GUIContent label, bool includeChildren) + private static void DrawPropertyField(Rect rect, NaughtyProperty naughtyProperty, GUIContent label, bool includeChildren) { - EditorGUI.PropertyField(rect, property.property, label, includeChildren); + EditorGUI.PropertyField(rect, naughtyProperty.serializedProperty, label, includeChildren); } - private static void DrawPropertyField_Layout(Rect rect, NaughtyProperty property, GUIContent label, bool includeChildren) + private static void DrawPropertyField_Layout(Rect rect, NaughtyProperty naughtyProperty, GUIContent label, bool includeChildren) { - EditorGUILayout.PropertyField(property.property, label, includeChildren); + EditorGUILayout.PropertyField(naughtyProperty.serializedProperty, label, includeChildren); } - private static void PropertyField_Implementation(Rect rect, NaughtyProperty property, bool includeChildren, PropertyFieldFunction propertyFieldFunction) + private static void PropertyField_Implementation(Rect rect, NaughtyProperty naughtyProperty, bool includeChildren, PropertyFieldFunction propertyFieldFunction) { - if (property.specialCaseDrawerAttribute != null) + if (naughtyProperty.specialCaseDrawerAttribute != null) { - property.specialCaseDrawerAttribute.GetDrawer().OnGUI(rect, property.property); + naughtyProperty.specialCaseDrawerAttribute.GetDrawer().OnGUI(rect, naughtyProperty.serializedProperty); } else { // Check if visible - bool visible = PropertyUtility.IsVisible(property.showIfAttribute, property.property); + bool visible = PropertyUtility.IsVisible(naughtyProperty.showIfAttribute, naughtyProperty.serializedProperty); if (!visible) { return; } // Validate - foreach (var validatorAttribute in property.validatorAttributes) + foreach (var validatorAttribute in naughtyProperty.validatorAttributes) { - validatorAttribute.GetValidator().ValidateProperty(property.property); + validatorAttribute.GetValidator().ValidateProperty(naughtyProperty.serializedProperty); } // Check if enabled and draw EditorGUI.BeginChangeCheck(); - bool enabled = PropertyUtility.IsEnabled(property.readOnlyAttribute, property.enableIfAttribute, property.property); + bool enabled = PropertyUtility.IsEnabled(naughtyProperty.readOnlyAttribute, naughtyProperty.enableIfAttribute, naughtyProperty.serializedProperty); using (new EditorGUI.DisabledScope(disabled: !enabled)) { - propertyFieldFunction.Invoke(rect, property, PropertyUtility.GetLabel(property.property), includeChildren); + propertyFieldFunction.Invoke(rect, naughtyProperty, PropertyUtility.GetLabel(naughtyProperty.serializedProperty), includeChildren); } // Call OnValueChanged callbacks if (EditorGUI.EndChangeCheck()) { - PropertyUtility.CallOnValueChangedCallbacks(property.property); + PropertyUtility.CallOnValueChangedCallbacks(naughtyProperty.serializedProperty); } } } diff --git a/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs b/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs index 1f57bfbd..9337afe0 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs @@ -26,19 +26,19 @@ public static T[] GetAttributes(SerializedProperty property) where T : class return (T[])fieldInfo.GetCustomAttributes(typeof(T), true); } - public static NaughtyProperty CreateNaughtyProperty(SerializedProperty property) + public static NaughtyProperty CreateNaughtyProperty(SerializedProperty serializedProperty) { NaughtyProperty naughtyProperty = new NaughtyProperty(); - naughtyProperty.property = property; + naughtyProperty.serializedProperty = serializedProperty; - naughtyProperty.readOnlyAttribute = PropertyUtility.GetAttribute(naughtyProperty.property); - naughtyProperty.enableIfAttribute = PropertyUtility.GetAttribute(naughtyProperty.property); + naughtyProperty.readOnlyAttribute = PropertyUtility.GetAttribute(naughtyProperty.serializedProperty); + naughtyProperty.enableIfAttribute = PropertyUtility.GetAttribute(naughtyProperty.serializedProperty); - naughtyProperty.showIfAttribute = PropertyUtility.GetAttribute(naughtyProperty.property); - naughtyProperty.validatorAttributes = PropertyUtility.GetAttributes(naughtyProperty.property); + naughtyProperty.showIfAttribute = PropertyUtility.GetAttribute(naughtyProperty.serializedProperty); + naughtyProperty.validatorAttributes = PropertyUtility.GetAttributes(naughtyProperty.serializedProperty); naughtyProperty.specialCaseDrawerAttribute = - PropertyUtility.GetAttribute(naughtyProperty.property); + PropertyUtility.GetAttribute(naughtyProperty.serializedProperty); return naughtyProperty; } From 85d38ea3d1ee0e7b3fc2c72601733fc0f9eac22d Mon Sep 17 00:00:00 2001 From: Nigg Date: Mon, 6 Sep 2021 16:26:26 +0200 Subject: [PATCH 06/14] #133 another small boost --- .../Scripts/Editor/NaughtyInspector.cs | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs index 82e39b4b..649ace9b 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs @@ -60,7 +60,6 @@ protected virtual void OnDisable() ReorderableListPropertyDrawer.Instance.ClearCache(); } - public override void OnInspectorGUI() { if (!_anyNaughtyAttribute) @@ -77,9 +76,11 @@ public override void OnInspectorGUI() DrawButtons(); } - protected void GetSerializedProperties(ref List outSerializedProperties) + protected virtual void GetSerializedProperties(ref List outSerializedProperties) { outSerializedProperties.Clear(); + outSerializedProperties.TrimExcess(); + using (var iterator = serializedObject.GetIterator()) { if (iterator.NextVisible(true)) @@ -95,7 +96,7 @@ protected void GetSerializedProperties(ref List outSerializedPr } } - protected void DrawSerializedProperties() + protected virtual void DrawSerializedProperties() { serializedObject.Update(); @@ -108,24 +109,24 @@ protected void DrawSerializedProperties() } // Draw non-grouped serialized properties - foreach (var property in _nonGroupedSerializedProperty) + foreach (var naughtyProperty in _nonGroupedSerializedProperty) { - NaughtyEditorGUI.PropertyField_Layout(property, includeChildren: true); + NaughtyEditorGUI.PropertyField_Layout(naughtyProperty, includeChildren: true); } // Draw grouped serialized properties foreach (var group in _groupedSerialzedProperty) { - IEnumerable visibleProperties = group.Where(p => PropertyUtility.IsVisible(p.serializedProperty)); + IEnumerable visibleProperties = group.Where(p => PropertyUtility.IsVisible(p.showIfAttribute, p.serializedProperty)); if (!visibleProperties.Any()) { continue; } NaughtyEditorGUI.BeginBoxGroup_Layout(group.Key); - foreach (var property in visibleProperties) + foreach (var naughtyProperty in visibleProperties) { - NaughtyEditorGUI.PropertyField_Layout(property, includeChildren: true); + NaughtyEditorGUI.PropertyField_Layout(naughtyProperty, includeChildren: true); } NaughtyEditorGUI.EndBoxGroup_Layout(); @@ -134,7 +135,7 @@ protected void DrawSerializedProperties() // Draw foldout serialized properties foreach (var group in _foldoutGroupedSerializedProperty) { - IEnumerable visibleProperties = group.Where(p => PropertyUtility.IsVisible(p.serializedProperty)); + IEnumerable visibleProperties = group.Where(p => PropertyUtility.IsVisible(p.showIfAttribute, p.serializedProperty)); if (!visibleProperties.Any()) { continue; @@ -148,9 +149,9 @@ protected void DrawSerializedProperties() _foldouts[group.Key].Value = EditorGUILayout.Foldout(_foldouts[group.Key].Value, group.Key, true); if (_foldouts[group.Key].Value) { - foreach (var property in visibleProperties) + foreach (var naughtyProperty in visibleProperties) { - NaughtyEditorGUI.PropertyField_Layout(property, true); + NaughtyEditorGUI.PropertyField_Layout(naughtyProperty, true); } } } @@ -158,7 +159,7 @@ protected void DrawSerializedProperties() serializedObject.ApplyModifiedProperties(); } - protected void DrawNonSerializedFields(bool drawHeader = false) + protected virtual void DrawNonSerializedFields(bool drawHeader = false) { if (_nonSerializedFields.Any()) { @@ -177,7 +178,7 @@ protected void DrawNonSerializedFields(bool drawHeader = false) } } - protected void DrawNativeProperties(bool drawHeader = false) + protected virtual void DrawNativeProperties(bool drawHeader = false) { if (_nativeProperties.Any()) { @@ -196,7 +197,7 @@ protected void DrawNativeProperties(bool drawHeader = false) } } - protected void DrawButtons(bool drawHeader = false) + protected virtual void DrawButtons(bool drawHeader = false) { if (_methods.Any()) { From 5a955099164da55d77215b34ae6e84257ff1b180 Mon Sep 17 00:00:00 2001 From: Nigg Date: Mon, 6 Sep 2021 17:03:37 +0200 Subject: [PATCH 07/14] #133 memory cleanup refactoring and notes/ ideas to potentially improve the gathering of the required properties and attributes --- .../Samples/DemoScene/DemoScene.unity | 54 ++++++++++--------- .../Scripts/Editor/NaughtyInspector.cs | 34 +++++++++--- 2 files changed, 56 insertions(+), 32 deletions(-) diff --git a/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity b/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity index 49224701..0a53cbda 100644 --- a/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity +++ b/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity @@ -200,8 +200,8 @@ MonoBehaviour: m_EditorClassIdentifier: enable1: 0 enable2: 0 - enum1: 0 - enum2: 0 + enum1: 2 + enum2: 3 enableIfAll: enableIfAny: enableIfEnum: @@ -311,11 +311,11 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: e0cc8a31c22090847b75538c0ed2d2fc, type: 3} m_Name: m_EditorClassIdentifier: - inputAxis0: + inputAxis0: Fire1 nest1: - inputAxis1: Horizontal + inputAxis1: Vertical nest2: - inputAxis2: Vertical + inputAxis2: Fire2 --- !u!1 &237121640 GameObject: m_ObjectHideFlags: 0 @@ -508,9 +508,9 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: fd67fbde6acdd6a44944f12e507067c5, type: 3} m_Name: m_EditorClassIdentifier: - minMaxSlider0: {x: 0, y: 0.5} + minMaxSlider0: {x: 0.10512249, y: 0.5} nest1: - minMaxSlider1: {x: 0.25, y: 0.75} + minMaxSlider1: {x: 0.25, y: 0.5771715} nest2: minMaxSlider2: {x: 6, y: 11} --- !u!1 &645779700 @@ -577,7 +577,7 @@ MonoBehaviour: str1: trans0: {fileID: 0} trans1: {fileID: 0} - myInt: 15 + myInt: 13 curve: serializedVersion: 2 m_Curve: @@ -1039,7 +1039,7 @@ MonoBehaviour: nest2: stamina: 75 elixir: 50 - maxElixir: 100 + maxElixir: 71 --- !u!1 &993534225 GameObject: m_ObjectHideFlags: 0 @@ -1483,9 +1483,9 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 8bcc0d5613b48fb43bd36c9d37e99900, type: 3} m_Name: m_EditorClassIdentifier: - tag0: + tag0: Finish nest1: - tag1: MainCamera + tag1: EditorOnly nest2: tag2: Player --- !u!1 &1444377589 @@ -1624,7 +1624,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 94adafcfe59aa344c9b5596b2cc6ecd0, type: 3} m_Name: m_EditorClassIdentifier: - int0: 0 + int0: 2 nest1: int1: 0 nest2: @@ -1820,11 +1820,11 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 0dcb08e489c17644e9eacaa1ec5fe781, type: 3} m_Name: m_EditorClassIdentifier: - normal: 0 + normal: 37 nest1: - warning: 0 + warning: 37 nest2: - error: 0 + error: 70 --- !u!1 &1591883662 GameObject: m_ObjectHideFlags: 0 @@ -2113,22 +2113,24 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: c93fde7cd79390148ac576c3a159a77b, type: 3} m_Name: m_EditorClassIdentifier: - intArray: 020000000300000001000000 + intArray: 010000000300000002000000 vectorList: - - {x: 2, y: 2, z: 2} - {x: 3, y: 3, z: 3} + - {x: 2, y: 2, z: 2} - {x: 1, y: 1, z: 1} structList: - - Int: 2 - Float: 2 - Vector: {x: 2, y: 2, z: 2} - Int: 3 Float: 3 Vector: {x: 3, y: 3, z: 3} - Int: 1 - Float: 1 + Float: 1.96 Vector: {x: 1, y: 1, z: 1} - gameObjectsList: [] + - Int: 2 + Float: 2 + Vector: {x: 2, y: 2, z: 2} + gameObjectsList: + - {fileID: 0} + - {fileID: 0} transformsList: [] monoBehavioursList: [] --- !u!1 &1784643784 @@ -2174,13 +2176,13 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 3920f5ea384951b4990e4d9e8032d12e, type: 3} m_Name: m_EditorClassIdentifier: - int0: 0 + int0: 27 int1: 0 - float0: 0 + float0: 2.23 float1: 0 - slider0: {x: 0.25, y: 0.75} + slider0: {x: 0.33104458, y: 0.75} slider1: {x: 0.25, y: 0.75} - str0: + str0: gg str1: trans0: {fileID: 0} trans1: {fileID: 0} diff --git a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs index 649ace9b..c8b3cd0e 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System; using System.Linq; using System.Reflection; @@ -30,6 +29,34 @@ public class NaughtyInspector : UnityEditor.Editor private bool _anyNaughtyAttribute; protected virtual void OnEnable() + { + /* + * TODO: + * OnEnable is called for all monos and scriptable objects, + * which eats some one time perf after compilation and also takes some memory (although not noticeable) + * any other way to trigger this like via a custom editor/ window with on focus?? + * + * Selection.selectionChanged += ???? + * Base Mono and SO scripts that handle this?? + */ + + this.Prepare(); + } + + protected virtual void OnDisable() + { + //cleanup memory + ReorderableListPropertyDrawer.Instance.ClearCache(); + + _foldoutGroupedSerializedProperty = Enumerable.Empty>(); + _groupedSerialzedProperty = Enumerable.Empty>(); + _nonGroupedSerializedProperty = Enumerable.Empty(); + _serializedProperties.Clear(); + + m_ScriptProperty = default; + } + + public virtual void Prepare() { _nonSerializedFields = ReflectionUtility.GetAllFields( target, f => f.GetCustomAttributes(typeof(ShowNonSerializedFieldAttribute), true).Length > 0); @@ -54,11 +81,6 @@ protected virtual void OnEnable() _foldoutGroupedSerializedProperty = GetFoldoutProperties(_serializedProperties); } - - protected virtual void OnDisable() - { - ReorderableListPropertyDrawer.Instance.ClearCache(); - } public override void OnInspectorGUI() { From 92c907b96fc13b73d105511e0e4d76e05e1cb21e Mon Sep 17 00:00:00 2001 From: Nigg Date: Mon, 6 Sep 2021 17:09:40 +0200 Subject: [PATCH 08/14] reverted modified packages to use rider on my machine --- Packages/manifest.json | 1 - Packages/packages-lock.json | 9 --------- 2 files changed, 10 deletions(-) diff --git a/Packages/manifest.json b/Packages/manifest.json index 4aaf27e2..6c25be29 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -3,7 +3,6 @@ "com.unity.2d.sprite": "1.0.0", "com.unity.2d.tilemap": "1.0.0", "com.unity.collab-proxy": "1.3.9", - "com.unity.ide.rider": "3.0.7", "com.unity.ide.visualstudio": "2.0.7", "com.unity.test-framework": "1.1.24", "com.unity.textmeshpro": "3.0.4", diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index d719c879..f28a4c72 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -26,15 +26,6 @@ "dependencies": {}, "url": "https://packages.unity.com" }, - "com.unity.ide.rider": { - "version": "3.0.7", - "depth": 0, - "source": "registry", - "dependencies": { - "com.unity.ext.nunit": "1.0.6" - }, - "url": "https://packages.unity.com" - }, "com.unity.ide.visualstudio": { "version": "2.0.7", "depth": 0, From fa4a69e58877c2c472f370d6793c0b8800aa7580 Mon Sep 17 00:00:00 2001 From: Nigg Date: Fri, 10 Sep 2021 14:03:19 +0200 Subject: [PATCH 09/14] added animator controller support in the animator param attribute, so it can be used in scriptable objects aswell. also added caching --- .../Samples/DemoScene/DemoScene.unity | 21 +++++- .../TestAssets/NaughtyScriptableObject.asset | 3 + .../AnimatorParamPropertyDrawer.cs | 67 +++++++++++++------ .../Scripts/Test/AnimatorParamTest.cs | 40 +++++++++-- .../Scripts/Test/_NaughtyScriptableObject.cs | 18 +++++ 5 files changed, 123 insertions(+), 26 deletions(-) diff --git a/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity b/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity index 0a53cbda..5ae121f6 100644 --- a/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity +++ b/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity @@ -561,12 +561,18 @@ MonoBehaviour: name0: Float1 animatorParamNest1: animator1: {fileID: 1178133862} + animatorController1: {fileID: 0} hash1: 726565755 name1: Float1 + hashController1: 0 + nameController1: nest2: animator2: {fileID: 1178133862} + animatorController2: {fileID: 0} hash1: 0 name1: Trigger0 + hashController2: 0 + nameController2: int0: 25 int1: 0 float0: 0 @@ -1248,16 +1254,27 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: animator0: {fileID: 1178133862} - hash0: 1943738498 + animatorController0: {fileID: 9100000, guid: 63ee86efd213bf34285c95f33e79dc6c, type: 2} + hash0: -392453409 name0: Float0 + hashController0: 726565755 + nameController0: Trigger1 nest1: animator1: {fileID: 1178133862} - hash1: 726565755 + animatorController1: {fileID: 9100000, guid: 63ee86efd213bf34285c95f33e79dc6c, + type: 2} + hash1: 1548334061 name1: Float0 + hashController1: 1548334061 + nameController1: Float1 nest2: animator2: {fileID: 1178133862} + animatorController2: {fileID: 9100000, guid: 63ee86efd213bf34285c95f33e79dc6c, + type: 2} hash1: -392453409 name1: Trigger0 + hashController2: -392453409 + nameController2: Trigger1 --- !u!95 &1178133862 Animator: serializedVersion: 3 diff --git a/Assets/NaughtyAttributes/Samples/DemoScene/TestAssets/NaughtyScriptableObject.asset b/Assets/NaughtyAttributes/Samples/DemoScene/TestAssets/NaughtyScriptableObject.asset index ce97939e..673d2a6b 100644 --- a/Assets/NaughtyAttributes/Samples/DemoScene/TestAssets/NaughtyScriptableObject.asset +++ b/Assets/NaughtyAttributes/Samples/DemoScene/TestAssets/NaughtyScriptableObject.asset @@ -16,3 +16,6 @@ MonoBehaviour: - {fileID: 11400000, guid: 149474eb879a6a641b560ca17d48712f, type: 2} - {fileID: 11400000, guid: ca97c330d5c96794aa4df848d63f836b, type: 2} - {fileID: 0} + animatorController0: {fileID: 9100000, guid: 63ee86efd213bf34285c95f33e79dc6c, type: 2} + hashController0: -392453409 + nameController0: Trigger1 diff --git a/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/AnimatorParamPropertyDrawer.cs b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/AnimatorParamPropertyDrawer.cs index 451686b1..4e267e66 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/AnimatorParamPropertyDrawer.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/AnimatorParamPropertyDrawer.cs @@ -12,10 +12,16 @@ public class AnimatorParamPropertyDrawer : PropertyDrawerBase private const string InvalidAnimatorControllerWarningMessage = "Target animator controller is null"; private const string InvalidTypeWarningMessage = "{0} must be an int or a string"; + private AnimatorController _cachedAnimatorController; + protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label) { AnimatorParamAttribute animatorParamAttribute = PropertyUtility.GetAttribute(property); - bool validAnimatorController = GetAnimatorController(property, animatorParamAttribute.AnimatorName) != null; + + if (_cachedAnimatorController == null) + _cachedAnimatorController = GetAnimatorController(property, animatorParamAttribute.AnimatorName); + + bool validAnimatorController = _cachedAnimatorController != null; bool validPropertyType = property.propertyType == SerializedPropertyType.Integer || property.propertyType == SerializedPropertyType.String; return (validAnimatorController && validPropertyType) @@ -29,7 +35,7 @@ protected override void OnGUI_Internal(Rect rect, SerializedProperty property, G AnimatorParamAttribute animatorParamAttribute = PropertyUtility.GetAttribute(property); - AnimatorController animatorController = GetAnimatorController(property, animatorParamAttribute.AnimatorName); + AnimatorController animatorController = _cachedAnimatorController; if (animatorController == null) { DrawDefaultPropertyAndHelpBox(rect, property, InvalidAnimatorControllerWarningMessage, MessageType.Warning); @@ -131,39 +137,60 @@ private static AnimatorController GetAnimatorController(SerializedProperty prope object target = PropertyUtility.GetTargetObjectWithProperty(property); FieldInfo animatorFieldInfo = ReflectionUtility.GetField(target, animatorName); - if (animatorFieldInfo != null && - animatorFieldInfo.FieldType == typeof(Animator)) + if (animatorFieldInfo != null) { - Animator animator = animatorFieldInfo.GetValue(target) as Animator; - if (animator != null) + if (animatorFieldInfo.FieldType == typeof(Animator)) + { + Animator animator = animatorFieldInfo.GetValue(target) as Animator; + if (animator != null) + { + AnimatorController animatorController = + animator.runtimeAnimatorController as AnimatorController; + return animatorController; + } + } + else if (animatorFieldInfo.FieldType == typeof(AnimatorController)) { - AnimatorController animatorController = animator.runtimeAnimatorController as AnimatorController; - return animatorController; + return animatorFieldInfo.GetValue(target) as AnimatorController; } } PropertyInfo animatorPropertyInfo = ReflectionUtility.GetProperty(target, animatorName); - if (animatorPropertyInfo != null && - animatorPropertyInfo.PropertyType == typeof(Animator)) + if (animatorPropertyInfo != null) { - Animator animator = animatorPropertyInfo.GetValue(target) as Animator; - if (animator != null) + if (animatorPropertyInfo.PropertyType == typeof(Animator)) { - AnimatorController animatorController = animator.runtimeAnimatorController as AnimatorController; - return animatorController; + Animator animator = animatorPropertyInfo.GetValue(target) as Animator; + if (animator != null) + { + AnimatorController animatorController = + animator.runtimeAnimatorController as AnimatorController; + return animatorController; + } + } + else if (animatorPropertyInfo.PropertyType == typeof(AnimatorController)) + { + return animatorPropertyInfo.GetValue(target) as AnimatorController; } } MethodInfo animatorGetterMethodInfo = ReflectionUtility.GetMethod(target, animatorName); if (animatorGetterMethodInfo != null && - animatorGetterMethodInfo.ReturnType == typeof(Animator) && - animatorGetterMethodInfo.GetParameters().Length == 0) + animatorGetterMethodInfo.GetParameters().Length == 0) { - Animator animator = animatorGetterMethodInfo.Invoke(target, null) as Animator; - if (animator != null) + if (animatorGetterMethodInfo.ReturnType == typeof(Animator)) + { + Animator animator = animatorGetterMethodInfo.Invoke(target, null) as Animator; + if (animator != null) + { + AnimatorController animatorController = + animator.runtimeAnimatorController as AnimatorController; + return animatorController; + } + } + else if (animatorGetterMethodInfo.ReturnType == typeof(AnimatorController)) { - AnimatorController animatorController = animator.runtimeAnimatorController as AnimatorController; - return animatorController; + return animatorGetterMethodInfo.Invoke(target, null) as AnimatorController; } } diff --git a/Assets/NaughtyAttributes/Scripts/Test/AnimatorParamTest.cs b/Assets/NaughtyAttributes/Scripts/Test/AnimatorParamTest.cs index 0bbc476c..b95e23cb 100644 --- a/Assets/NaughtyAttributes/Scripts/Test/AnimatorParamTest.cs +++ b/Assets/NaughtyAttributes/Scripts/Test/AnimatorParamTest.cs @@ -1,4 +1,5 @@ -using UnityEngine; +using UnityEditor.Animations; +using UnityEngine; namespace NaughtyAttributes.Test { @@ -6,20 +7,31 @@ public class AnimatorParamTest : MonoBehaviour { public Animator animator0; + public AnimatorController animatorController0; + [AnimatorParam("animator0")] public int hash0; - [AnimatorParam("animator0")] public string name0; + + [AnimatorParam("animatorController0")] + public int hashController0; + + [AnimatorParam("animatorController0")] + public string nameController0; public AnimatorParamNest1 nest1; - [Button("Log 'hash0' and 'name0'")] + [Button("Log 'hash0', 'hashController0' and 'name0', 'nameController0'")] private void TestLog() { Debug.Log($"hash0 = {hash0}"); Debug.Log($"name0 = {name0}"); Debug.Log($"Animator.StringToHash(name0) = {Animator.StringToHash(name0)}"); + + Debug.Log($"hashController0 = {hashController0}"); + Debug.Log($"nameController0 = {nameController0}"); + Debug.Log($"Animator.StringToHash(nameController0) = {Animator.StringToHash(nameController0)}"); } } @@ -27,14 +39,24 @@ private void TestLog() public class AnimatorParamNest1 { public Animator animator1; - private Animator Animator1 => animator1; + public AnimatorController animatorController1; + private Animator Animator1 => animator1; + + private AnimatorController AnimatorController1 => animatorController1; + [AnimatorParam("Animator1", AnimatorControllerParameterType.Bool)] public int hash1; [AnimatorParam("Animator1", AnimatorControllerParameterType.Float)] public string name1; + + [AnimatorParam("AnimatorController1", AnimatorControllerParameterType.Bool)] + public int hashController1; + [AnimatorParam("AnimatorController1", AnimatorControllerParameterType.Float)] + public string nameController1; + public AnimatorParamNest2 nest2; } @@ -42,12 +64,22 @@ public class AnimatorParamNest1 public class AnimatorParamNest2 { public Animator animator2; + public AnimatorController animatorController2; + private Animator GetAnimator2() => animator2; + + private AnimatorController GetAnimatorController2() => animatorController2; [AnimatorParam("GetAnimator2", AnimatorControllerParameterType.Int)] public int hash1; [AnimatorParam("GetAnimator2", AnimatorControllerParameterType.Trigger)] public string name1; + + [AnimatorParam("GetAnimatorController2", AnimatorControllerParameterType.Int)] + public int hashController2; + + [AnimatorParam("GetAnimatorController2", AnimatorControllerParameterType.Trigger)] + public string nameController2; } } diff --git a/Assets/NaughtyAttributes/Scripts/Test/_NaughtyScriptableObject.cs b/Assets/NaughtyAttributes/Scripts/Test/_NaughtyScriptableObject.cs index f2cb1b68..4c67c665 100644 --- a/Assets/NaughtyAttributes/Scripts/Test/_NaughtyScriptableObject.cs +++ b/Assets/NaughtyAttributes/Scripts/Test/_NaughtyScriptableObject.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using UnityEditor.Animations; using UnityEngine; namespace NaughtyAttributes.Test @@ -8,5 +9,22 @@ public class _NaughtyScriptableObject : ScriptableObject { [Expandable] public List<_TestScriptableObject> list; + + public AnimatorController animatorController0; + + [AnimatorParam(nameof(animatorController0))] + public int hashController0; + + [AnimatorParam(nameof(animatorController0))] + public string nameController0; + + + [Button("Log 'hashController0' and 'nameController0'")] + private void TestLog() + { + Debug.Log($"hashController0 = {hashController0}"); + Debug.Log($"nameController0 = {nameController0}"); + Debug.Log($"Animator.StringToHash(nameController0) = {Animator.StringToHash(nameController0)}"); + } } } From 4f5a6b1e03909293716a9da38aaa91125241eee0 Mon Sep 17 00:00:00 2001 From: Nigg Date: Fri, 10 Sep 2021 14:28:40 +0200 Subject: [PATCH 10/14] #133 GetLabel Attribute caching --- .../Scripts/Editor/NaughtyInspector.cs | 10 +++++----- .../Scripts/Editor/NaughtyProperty.cs | 7 ++++--- .../Editor/Utility/NaughtyEditorGUI.cs | 2 +- .../Scripts/Editor/Utility/PropertyUtility.cs | 19 +++++++++++++------ 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs index c8b3cd0e..612825a9 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs @@ -144,16 +144,16 @@ protected virtual void DrawSerializedProperties() { continue; } - + NaughtyEditorGUI.BeginBoxGroup_Layout(group.Key); foreach (var naughtyProperty in visibleProperties) { NaughtyEditorGUI.PropertyField_Layout(naughtyProperty, includeChildren: true); } - + NaughtyEditorGUI.EndBoxGroup_Layout(); } - + // Draw foldout serialized properties foreach (var group in _foldoutGroupedSerializedProperty) { @@ -162,12 +162,12 @@ protected virtual void DrawSerializedProperties() { continue; } - + if (!_foldouts.ContainsKey(group.Key)) { _foldouts[group.Key] = new SavedBool($"{target.GetInstanceID()}.{group.Key}", false); } - + _foldouts[group.Key].Value = EditorGUILayout.Foldout(_foldouts[group.Key].Value, group.Key, true); if (_foldouts[group.Key].Value) { diff --git a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyProperty.cs b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyProperty.cs index f71a2bb8..269747b5 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyProperty.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyProperty.cs @@ -5,13 +5,14 @@ namespace NaughtyAttributes.Editor public class NaughtyProperty { public SerializedProperty serializedProperty; + + public LabelAttribute labelAttribute; + public SpecialCaseDrawerAttribute specialCaseDrawerAttribute; + public ShowIfAttributeBase showIfAttribute; - public EnableIfAttributeBase enableIfAttribute; - public ReadOnlyAttribute readOnlyAttribute; - public ValidatorAttribute[] validatorAttributes; } } diff --git a/Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs b/Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs index 6709f4c1..f15fc7ff 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs @@ -67,7 +67,7 @@ private static void PropertyField_Implementation(Rect rect, NaughtyProperty naug using (new EditorGUI.DisabledScope(disabled: !enabled)) { - propertyFieldFunction.Invoke(rect, naughtyProperty, PropertyUtility.GetLabel(naughtyProperty.serializedProperty), includeChildren); + propertyFieldFunction.Invoke(rect, naughtyProperty, PropertyUtility.GetLabel(naughtyProperty.labelAttribute, naughtyProperty.serializedProperty), includeChildren); } // Call OnValueChanged callbacks diff --git a/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs b/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs index 9337afe0..4f7dc6f1 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs @@ -31,14 +31,16 @@ public static NaughtyProperty CreateNaughtyProperty(SerializedProperty serialize NaughtyProperty naughtyProperty = new NaughtyProperty(); naughtyProperty.serializedProperty = serializedProperty; - naughtyProperty.readOnlyAttribute = PropertyUtility.GetAttribute(naughtyProperty.serializedProperty); - naughtyProperty.enableIfAttribute = PropertyUtility.GetAttribute(naughtyProperty.serializedProperty); + naughtyProperty.readOnlyAttribute = PropertyUtility.GetAttribute(serializedProperty); + naughtyProperty.enableIfAttribute = PropertyUtility.GetAttribute(serializedProperty); - naughtyProperty.showIfAttribute = PropertyUtility.GetAttribute(naughtyProperty.serializedProperty); - naughtyProperty.validatorAttributes = PropertyUtility.GetAttributes(naughtyProperty.serializedProperty); + naughtyProperty.showIfAttribute = PropertyUtility.GetAttribute(serializedProperty); + naughtyProperty.validatorAttributes = PropertyUtility.GetAttributes(serializedProperty); + naughtyProperty.labelAttribute = PropertyUtility.GetAttribute(serializedProperty); + naughtyProperty.specialCaseDrawerAttribute = - PropertyUtility.GetAttribute(naughtyProperty.serializedProperty); + PropertyUtility.GetAttribute(serializedProperty); return naughtyProperty; } @@ -46,6 +48,11 @@ public static NaughtyProperty CreateNaughtyProperty(SerializedProperty serialize public static GUIContent GetLabel(SerializedProperty property) { LabelAttribute labelAttribute = GetAttribute(property); + return GetLabel(labelAttribute, property); + } + + public static GUIContent GetLabel(LabelAttribute labelAttribute, SerializedProperty property) + { string labelText = (labelAttribute == null) ? property.displayName : labelAttribute.Label; @@ -155,7 +162,7 @@ public static bool IsVisible(ShowIfAttributeBase showIfAttribute, SerializedProp { return true; } - + object target = GetTargetObjectWithProperty(property); // deal with enum conditions From 8eb3aa1c3b8516466aa5d371dbc43d50328c85a8 Mon Sep 17 00:00:00 2001 From: Nigg Date: Fri, 10 Sep 2021 15:04:51 +0200 Subject: [PATCH 11/14] #133 caching isVisible, isEnabled and special drawers --- .../Scripts/Editor/NaughtyInspector.cs | 50 ++++++++++++++++--- .../Scripts/Editor/NaughtyProperty.cs | 3 ++ .../SpecialCasePropertyDrawerBase.cs | 21 +++++--- .../Editor/Utility/NaughtyEditorGUI.cs | 24 +++++---- 4 files changed, 74 insertions(+), 24 deletions(-) diff --git a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs index 612825a9..3552d636 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs @@ -27,6 +27,8 @@ public class NaughtyInspector : UnityEditor.Editor private Dictionary _foldouts = new Dictionary(); private bool _anyNaughtyAttribute; + private bool _useCachedMetaAttributes; + private bool _changeDetected; protected virtual void OnEnable() { @@ -80,10 +82,14 @@ public virtual void Prepare() _groupedSerialzedProperty = GetGroupedProperties(_serializedProperties); _foldoutGroupedSerializedProperty = GetFoldoutProperties(_serializedProperties); + + _useCachedMetaAttributes = false; } public override void OnInspectorGUI() { + _changeDetected = false; + if (!_anyNaughtyAttribute) { DrawDefaultInspector(); @@ -96,6 +102,8 @@ public override void OnInspectorGUI() DrawNonSerializedFields(); DrawNativeProperties(); DrawButtons(); + + _useCachedMetaAttributes = !_changeDetected; } protected virtual void GetSerializedProperties(ref List outSerializedProperties) @@ -133,13 +141,33 @@ protected virtual void DrawSerializedProperties() // Draw non-grouped serialized properties foreach (var naughtyProperty in _nonGroupedSerializedProperty) { - NaughtyEditorGUI.PropertyField_Layout(naughtyProperty, includeChildren: true); + if (!_useCachedMetaAttributes) + { + naughtyProperty.cachedIsVisible = PropertyUtility.IsVisible(naughtyProperty.showIfAttribute, + naughtyProperty.serializedProperty); + + naughtyProperty.cachedIsEnabled = PropertyUtility.IsEnabled(naughtyProperty.readOnlyAttribute, naughtyProperty.enableIfAttribute, + naughtyProperty.serializedProperty); + } + + _changeDetected |= NaughtyEditorGUI.PropertyField_Layout(naughtyProperty, includeChildren: true); } // Draw grouped serialized properties foreach (var group in _groupedSerialzedProperty) { - IEnumerable visibleProperties = group.Where(p => PropertyUtility.IsVisible(p.showIfAttribute, p.serializedProperty)); + IEnumerable visibleProperties = + _useCachedMetaAttributes + ? group.Where(p => p.cachedIsVisible) + : group.Where(p => + { + p.cachedIsEnabled = PropertyUtility.IsEnabled(p.readOnlyAttribute, p.enableIfAttribute, + p.serializedProperty); + + return p.cachedIsVisible = + PropertyUtility.IsVisible(p.showIfAttribute, p.serializedProperty); + }); + if (!visibleProperties.Any()) { continue; @@ -148,16 +176,26 @@ protected virtual void DrawSerializedProperties() NaughtyEditorGUI.BeginBoxGroup_Layout(group.Key); foreach (var naughtyProperty in visibleProperties) { - NaughtyEditorGUI.PropertyField_Layout(naughtyProperty, includeChildren: true); + _changeDetected |= NaughtyEditorGUI.PropertyField_Layout(naughtyProperty, includeChildren: true); } - NaughtyEditorGUI.EndBoxGroup_Layout(); } // Draw foldout serialized properties foreach (var group in _foldoutGroupedSerializedProperty) { - IEnumerable visibleProperties = group.Where(p => PropertyUtility.IsVisible(p.showIfAttribute, p.serializedProperty)); + IEnumerable visibleProperties = + _useCachedMetaAttributes + ? group.Where(p => p.cachedIsVisible) + : group.Where(p => + { + p.cachedIsEnabled = PropertyUtility.IsEnabled(p.readOnlyAttribute, p.enableIfAttribute, + p.serializedProperty); + + return p.cachedIsVisible = + PropertyUtility.IsVisible(p.showIfAttribute, p.serializedProperty); + }); + if (!visibleProperties.Any()) { continue; @@ -173,7 +211,7 @@ protected virtual void DrawSerializedProperties() { foreach (var naughtyProperty in visibleProperties) { - NaughtyEditorGUI.PropertyField_Layout(naughtyProperty, true); + _changeDetected |= NaughtyEditorGUI.PropertyField_Layout(naughtyProperty, true); } } } diff --git a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyProperty.cs b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyProperty.cs index 269747b5..2a68e6b3 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyProperty.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyProperty.cs @@ -14,5 +14,8 @@ public class NaughtyProperty public EnableIfAttributeBase enableIfAttribute; public ReadOnlyAttribute readOnlyAttribute; public ValidatorAttribute[] validatorAttributes; + + public bool cachedIsVisible = true; + public bool cachedIsEnabled = true; } } diff --git a/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers_SpecialCase/SpecialCasePropertyDrawerBase.cs b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers_SpecialCase/SpecialCasePropertyDrawerBase.cs index 8f283d0b..6f82f901 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers_SpecialCase/SpecialCasePropertyDrawerBase.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers_SpecialCase/SpecialCasePropertyDrawerBase.cs @@ -7,36 +7,41 @@ namespace NaughtyAttributes.Editor { public abstract class SpecialCasePropertyDrawerBase { - public void OnGUI(Rect rect, SerializedProperty property) + public bool OnGUI(Rect rect, NaughtyProperty naughtyProperty) { + bool changeDetected = false; + // Check if visible - bool visible = PropertyUtility.IsVisible(property); + bool visible = PropertyUtility.IsVisible(naughtyProperty.showIfAttribute, naughtyProperty.serializedProperty); if (!visible) { - return; + return false; } // Validate - ValidatorAttribute[] validatorAttributes = PropertyUtility.GetAttributes(property); + ValidatorAttribute[] validatorAttributes = naughtyProperty.validatorAttributes; foreach (var validatorAttribute in validatorAttributes) { - validatorAttribute.GetValidator().ValidateProperty(property); + validatorAttribute.GetValidator().ValidateProperty(naughtyProperty.serializedProperty); } // Check if enabled and draw EditorGUI.BeginChangeCheck(); - bool enabled = PropertyUtility.IsEnabled(property); + bool enabled = PropertyUtility.IsEnabled(naughtyProperty.readOnlyAttribute, naughtyProperty.enableIfAttribute, naughtyProperty.serializedProperty); using (new EditorGUI.DisabledScope(disabled: !enabled)) { - OnGUI_Internal(rect, property, PropertyUtility.GetLabel(property)); + OnGUI_Internal(rect, naughtyProperty.serializedProperty, PropertyUtility.GetLabel(naughtyProperty.labelAttribute, naughtyProperty.serializedProperty)); } // Call OnValueChanged callbacks if (EditorGUI.EndChangeCheck()) { - PropertyUtility.CallOnValueChangedCallbacks(property); + changeDetected = true; + PropertyUtility.CallOnValueChangedCallbacks(naughtyProperty.serializedProperty); } + + return changeDetected; } public float GetPropertyHeight(SerializedProperty property) diff --git a/Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs b/Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs index f15fc7ff..51a1d9f3 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs @@ -19,15 +19,15 @@ public static class NaughtyEditorGUI private delegate void PropertyFieldFunction(Rect rect, NaughtyProperty naughtyProperty, GUIContent label, bool includeChildren); - public static void PropertyField(Rect rect, NaughtyProperty naughtyProperty, bool includeChildren) + public static bool PropertyField(Rect rect, NaughtyProperty naughtyProperty, bool includeChildren) { - PropertyField_Implementation(rect, naughtyProperty, includeChildren, DrawPropertyField); + return PropertyField_Implementation(rect, naughtyProperty, includeChildren, DrawPropertyField); } - public static void PropertyField_Layout(NaughtyProperty naughtyProperty, bool includeChildren) + public static bool PropertyField_Layout(NaughtyProperty naughtyProperty, bool includeChildren) { Rect dummyRect = new Rect(); - PropertyField_Implementation(dummyRect, naughtyProperty, includeChildren, DrawPropertyField_Layout); + return PropertyField_Implementation(dummyRect, naughtyProperty, includeChildren, DrawPropertyField_Layout); } private static void DrawPropertyField(Rect rect, NaughtyProperty naughtyProperty, GUIContent label, bool includeChildren) @@ -40,19 +40,20 @@ private static void DrawPropertyField_Layout(Rect rect, NaughtyProperty naughtyP EditorGUILayout.PropertyField(naughtyProperty.serializedProperty, label, includeChildren); } - private static void PropertyField_Implementation(Rect rect, NaughtyProperty naughtyProperty, bool includeChildren, PropertyFieldFunction propertyFieldFunction) + private static bool PropertyField_Implementation(Rect rect, NaughtyProperty naughtyProperty, bool includeChildren, PropertyFieldFunction propertyFieldFunction) { + bool changeDetected = false; + if (naughtyProperty.specialCaseDrawerAttribute != null) { - naughtyProperty.specialCaseDrawerAttribute.GetDrawer().OnGUI(rect, naughtyProperty.serializedProperty); + return naughtyProperty.specialCaseDrawerAttribute.GetDrawer().OnGUI(rect, naughtyProperty); } else { // Check if visible - bool visible = PropertyUtility.IsVisible(naughtyProperty.showIfAttribute, naughtyProperty.serializedProperty); - if (!visible) + if (!naughtyProperty.cachedIsVisible) { - return; + return false; } // Validate @@ -63,7 +64,7 @@ private static void PropertyField_Implementation(Rect rect, NaughtyProperty naug // Check if enabled and draw EditorGUI.BeginChangeCheck(); - bool enabled = PropertyUtility.IsEnabled(naughtyProperty.readOnlyAttribute, naughtyProperty.enableIfAttribute, naughtyProperty.serializedProperty); + bool enabled = naughtyProperty.cachedIsEnabled; using (new EditorGUI.DisabledScope(disabled: !enabled)) { @@ -73,9 +74,12 @@ private static void PropertyField_Implementation(Rect rect, NaughtyProperty naug // Call OnValueChanged callbacks if (EditorGUI.EndChangeCheck()) { + changeDetected = true; PropertyUtility.CallOnValueChangedCallbacks(naughtyProperty.serializedProperty); } } + + return changeDetected; } public static float GetIndentLength(Rect sourceRect) From 1cc0aa69b6d719495f0ee4329677c3c2c79e9b15 Mon Sep 17 00:00:00 2001 From: Nigg Date: Fri, 10 Sep 2021 15:29:48 +0200 Subject: [PATCH 12/14] #133 waaay faster now --- .../Samples/DemoScene/DemoScene.unity | 4 +-- .../Scripts/Editor/NaughtyInspector.cs | 33 ++++++++++--------- .../AnimatorParamPropertyDrawer.cs | 12 +++++-- .../CurveRangePropertyDrawer.cs | 7 +++- .../ProgressBarPropertyDrawer.cs | 12 +++++-- .../PropertyDrawers/PropertyDrawerBase.cs | 7 +++- .../ShowAssetPreviewPropertyDrawer.cs | 8 ++++- .../MaxValuePropertyValidator.cs | 7 +++- .../MinValuePropertyValidator.cs | 7 +++- .../RequiredPropertyValidator.cs | 7 +++- .../ValidateInputPropertyValidator.cs | 7 +++- .../Scripts/Editor/Utility/PropertyUtility.cs | 2 ++ 12 files changed, 85 insertions(+), 28 deletions(-) diff --git a/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity b/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity index 0a53cbda..ed6078d0 100644 --- a/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity +++ b/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity @@ -557,7 +557,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: animator0: {fileID: 1178133862} - hash0: -392453409 + hash0: -1617128887 name0: Float1 animatorParamNest1: animator1: {fileID: 1178133862} @@ -577,7 +577,7 @@ MonoBehaviour: str1: trans0: {fileID: 0} trans1: {fileID: 0} - myInt: 13 + myInt: 24 curve: serializedVersion: 2 m_Curve: diff --git a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs index 3552d636..0b959a28 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs @@ -12,17 +12,17 @@ namespace NaughtyAttributes.Editor public class NaughtyInspector : UnityEditor.Editor { private List _serializedProperties = new List(); - private IEnumerable _nonSerializedFields; - private IEnumerable _nativeProperties; - private IEnumerable _methods; + private List _nonSerializedFields; + private List _nativeProperties; + private List _methods; - private IEnumerable _nonGroupedSerializedProperty; + private List _nonGroupedSerializedProperty; private SerializedProperty m_ScriptProperty; - private IEnumerable> _groupedSerialzedProperty; + private List> _groupedSerialzedProperty; - private IEnumerable> _foldoutGroupedSerializedProperty; + private List> _foldoutGroupedSerializedProperty; private Dictionary _foldouts = new Dictionary(); @@ -50,9 +50,12 @@ protected virtual void OnDisable() //cleanup memory ReorderableListPropertyDrawer.Instance.ClearCache(); - _foldoutGroupedSerializedProperty = Enumerable.Empty>(); - _groupedSerialzedProperty = Enumerable.Empty>(); - _nonGroupedSerializedProperty = Enumerable.Empty(); + _nonSerializedFields.Clear(); + _nativeProperties.Clear(); + _methods.Clear(); + _foldoutGroupedSerializedProperty.Clear(); + _groupedSerialzedProperty.Clear(); + _nonGroupedSerializedProperty.Clear(); _serializedProperties.Clear(); m_ScriptProperty = default; @@ -61,27 +64,27 @@ protected virtual void OnDisable() public virtual void Prepare() { _nonSerializedFields = ReflectionUtility.GetAllFields( - target, f => f.GetCustomAttributes(typeof(ShowNonSerializedFieldAttribute), true).Length > 0); + target, f => f.GetCustomAttributes(typeof(ShowNonSerializedFieldAttribute), true).Length > 0).ToList(); _nativeProperties = ReflectionUtility.GetAllProperties( - target, p => p.GetCustomAttributes(typeof(ShowNativePropertyAttribute), true).Length > 0); + target, p => p.GetCustomAttributes(typeof(ShowNativePropertyAttribute), true).Length > 0).ToList(); _methods = ReflectionUtility.GetAllMethods( - target, m => m.GetCustomAttributes(typeof(ButtonAttribute), true).Length > 0); + target, m => m.GetCustomAttributes(typeof(ButtonAttribute), true).Length > 0).ToList(); GetSerializedProperties(ref _serializedProperties); _anyNaughtyAttribute = _serializedProperties.Any(p => PropertyUtility.GetAttribute(p.serializedProperty) != null); - _nonGroupedSerializedProperty = GetNonGroupedProperties(_serializedProperties); + _nonGroupedSerializedProperty = GetNonGroupedProperties(_serializedProperties).ToList(); //.First(...) doesnt work for some reason because the m_Script field isnt loaded yet I assume NaughtyProperty[] mScripts = _serializedProperties.Where(p => p.serializedProperty.name.Equals("m_Script")).ToArray(); m_ScriptProperty = mScripts.Length > 0 ? mScripts[0].serializedProperty : null; - _groupedSerialzedProperty = GetGroupedProperties(_serializedProperties); + _groupedSerialzedProperty = GetGroupedProperties(_serializedProperties).ToList(); - _foldoutGroupedSerializedProperty = GetFoldoutProperties(_serializedProperties); + _foldoutGroupedSerializedProperty = GetFoldoutProperties(_serializedProperties).ToList(); _useCachedMetaAttributes = false; } diff --git a/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/AnimatorParamPropertyDrawer.cs b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/AnimatorParamPropertyDrawer.cs index 451686b1..74204986 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/AnimatorParamPropertyDrawer.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/AnimatorParamPropertyDrawer.cs @@ -9,12 +9,17 @@ namespace NaughtyAttributes.Editor [CustomPropertyDrawer(typeof(AnimatorParamAttribute))] public class AnimatorParamPropertyDrawer : PropertyDrawerBase { + private AnimatorParamAttribute _cachedAnimatorParamAttribute; + private const string InvalidAnimatorControllerWarningMessage = "Target animator controller is null"; private const string InvalidTypeWarningMessage = "{0} must be an int or a string"; protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label) { - AnimatorParamAttribute animatorParamAttribute = PropertyUtility.GetAttribute(property); + if (_cachedAnimatorParamAttribute == null) + _cachedAnimatorParamAttribute = PropertyUtility.GetAttribute(property); + + AnimatorParamAttribute animatorParamAttribute = _cachedAnimatorParamAttribute; bool validAnimatorController = GetAnimatorController(property, animatorParamAttribute.AnimatorName) != null; bool validPropertyType = property.propertyType == SerializedPropertyType.Integer || property.propertyType == SerializedPropertyType.String; @@ -27,7 +32,10 @@ protected override void OnGUI_Internal(Rect rect, SerializedProperty property, G { EditorGUI.BeginProperty(rect, label, property); - AnimatorParamAttribute animatorParamAttribute = PropertyUtility.GetAttribute(property); + if (_cachedAnimatorParamAttribute == null) + _cachedAnimatorParamAttribute = PropertyUtility.GetAttribute(property); + + AnimatorParamAttribute animatorParamAttribute = _cachedAnimatorParamAttribute; AnimatorController animatorController = GetAnimatorController(property, animatorParamAttribute.AnimatorName); if (animatorController == null) diff --git a/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/CurveRangePropertyDrawer.cs b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/CurveRangePropertyDrawer.cs index e7960689..995d15a5 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/CurveRangePropertyDrawer.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/CurveRangePropertyDrawer.cs @@ -6,6 +6,8 @@ namespace NaughtyAttributes.Editor [CustomPropertyDrawer(typeof(CurveRangeAttribute))] public class CurveRangePropertyDrawer : PropertyDrawerBase { + private CurveRangeAttribute _cachedCurveRangeAttribute; + protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label) { float propertyHeight = property.propertyType == SerializedPropertyType.AnimationCurve @@ -27,7 +29,10 @@ protected override void OnGUI_Internal(Rect rect, SerializedProperty property, G return; } - var attribute = PropertyUtility.GetAttribute(property); + if (_cachedCurveRangeAttribute == null) + _cachedCurveRangeAttribute = PropertyUtility.GetAttribute(property); + + var attribute = _cachedCurveRangeAttribute; EditorGUI.CurveField( rect, diff --git a/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ProgressBarPropertyDrawer.cs b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ProgressBarPropertyDrawer.cs index f1e2787e..62512832 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ProgressBarPropertyDrawer.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ProgressBarPropertyDrawer.cs @@ -7,9 +7,14 @@ namespace NaughtyAttributes.Editor [CustomPropertyDrawer(typeof(ProgressBarAttribute))] public class ProgressBarPropertyDrawer : PropertyDrawerBase { + private ProgressBarAttribute _cachedProgressBarAttribute; + protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label) { - ProgressBarAttribute progressBarAttribute = PropertyUtility.GetAttribute(property); + if (_cachedProgressBarAttribute == null) + _cachedProgressBarAttribute = PropertyUtility.GetAttribute(property); + + ProgressBarAttribute progressBarAttribute = _cachedProgressBarAttribute; var maxValue = GetMaxValue(property, progressBarAttribute); return IsNumber(property) && IsNumber(maxValue) @@ -28,7 +33,10 @@ protected override void OnGUI_Internal(Rect rect, SerializedProperty property, G return; } - ProgressBarAttribute progressBarAttribute = PropertyUtility.GetAttribute(property); + if (_cachedProgressBarAttribute == null) + _cachedProgressBarAttribute = PropertyUtility.GetAttribute(property); + + ProgressBarAttribute progressBarAttribute = _cachedProgressBarAttribute; var value = property.propertyType == SerializedPropertyType.Integer ? property.intValue : property.floatValue; var valueFormatted = property.propertyType == SerializedPropertyType.Integer ? value.ToString() : string.Format("{0:0.00}", value); var maxValue = GetMaxValue(property, progressBarAttribute); diff --git a/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/PropertyDrawerBase.cs b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/PropertyDrawerBase.cs index 4c5888de..d460a6cf 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/PropertyDrawerBase.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/PropertyDrawerBase.cs @@ -5,6 +5,8 @@ namespace NaughtyAttributes.Editor { public abstract class PropertyDrawerBase : PropertyDrawer { + private SpecialCaseDrawerAttribute _cachedSpecialCaseDrawerAttribute; + public sealed override void OnGUI(Rect rect, SerializedProperty property, GUIContent label) { // Check if visible @@ -57,7 +59,10 @@ protected virtual float GetPropertyHeight_Internal(SerializedProperty property, protected float GetPropertyHeight(SerializedProperty property) { - SpecialCaseDrawerAttribute specialCaseAttribute = PropertyUtility.GetAttribute(property); + if (_cachedSpecialCaseDrawerAttribute == null) + _cachedSpecialCaseDrawerAttribute = PropertyUtility.GetAttribute(property); + + SpecialCaseDrawerAttribute specialCaseAttribute = _cachedSpecialCaseDrawerAttribute; if (specialCaseAttribute != null) { return specialCaseAttribute.GetDrawer().GetPropertyHeight(property); diff --git a/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ShowAssetPreviewPropertyDrawer.cs b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ShowAssetPreviewPropertyDrawer.cs index 0d0fe9be..d0bd8521 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ShowAssetPreviewPropertyDrawer.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/PropertyDrawers/ShowAssetPreviewPropertyDrawer.cs @@ -6,6 +6,8 @@ namespace NaughtyAttributes.Editor [CustomPropertyDrawer(typeof(ShowAssetPreviewAttribute))] public class ShowAssetPreviewPropertyDrawer : PropertyDrawerBase { + private ShowAssetPreviewAttribute _cachedShowAssetPreviewAttribute; + protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label) { if (property.propertyType == SerializedPropertyType.ObjectReference) @@ -93,7 +95,11 @@ private Vector2 GetAssetPreviewSize(SerializedProperty property) int targetWidth = ShowAssetPreviewAttribute.DefaultWidth; int targetHeight = ShowAssetPreviewAttribute.DefaultHeight; - ShowAssetPreviewAttribute showAssetPreviewAttribute = PropertyUtility.GetAttribute(property); + if (_cachedShowAssetPreviewAttribute == null) + _cachedShowAssetPreviewAttribute = + PropertyUtility.GetAttribute(property); + + ShowAssetPreviewAttribute showAssetPreviewAttribute = _cachedShowAssetPreviewAttribute; if (showAssetPreviewAttribute != null) { targetWidth = showAssetPreviewAttribute.Width; diff --git a/Assets/NaughtyAttributes/Scripts/Editor/PropertyValidators/MaxValuePropertyValidator.cs b/Assets/NaughtyAttributes/Scripts/Editor/PropertyValidators/MaxValuePropertyValidator.cs index 16ab419b..2e10ba5b 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/PropertyValidators/MaxValuePropertyValidator.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/PropertyValidators/MaxValuePropertyValidator.cs @@ -5,9 +5,14 @@ namespace NaughtyAttributes.Editor { public class MaxValuePropertyValidator : PropertyValidatorBase { + private MaxValueAttribute _cachedMaxValueAttribute; + public override void ValidateProperty(SerializedProperty property) { - MaxValueAttribute maxValueAttribute = PropertyUtility.GetAttribute(property); + if (_cachedMaxValueAttribute == null) + _cachedMaxValueAttribute = PropertyUtility.GetAttribute(property); + + MaxValueAttribute maxValueAttribute = _cachedMaxValueAttribute; if (property.propertyType == SerializedPropertyType.Integer) { diff --git a/Assets/NaughtyAttributes/Scripts/Editor/PropertyValidators/MinValuePropertyValidator.cs b/Assets/NaughtyAttributes/Scripts/Editor/PropertyValidators/MinValuePropertyValidator.cs index 69cd7299..5338a401 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/PropertyValidators/MinValuePropertyValidator.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/PropertyValidators/MinValuePropertyValidator.cs @@ -5,9 +5,14 @@ namespace NaughtyAttributes.Editor { public class MinValuePropertyValidator : PropertyValidatorBase { + private MinValueAttribute _cachedMinValueAttribute; + public override void ValidateProperty(SerializedProperty property) { - MinValueAttribute minValueAttribute = PropertyUtility.GetAttribute(property); + if (_cachedMinValueAttribute == null) + _cachedMinValueAttribute = PropertyUtility.GetAttribute(property); + + MinValueAttribute minValueAttribute = _cachedMinValueAttribute; if (property.propertyType == SerializedPropertyType.Integer) { diff --git a/Assets/NaughtyAttributes/Scripts/Editor/PropertyValidators/RequiredPropertyValidator.cs b/Assets/NaughtyAttributes/Scripts/Editor/PropertyValidators/RequiredPropertyValidator.cs index b1d15eb8..e76571a9 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/PropertyValidators/RequiredPropertyValidator.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/PropertyValidators/RequiredPropertyValidator.cs @@ -4,9 +4,14 @@ namespace NaughtyAttributes.Editor { public class RequiredPropertyValidator : PropertyValidatorBase { + private RequiredAttribute _cachedRequiredAttribute; + public override void ValidateProperty(SerializedProperty property) { - RequiredAttribute requiredAttribute = PropertyUtility.GetAttribute(property); + if (_cachedRequiredAttribute == null) + _cachedRequiredAttribute = PropertyUtility.GetAttribute(property); + + RequiredAttribute requiredAttribute = _cachedRequiredAttribute; if (property.propertyType == SerializedPropertyType.ObjectReference) { diff --git a/Assets/NaughtyAttributes/Scripts/Editor/PropertyValidators/ValidateInputPropertyValidator.cs b/Assets/NaughtyAttributes/Scripts/Editor/PropertyValidators/ValidateInputPropertyValidator.cs index 96277bb7..c1a431b2 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/PropertyValidators/ValidateInputPropertyValidator.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/PropertyValidators/ValidateInputPropertyValidator.cs @@ -6,9 +6,14 @@ namespace NaughtyAttributes.Editor { public class ValidateInputPropertyValidator : PropertyValidatorBase { + private ValidateInputAttribute _cachedValidateInputAttribute; + public override void ValidateProperty(SerializedProperty property) { - ValidateInputAttribute validateInputAttribute = PropertyUtility.GetAttribute(property); + if (_cachedValidateInputAttribute == null) + _cachedValidateInputAttribute = PropertyUtility.GetAttribute(property); + + ValidateInputAttribute validateInputAttribute = _cachedValidateInputAttribute; object target = PropertyUtility.GetTargetObjectWithProperty(property); MethodInfo validationCallback = ReflectionUtility.GetMethod(target, validateInputAttribute.CallbackName); diff --git a/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs b/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs index 4f7dc6f1..f73796e7 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs @@ -11,6 +11,8 @@ public static class PropertyUtility { public static T GetAttribute(SerializedProperty property) where T : class { + //Debug.Log("waste of time"); + T[] attributes = GetAttributes(property); return (attributes.Length > 0) ? attributes[0] : null; } From 16063680937fa67faa5375973917cb882995ca1c Mon Sep 17 00:00:00 2001 From: Nigg Date: Fri, 10 Sep 2021 16:12:05 +0200 Subject: [PATCH 13/14] removed log comment to detect overused reflection calls - still used in the base property drawers but dont know how to reach them --- .../Samples/DemoScene/DemoScene.unity | 18 +++++++++--------- .../Scripts/Editor/Utility/PropertyUtility.cs | 2 -- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity b/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity index ed6078d0..d08624a7 100644 --- a/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity +++ b/Assets/NaughtyAttributes/Samples/DemoScene/DemoScene.unity @@ -663,9 +663,9 @@ MonoBehaviour: nest2: vectorValue: {x: 0, y: 0, z: 0} enable1: 0 - enable2: 0 - enableIfEnum1: 0 - enableIfEnum2: 0 + enable2: 1 + enableIfEnum1: 1 + enableIfEnum2: 10 enableIfAll: enableIfAny: enableIfEnum: @@ -691,7 +691,7 @@ MonoBehaviour: enableIfEnum: {x: 0.25, y: 0.75} enableIfEnumFlag: {x: 0, y: 0} enableIfEnumFlagMulti: {x: 0, y: 0} - enumFlagsTest0: 0 + enumFlagsTest0: 1 enumFlagsNest1: flags1: 0 nest2: @@ -705,7 +705,7 @@ MonoBehaviour: foldoutTestInt1: 0 foldoutTestFloat0: 0 foldoutTestFloat1: 0 - foldoutTestSlider0: {x: 0, y: 0.46343613} + foldoutTestSlider0: {x: 0.4172527, y: 0.8947535} foldoutTestSlider1: {x: 0, y: 1} foldoutTestStr0: foldoutTestStr1: @@ -713,7 +713,7 @@ MonoBehaviour: foldoutTestTrans1: {fileID: 0} hide1: 0 hide2: 0 - hideIfEnum1: 0 + hideIfEnum1: 1 hideIfEnum2: 0 hideIfAll: hideIfAny: @@ -740,10 +740,10 @@ MonoBehaviour: hideIfEnum: {x: 0.25, y: 0.75} hideIfEnumFlag: {x: 0, y: 0} hideIfEnumFlagMulti: {x: 0, y: 0} - showIf1: 0 + showIf1: 1 showIf2: 0 - showIfEnum1: 0 - showIfEnum2: 0 + showIfEnum1: 1 + showIfEnum2: 1 showIfAll: showIfAny: showIfEnum: diff --git a/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs b/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs index f73796e7..4f7dc6f1 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs @@ -11,8 +11,6 @@ public static class PropertyUtility { public static T GetAttribute(SerializedProperty property) where T : class { - //Debug.Log("waste of time"); - T[] attributes = GetAttributes(property); return (attributes.Length > 0) ? attributes[0] : null; } From cc1828724bda28e164dc94fbf85346c7c90bb23b Mon Sep 17 00:00:00 2001 From: Nigg Date: Fri, 10 Sep 2021 17:01:44 +0200 Subject: [PATCH 14/14] fixed build error because animator controller is in editor namespace --- .../Scripts/Test/AnimatorParamTest.cs | 31 +++++++++++++------ .../Scripts/Test/_NaughtyScriptableObject.cs | 9 +++--- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/Assets/NaughtyAttributes/Scripts/Test/AnimatorParamTest.cs b/Assets/NaughtyAttributes/Scripts/Test/AnimatorParamTest.cs index b95e23cb..775e9206 100644 --- a/Assets/NaughtyAttributes/Scripts/Test/AnimatorParamTest.cs +++ b/Assets/NaughtyAttributes/Scripts/Test/AnimatorParamTest.cs @@ -1,5 +1,4 @@ -using UnityEditor.Animations; -using UnityEngine; +using UnityEngine; namespace NaughtyAttributes.Test { @@ -7,7 +6,9 @@ public class AnimatorParamTest : MonoBehaviour { public Animator animator0; - public AnimatorController animatorController0; +#if UNITY_EDITOR + public UnityEditor.Animations.AnimatorController animatorController0; +#endif [AnimatorParam("animator0")] public int hash0; @@ -39,11 +40,16 @@ private void TestLog() public class AnimatorParamNest1 { public Animator animator1; - public AnimatorController animatorController1; - + +#if UNITY_EDITOR + public UnityEditor.Animations.AnimatorController animatorController1; +#endif + private Animator Animator1 => animator1; - private AnimatorController AnimatorController1 => animatorController1; +#if UNITY_EDITOR + private UnityEditor.Animations.AnimatorController AnimatorController1 => animatorController1; +#endif [AnimatorParam("Animator1", AnimatorControllerParameterType.Bool)] public int hash1; @@ -64,12 +70,17 @@ public class AnimatorParamNest1 public class AnimatorParamNest2 { public Animator animator2; - public AnimatorController animatorController2; - + +#if UNITY_EDITOR + public UnityEditor.Animations.AnimatorController animatorController2; +#endif + private Animator GetAnimator2() => animator2; - private AnimatorController GetAnimatorController2() => animatorController2; - +#if UNITY_EDITOR + private UnityEditor.Animations.AnimatorController GetAnimatorController2() => animatorController2; +#endif + [AnimatorParam("GetAnimator2", AnimatorControllerParameterType.Int)] public int hash1; diff --git a/Assets/NaughtyAttributes/Scripts/Test/_NaughtyScriptableObject.cs b/Assets/NaughtyAttributes/Scripts/Test/_NaughtyScriptableObject.cs index 4c67c665..41609c8d 100644 --- a/Assets/NaughtyAttributes/Scripts/Test/_NaughtyScriptableObject.cs +++ b/Assets/NaughtyAttributes/Scripts/Test/_NaughtyScriptableObject.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using UnityEditor.Animations; using UnityEngine; namespace NaughtyAttributes.Test @@ -10,12 +9,14 @@ public class _NaughtyScriptableObject : ScriptableObject [Expandable] public List<_TestScriptableObject> list; - public AnimatorController animatorController0; +#if UNITY_EDITOR + public UnityEditor.Animations.AnimatorController animatorController0; +#endif - [AnimatorParam(nameof(animatorController0))] + [AnimatorParam("animatorController0")] public int hashController0; - [AnimatorParam(nameof(animatorController0))] + [AnimatorParam("animatorController0")] public string nameController0;