Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support easy buttons #356

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,7 +17,10 @@ public class NaughtyInspector : UnityEditor.Editor
private IEnumerable<PropertyInfo> _nativeProperties;
private IEnumerable<MethodInfo> _methods;
private Dictionary<string, SavedBool> _foldouts = new Dictionary<string, SavedBool>();


private delegate void EBDrawMethodDel(IEnumerable<object> targets);
private EBDrawMethodDel _ebEbDrawMethod;

protected virtual void OnEnable()
{
_nonSerializedFields = ReflectionUtility.GetAllFields(
Expand All @@ -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<object>) });
if (drawMethodInfo == null) return false;

_ebEbDrawMethod = drawMethodInfo.CreateDelegate(typeof(EBDrawMethodDel), ebDrawer) as EBDrawMethodDel;
return true;
}

protected virtual void OnDisable()
Expand Down Expand Up @@ -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)
{
Expand All @@ -187,6 +220,8 @@ protected void DrawButtons(bool drawHeader = false)
{
NaughtyEditorGUI.Button(serializedObject.targetObject, method);
}

_ebEbDrawMethod?.Invoke(targets);
}
}

Expand Down