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; }