From 5e44caca539a16aa1759c064b7d0b08365df280d Mon Sep 17 00:00:00 2001 From: Pasha Date: Sat, 24 Jan 2026 12:00:50 +0000 Subject: [PATCH] Added attribute cache --- .../Editor/Drawers/MonoBehaviourDrawer.cs | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs b/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs index 403043d..f9e4417 100644 --- a/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs +++ b/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs @@ -7,17 +7,24 @@ using UnityEngine; namespace PashaBibko.Pacore.Editor.Drawers { - [CustomEditor(typeof(MonoBehaviour), editorForChildClasses: true)] - public class MonoBehaviourDrawer : UnityEditor.Editor + public static class InspectorCallableAttributeCache { private const BindingFlags BINDING_FLAGS = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; - private InspectorCallableAttribute[] GetTypeButtons() + private static Dictionary Cache { get; } = new(); + + public static InspectorCallableAttribute[] GetAllAttributes(Type type) { - Type type = target.GetType(); + /* Checks the cache for the type */ + if (Cache.TryGetValue(type, out InspectorCallableAttribute[] attributes)) + { + return attributes; + } + + /* Finds all the functions with the attribute */ MethodInfo[] methods = type.GetMethods(BINDING_FLAGS); List buttons = new(); @@ -30,14 +37,27 @@ namespace PashaBibko.Pacore.Editor.Drawers } } - return buttons.ToArray(); + /* Adds it to the cache before returning */ + InspectorCallableAttribute[] array = buttons.ToArray(); + Cache.Add(type, array); + return array; } + } + [CustomEditor(typeof(MonoBehaviour), editorForChildClasses: true)] + public class MonoBehaviourDrawer : UnityEditor.Editor + { public override void OnInspectorGUI() { DrawDefaultInspector(); - - InspectorCallableAttribute[] buttons = GetTypeButtons(); + + Type type = target.GetType(); + DrawFunctionButtons(type); + } + + public static void DrawFunctionButtons(Type type) + { + InspectorCallableAttribute[] buttons = InspectorCallableAttributeCache.GetAllAttributes(type); if (buttons.Length == 0) { return;