Made InspectorCallable able to call the functions
This commit is contained in:
@@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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; }
|
||||||
|
|||||||
@@ -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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user