diff --git a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs index cce935b4..0c5e7f0a 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,34 @@ protected virtual void OnEnable() _methods = ReflectionUtility.GetAllMethods( target, m => m.GetCustomAttributes(typeof(ButtonAttribute), true).Length > 0); + + if (!BindEasyButtonDrawer()) + { + Debug.LogWarning("EasyButtons drawer definition does not match"); + } + } + + private bool BindEasyButtonDrawer() + { + var ebDrawerType = Type.GetType("EasyButtons.Editor.ButtonsDrawer, EasyButtons.Editor"); + if (ebDrawerType == null) return true; + + var constructor = ebDrawerType.GetConstructor(new [] {typeof(object)}); + var ebDrawer = constructor?.Invoke(new[] { target }); + if (ebDrawer == null) return false; + + var buttonsListField = ebDrawerType.GetField("Buttons", BindingFlags.Instance | BindingFlags.Public); + if (buttonsListField == null) return false; + + var buttonsList = buttonsListField.GetValue(ebDrawer) as IList; + + 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() @@ -173,7 +206,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 +220,8 @@ protected void DrawButtons(bool drawHeader = false) { NaughtyEditorGUI.Button(serializedObject.targetObject, method); } + + _ebEbDrawMethod?.Invoke(targets); } }