Skip to content

Commit

Permalink
ButtonMethodAttribute is now working with SO as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Deadcows committed Apr 22, 2019
1 parent 195fd9b commit ce038dc
Showing 1 changed file with 55 additions and 21 deletions.
76 changes: 55 additions & 21 deletions Attributes/ButtonMethodAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ButtonMethodAttribute : PropertyAttribute
namespace MyBox.Internal
{
[CustomEditor(typeof(MonoBehaviour), true)]
public class ButtonMethodAttributeEditor : Editor
public class ButtonMethodMonoBehaviourEditor : Editor
{
private List<MethodInfo> _methods;
private MonoBehaviour _target;
Expand All @@ -36,37 +36,48 @@ private void OnEnable()
_target = target as MonoBehaviour;
if (_target == null) return;

CollectValidMembers(_target.GetType());
_methods = ButtonMethodHandler.CollectValidMembers(_target.GetType());
}

public override void OnInspectorGUI()
{
base.OnInspectorGUI();

if (_methods == null) return;

EditorGUILayout.Space();

foreach (MethodInfo method in _methods)
{
if (GUILayout.Button(method.Name.SplitCamelCase())) InvokeMethod(method);
}
ButtonMethodHandler.OnInspectorGUI(_target, _methods);
}
}


private void InvokeMethod(MethodInfo method)
[CustomEditor(typeof(ScriptableObject), true)]
public class ButtonMethodScriptableObjectEditor : Editor
{
private List<MethodInfo> _methods;
private ScriptableObject _target;

private void OnEnable()
{
var result = method.Invoke(target, null);
_target = target as ScriptableObject;
if (_target == null) return;

if (result != null)
{
var message = string.Format("{0} \nResult of Method '{1}' invocation on object {2}", result, method.Name, _target.name);
Debug.Log(message, _target);
}
_methods = ButtonMethodHandler.CollectValidMembers(_target.GetType());
}

private void CollectValidMembers(Type type)
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (_methods == null) return;

ButtonMethodHandler.OnInspectorGUI(_target, _methods);
}
}

public static class ButtonMethodHandler
{
public static List<MethodInfo> CollectValidMembers(Type type)
{
List<MethodInfo> methods = null;

var members = type.GetMembers(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
.Where(IsButtonMethod);

Expand All @@ -75,13 +86,36 @@ private void CollectValidMembers(Type type)
var method = member as MethodInfo;
if (IsValidMember(method, member))
{
if (_methods == null) _methods = new List<MethodInfo>();
_methods.Add(method);
if (methods == null) methods = new List<MethodInfo>();
methods.Add(method);
}
}

return methods;
}

public static void OnInspectorGUI(UnityEngine.Object target, List<MethodInfo> methods)
{
EditorGUILayout.Space();

foreach (MethodInfo method in methods)
{
if (GUILayout.Button(method.Name.SplitCamelCase())) InvokeMethod(target, method);
}
}

private static void InvokeMethod(UnityEngine.Object target, MethodInfo method)
{
var result = method.Invoke(target, null);

if (result != null)
{
var message = string.Format("{0} \nResult of Method '{1}' invocation on object {2}", result, method.Name, target.name);
Debug.Log(message, target);
}
}

private bool IsValidMember(MethodInfo method, MemberInfo member)
private static bool IsValidMember(MethodInfo method, MemberInfo member)
{
if (method == null)
{
Expand All @@ -102,7 +136,7 @@ private bool IsValidMember(MethodInfo method, MemberInfo member)
return true;
}

private bool IsButtonMethod(MemberInfo memberInfo)
private static bool IsButtonMethod(MemberInfo memberInfo)
{
return Attribute.IsDefined(memberInfo, typeof(ButtonMethodAttribute));
}
Expand Down

0 comments on commit ce038dc

Please sign in to comment.