Skip to content

Commit

Permalink
Changes to handle multiple MonoBehaviourCustomEditors
Browse files Browse the repository at this point in the history
  • Loading branch information
omegaleo committed May 11, 2024
1 parent d87b749 commit 01c406f
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions Editor/Editors/MonoBehaviourCustomEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,46 @@
using UnityEngine.Tilemaps;
using Object = UnityEngine.Object;

namespace OmegaLeo.Toolbox.Editor.Omega_Leo_Toolbox.Editor.Editors
namespace pt.omegaleo.utils.Editor.Editors
{
[CustomEditor(typeof(MonoBehaviour), true), CanEditMultipleObjects] // Target all MonoBehaviours and descendants
public class MonoBehaviourCustomEditor : UnityEditor.Editor
{
public override void OnInspectorGUI()
public class MonoBehaviourCustomEditor : UnityEditor.Editor
{
DrawDefaultInspector(); // Draw the normal inspector
public override void OnInspectorGUI()
{
DrawDefaultInspector(); // Draw the normal inspector
CustomEvents();
}

// Get the type descriptor for the MonoBehaviour we are drawing
var type = target.GetType();
protected virtual void CustomEvents()
{
// Get the type descriptor for the MonoBehaviour we are drawing
var type = target.GetType();

HandleMethods(type);
}
HandleMethods(type);
}

private void HandleMethods(Type type)
{
// Iterate over each private or public instance method (no static methods atm)
foreach (var method in type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance |
BindingFlags.FlattenHierarchy))
private void HandleMethods(Type type)
{
// make sure it is decorated by our custom attribute
var attributes = method.GetCustomAttributes(typeof(MethodButtonAttribute), true);
if (attributes.Length > 0)
// Iterate over each private or public instance method (no static methods atm)
foreach (var method in type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.FlattenHierarchy))
{
var attribute = attributes.Select(x => x as MethodButtonAttribute)
.FirstOrDefault(x => x.methodName == method.Name);
if (GUILayout.Button(attribute.buttonName))
// make sure it is decorated by our custom attribute
var attributes = method.GetCustomAttributes(typeof(MethodButtonAttribute), true);
if (attributes.Length > 0)
{
// If the user clicks the button, invoke the method immediately.
// There are many ways to do this but I chose to use Invoke which only works in Play Mode.
method.Invoke(target, null);
var attribute = attributes.Select(x => x as MethodButtonAttribute)
.FirstOrDefault(x => x.methodName == method.Name);
if (GUILayout.Button(attribute.buttonName))
{
// If the user clicks the button, invoke the method immediately.
// There are many ways to do this but I chose to use Invoke which only works in Play Mode.
method.Invoke(target, null);
}
}
}
}
}
}
}

0 comments on commit 01c406f

Please sign in to comment.