Made InspectorCallable able to call the functions

This commit is contained in:
2026-01-24 12:14:43 +00:00
parent 5e44caca53
commit d58ca060f3
3 changed files with 36 additions and 17 deletions

View File

@@ -4,41 +4,54 @@ using System.Reflection;
using PashaBibko.Pacore.Shared.Attributes; using PashaBibko.Pacore.Shared.Attributes;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
using Object = UnityEngine.Object;
namespace PashaBibko.Pacore.Editor.Drawers namespace PashaBibko.Pacore.Editor.Drawers
{ {
public static class InspectorCallableAttributeCache public static class InspectorCallableAttributeCache
{ {
public struct AttributeInfo
{
public InspectorCallableAttribute Attribute;
public MethodInfo AttachedMethod;
}
private const BindingFlags BINDING_FLAGS = private const BindingFlags BINDING_FLAGS =
BindingFlags.Public | BindingFlags.Public |
BindingFlags.NonPublic | BindingFlags.NonPublic |
BindingFlags.Instance; BindingFlags.Instance;
private static Dictionary<Type, InspectorCallableAttribute[]> Cache { get; } = new(); private static Dictionary<Type, AttributeInfo[]> Cache { get; } = new();
public static InspectorCallableAttribute[] GetAllAttributes(Type type) public static AttributeInfo[] GetAllAttributes(Type type)
{ {
/* Checks the cache for the type */ /* Checks the cache for the type */
if (Cache.TryGetValue(type, out InspectorCallableAttribute[] attributes)) if (Cache.TryGetValue(type, out AttributeInfo[] attributes))
{ {
return attributes; return attributes;
} }
/* Finds all the functions with the attribute */ /* Finds all the functions with the attribute */
MethodInfo[] methods = type.GetMethods(BINDING_FLAGS); MethodInfo[] methods = type.GetMethods(BINDING_FLAGS);
List<InspectorCallableAttribute> buttons = new(); List<AttributeInfo> buttons = new();
foreach (MethodInfo method in methods) foreach (MethodInfo method in methods)
{ {
InspectorCallableAttribute attribute = method.GetCustomAttribute<InspectorCallableAttribute>(); InspectorCallableAttribute attribute = method.GetCustomAttribute<InspectorCallableAttribute>();
if (attribute != null) if (attribute != null)
{ {
buttons.Add(attribute); AttributeInfo info = new()
{
Attribute = attribute,
AttachedMethod = method,
};
buttons.Add(info);
} }
} }
/* Adds it to the cache before returning */ /* Adds it to the cache before returning */
InspectorCallableAttribute[] array = buttons.ToArray(); AttributeInfo[] array = buttons.ToArray();
Cache.Add(type, array); Cache.Add(type, array);
return array; return array;
} }
@@ -50,14 +63,15 @@ namespace PashaBibko.Pacore.Editor.Drawers
public override void OnInspectorGUI() public override void OnInspectorGUI()
{ {
DrawDefaultInspector(); DrawDefaultInspector();
DrawFunctionButtons(target);
Type type = target.GetType();
DrawFunctionButtons(type);
} }
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) if (buttons.Length == 0)
{ {
return; return;
@@ -66,9 +80,13 @@ namespace PashaBibko.Pacore.Editor.Drawers
EditorGUILayout.Space(); EditorGUILayout.Space();
EditorGUILayout.LabelField("Functions", EditorStyles.boldLabel); 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);
}
} }
} }
} }

View File

@@ -1,9 +1,9 @@
using System; using JetBrains.Annotations;
using UnityEngine; using System;
namespace PashaBibko.Pacore.Shared.Attributes namespace PashaBibko.Pacore.Shared.Attributes
{ {
[AttributeUsage(AttributeTargets.Method)] [MeansImplicitUse, AttributeUsage(AttributeTargets.Method)]
public class InspectorCallableAttribute : Attribute public class InspectorCallableAttribute : Attribute
{ {
public string ButtonName { get; } public string ButtonName { get; }
@@ -13,4 +13,4 @@ namespace PashaBibko.Pacore.Shared.Attributes
ButtonName = name; ButtonName = name;
} }
} }
} }

View File

@@ -17,5 +17,6 @@ public class TestMonoBehaviour : MonoBehaviour
[InspectorCallable("Other Test button")] public void DontLogTestValue() [InspectorCallable("Other Test button")] public void DontLogTestValue()
{ {
Debug.Log("Test");
} }
} }