From ec271c8b939de14a78a4447b817cd129c937de3f Mon Sep 17 00:00:00 2001 From: Artemis Date: Sat, 13 May 2023 02:34:27 +0530 Subject: [PATCH 1/3] support easy buttons --- .../Scripts/Editor/NaughtyInspector.cs | 59 ++++++++++++++++++- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs index cce935b4..0c842c90 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs @@ -1,4 +1,6 @@ -using System.Collections.Generic; +using System; +using System.Collections; +using System.Collections.Generic; using System.Linq; using System.Reflection; using UnityEditor; @@ -15,7 +17,10 @@ public class NaughtyInspector : UnityEditor.Editor private IEnumerable _nativeProperties; private IEnumerable _methods; private Dictionary _foldouts = new Dictionary(); - + + private delegate void EBDrawMethodDel(IEnumerable targets); + private EBDrawMethodDel _ebEbDrawMethod; + protected virtual void OnEnable() { _nonSerializedFields = ReflectionUtility.GetAllFields( @@ -26,6 +31,52 @@ protected virtual void OnEnable() _methods = ReflectionUtility.GetAllMethods( target, m => m.GetCustomAttributes(typeof(ButtonAttribute), true).Length > 0); + + EasyButtonSupport(); + } + + private void EasyButtonSupport() + { + var ebDrawerType = Type.GetType("EasyButtons.Editor.ButtonsDrawer, EasyButtons.Editor"); + + if(ebDrawerType == null) return; + + var constructor = ebDrawerType.GetConstructor(new [] {typeof(object)}); + if(constructor == null) + { + Debug.LogWarning("NaughtyAttributes: EasyButtons.ButtonAttribute constructor not found"); + return; + } + + var ebDrawer = constructor.Invoke(new[] { target }); + if(ebDrawer == null) + { + Debug.LogWarning("NaughtyAttributes: EasyButtons.ButtonAttribute constructor failed"); + return; + } + + var buttonsListField = ebDrawerType.GetField("Buttons", BindingFlags.Instance | BindingFlags.Public); + if(buttonsListField == null) + { + Debug.LogWarning("NaughtyAttributes: EasyButtons.Editor.ButtonsDrawer.Buttons field not found"); + return; + } + + var buttonsList = buttonsListField.GetValue(ebDrawer) as IList; + + if(buttonsList == null || buttonsList.Count == 0) + { + return; + } + + var drawMethodInfo = ebDrawerType.GetMethod("DrawButtons", new [] { typeof(IEnumerable) }); + if(drawMethodInfo == null) + { + Debug.LogWarning("NaughtyAttributes: EasyButtons.Editor.ButtonsDrawer.DrawButtons method not found"); + return; + } + + _ebEbDrawMethod = drawMethodInfo.CreateDelegate(typeof(EBDrawMethodDel), ebDrawer) as EBDrawMethodDel; } protected virtual void OnDisable() @@ -173,7 +224,7 @@ protected void DrawNativeProperties(bool drawHeader = false) protected void DrawButtons(bool drawHeader = false) { - if (_methods.Any()) + if (_methods.Any() || _ebEbDrawMethod != null) { if (drawHeader) { @@ -187,6 +238,8 @@ protected void DrawButtons(bool drawHeader = false) { NaughtyEditorGUI.Button(serializedObject.targetObject, method); } + + _ebEbDrawMethod?.Invoke(targets); } } From 0752f7c4050bd34afe5d531be60663eecd225520 Mon Sep 17 00:00:00 2001 From: Artemis Date: Sat, 13 May 2023 10:24:03 +0530 Subject: [PATCH 2/3] simplify easy buttons check --- .../Scripts/Editor/NaughtyInspector.cs | 48 ++++++------------- 1 file changed, 15 insertions(+), 33 deletions(-) diff --git a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs index 0c842c90..8e351d1f 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs @@ -31,52 +31,34 @@ protected virtual void OnEnable() _methods = ReflectionUtility.GetAllMethods( target, m => m.GetCustomAttributes(typeof(ButtonAttribute), true).Length > 0); - - EasyButtonSupport(); + + if (!EasyButtonSupport()) + { + Debug.LogWarning("EasyButtons drawer definition does not match"); + } } - private void EasyButtonSupport() + private bool EasyButtonSupport() { var ebDrawerType = Type.GetType("EasyButtons.Editor.ButtonsDrawer, EasyButtons.Editor"); - - if(ebDrawerType == null) return; + if (ebDrawerType == null) return true; var constructor = ebDrawerType.GetConstructor(new [] {typeof(object)}); - if(constructor == null) - { - Debug.LogWarning("NaughtyAttributes: EasyButtons.ButtonAttribute constructor not found"); - return; - } - - var ebDrawer = constructor.Invoke(new[] { target }); - if(ebDrawer == null) - { - Debug.LogWarning("NaughtyAttributes: EasyButtons.ButtonAttribute constructor failed"); - return; - } + var ebDrawer = constructor?.Invoke(new[] { target }); + if (ebDrawer == null) return false; var buttonsListField = ebDrawerType.GetField("Buttons", BindingFlags.Instance | BindingFlags.Public); - if(buttonsListField == null) - { - Debug.LogWarning("NaughtyAttributes: EasyButtons.Editor.ButtonsDrawer.Buttons field not found"); - return; - } + if (buttonsListField == null) return false; var buttonsList = buttonsListField.GetValue(ebDrawer) as IList; - if(buttonsList == null || buttonsList.Count == 0) - { - return; - } - - var drawMethodInfo = ebDrawerType.GetMethod("DrawButtons", new [] { typeof(IEnumerable) }); - if(drawMethodInfo == null) - { - Debug.LogWarning("NaughtyAttributes: EasyButtons.Editor.ButtonsDrawer.DrawButtons method not found"); - return; - } + if (buttonsList == null || buttonsList.Count == 0) return true; + var drawMethodInfo = ebDrawerType.GetMethod("DrawButtons", new [] { typeof(IEnumerable) }); + if (drawMethodInfo == null) return false; + _ebEbDrawMethod = drawMethodInfo.CreateDelegate(typeof(EBDrawMethodDel), ebDrawer) as EBDrawMethodDel; + return true; } protected virtual void OnDisable() From 54ca0179e71af6ffec6b71be00dc599c253a0234 Mon Sep 17 00:00:00 2001 From: Artemis152 <37626459+Artemis-chan@users.noreply.github.com> Date: Sat, 13 May 2023 10:34:41 +0530 Subject: [PATCH 3/3] Better name for easy buttons check --- Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs index 8e351d1f..0c5e7f0a 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs @@ -32,13 +32,13 @@ protected virtual void OnEnable() _methods = ReflectionUtility.GetAllMethods( target, m => m.GetCustomAttributes(typeof(ButtonAttribute), true).Length > 0); - if (!EasyButtonSupport()) + if (!BindEasyButtonDrawer()) { Debug.LogWarning("EasyButtons drawer definition does not match"); } } - private bool EasyButtonSupport() + private bool BindEasyButtonDrawer() { var ebDrawerType = Type.GetType("EasyButtons.Editor.ButtonsDrawer, EasyButtons.Editor"); if (ebDrawerType == null) return true;