From 01c406f54e5395cc9e24ad47c6ee570e11557288 Mon Sep 17 00:00:00 2001 From: "Nuno \"Omega Leo\" Diogo" Date: Sat, 11 May 2024 11:21:40 +0100 Subject: [PATCH] Changes to handle multiple MonoBehaviourCustomEditors --- Editor/Editors/MonoBehaviourCustomEditor.cs | 53 +++++++++++---------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/Editor/Editors/MonoBehaviourCustomEditor.cs b/Editor/Editors/MonoBehaviourCustomEditor.cs index 35bf22d..afa35d4 100644 --- a/Editor/Editors/MonoBehaviourCustomEditor.cs +++ b/Editor/Editors/MonoBehaviourCustomEditor.cs @@ -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); + } } } } } -} } \ No newline at end of file