Started adding inspector callable

This commit is contained in:
2026-01-24 01:19:18 +00:00
parent 36052b32ca
commit 4284fd60a2
6 changed files with 87 additions and 4 deletions

View File

@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using PashaBibko.Pacore.Shared.Attributes;
using UnityEditor;
using UnityEngine;
namespace PashaBibko.Pacore.Editor.Drawers
{
[CustomEditor(typeof(MonoBehaviour), editorForChildClasses: true)]
public class MonoBehaviourDrawer : UnityEditor.Editor
{
private const BindingFlags BINDING_FLAGS =
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance;
private InspectorCallableAttribute[] GetTypeButtons()
{
Type type = target.GetType();
MethodInfo[] methods = type.GetMethods(BINDING_FLAGS);
List<InspectorCallableAttribute> buttons = new();
foreach (MethodInfo method in methods)
{
InspectorCallableAttribute attribute = method.GetCustomAttribute<InspectorCallableAttribute>();
if (attribute != null)
{
buttons.Add(attribute);
}
}
return buttons.ToArray();
}
public override void OnInspectorGUI()
{
DrawDefaultInspector();
InspectorCallableAttribute[] buttons = GetTypeButtons();
if (buttons.Length == 0)
{
return;
}
EditorGUILayout.Space();
EditorGUILayout.LabelField("Functions", EditorStyles.boldLabel);
foreach (InspectorCallableAttribute button in buttons)
{
GUILayout.Button(button.ButtonName);
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: eab6c2c196ec47a9ae8eb477a6ceb1a7
timeCreated: 1769195504

View File

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

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f9d7cf5ef25e483bb54ea9e2c1f60b57
timeCreated: 1769195867

View File

@@ -133,8 +133,8 @@ GameObject:
- component: {fileID: 330585546} - component: {fileID: 330585546}
- component: {fileID: 330585545} - component: {fileID: 330585545}
- component: {fileID: 330585544} - component: {fileID: 330585544}
- component: {fileID: 330585547}
- component: {fileID: 330585548} - component: {fileID: 330585548}
- component: {fileID: 330585547}
m_Layer: 0 m_Layer: 0
m_Name: Main Camera m_Name: Main Camera
m_TagString: MainCamera m_TagString: MainCamera
@@ -273,7 +273,7 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
go: {fileID: 0} go: {fileID: 0}
Test: 2147483647 Test: 0
--- !u!1 &410087039 --- !u!1 &410087039
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@@ -8,8 +8,14 @@ public class TestMonoBehaviour : MonoBehaviour
[DetectInspectorChanges("OnTestChange")] [DetectInspectorChanges("OnTestChange")]
public int Test; public int Test;
private void OnTestChange() private void OnTestChange() => LogTestValue();
[InspectorCallable("Test button")] public void LogTestValue()
{
Debug.Log($"Test value [{Test}]");
}
[InspectorCallable("Other Test button")] public void DontLogTestValue()
{ {
Debug.Log($"New value: {Test}");
} }
} }