From d58ca060f38eeff54dacd4cbde6a0fd9cfab3af3 Mon Sep 17 00:00:00 2001 From: Pasha Date: Sat, 24 Jan 2026 12:14:43 +0000 Subject: [PATCH] Made InspectorCallable able to call the functions --- .../Editor/Drawers/MonoBehaviourDrawer.cs | 44 +++++++++++++------ .../Shared/Attributes/InspectorCallable.cs | 8 ++-- Assets/TestMonoBehaviour.cs | 1 + 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs b/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs index f9e4417..c0dd846 100644 --- a/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs +++ b/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs @@ -4,41 +4,54 @@ using System.Reflection; using PashaBibko.Pacore.Shared.Attributes; using UnityEditor; using UnityEngine; +using Object = UnityEngine.Object; namespace PashaBibko.Pacore.Editor.Drawers { public static class InspectorCallableAttributeCache { + public struct AttributeInfo + { + public InspectorCallableAttribute Attribute; + public MethodInfo AttachedMethod; + } + private const BindingFlags BINDING_FLAGS = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; - private static Dictionary Cache { get; } = new(); + private static Dictionary Cache { get; } = new(); - public static InspectorCallableAttribute[] GetAllAttributes(Type type) + public static AttributeInfo[] GetAllAttributes(Type type) { /* Checks the cache for the type */ - if (Cache.TryGetValue(type, out InspectorCallableAttribute[] attributes)) + if (Cache.TryGetValue(type, out AttributeInfo[] attributes)) { return attributes; } /* Finds all the functions with the attribute */ MethodInfo[] methods = type.GetMethods(BINDING_FLAGS); - List buttons = new(); + List buttons = new(); foreach (MethodInfo method in methods) { InspectorCallableAttribute attribute = method.GetCustomAttribute(); if (attribute != null) { - buttons.Add(attribute); + AttributeInfo info = new() + { + Attribute = attribute, + AttachedMethod = method, + }; + + buttons.Add(info); } } /* Adds it to the cache before returning */ - InspectorCallableAttribute[] array = buttons.ToArray(); + AttributeInfo[] array = buttons.ToArray(); Cache.Add(type, array); return array; } @@ -50,14 +63,15 @@ namespace PashaBibko.Pacore.Editor.Drawers public override void OnInspectorGUI() { DrawDefaultInspector(); - - Type type = target.GetType(); - DrawFunctionButtons(type); + DrawFunctionButtons(target); } - public static void DrawFunctionButtons(Type type) + public static void DrawFunctionButtons(Object target) { - InspectorCallableAttribute[] buttons = InspectorCallableAttributeCache.GetAllAttributes(type); + Type type = target.GetType(); + InspectorCallableAttributeCache.AttributeInfo[] buttons + = InspectorCallableAttributeCache.GetAllAttributes(type); + if (buttons.Length == 0) { return; @@ -66,9 +80,13 @@ namespace PashaBibko.Pacore.Editor.Drawers EditorGUILayout.Space(); EditorGUILayout.LabelField("Functions", EditorStyles.boldLabel); - foreach (InspectorCallableAttribute button in buttons) + foreach (InspectorCallableAttributeCache.AttributeInfo button in buttons) { - GUILayout.Button(button.ButtonName); + string name = button.Attribute.ButtonName; + if (GUILayout.Button(name)) + { + button.AttachedMethod.Invoke(target, null); + } } } } diff --git a/Assets/Pacore/Shared/Attributes/InspectorCallable.cs b/Assets/Pacore/Shared/Attributes/InspectorCallable.cs index ed6e58e..c9239da 100644 --- a/Assets/Pacore/Shared/Attributes/InspectorCallable.cs +++ b/Assets/Pacore/Shared/Attributes/InspectorCallable.cs @@ -1,9 +1,9 @@ -using System; -using UnityEngine; +using JetBrains.Annotations; +using System; namespace PashaBibko.Pacore.Shared.Attributes { - [AttributeUsage(AttributeTargets.Method)] + [MeansImplicitUse, AttributeUsage(AttributeTargets.Method)] public class InspectorCallableAttribute : Attribute { public string ButtonName { get; } @@ -13,4 +13,4 @@ namespace PashaBibko.Pacore.Shared.Attributes ButtonName = name; } } -} \ No newline at end of file +} diff --git a/Assets/TestMonoBehaviour.cs b/Assets/TestMonoBehaviour.cs index a7c2b22..ba90daf 100644 --- a/Assets/TestMonoBehaviour.cs +++ b/Assets/TestMonoBehaviour.cs @@ -17,5 +17,6 @@ public class TestMonoBehaviour : MonoBehaviour [InspectorCallable("Other Test button")] public void DontLogTestValue() { + Debug.Log("Test"); } }