From 4284fd60a28f608ac69b8695035be8d5b5ce5b2f Mon Sep 17 00:00:00 2001 From: Pasha Date: Sat, 24 Jan 2026 01:19:18 +0000 Subject: [PATCH] Started adding inspector callable --- .../Editor/Drawers/MonoBehaviourDrawer.cs | 55 +++++++++++++++++++ .../Drawers/MonoBehaviourDrawer.cs.meta | 3 + .../Shared/Attributes/InspectorCallable.cs | 16 ++++++ .../Attributes/InspectorCallable.cs.meta | 3 + Assets/Scenes/SampleScene.unity | 4 +- Assets/TestMonoBehaviour.cs | 10 +++- 6 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs create mode 100644 Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs.meta create mode 100644 Assets/Pacore/Shared/Attributes/InspectorCallable.cs create mode 100644 Assets/Pacore/Shared/Attributes/InspectorCallable.cs.meta diff --git a/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs b/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs new file mode 100644 index 0000000..403043d --- /dev/null +++ b/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs @@ -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 buttons = new(); + + foreach (MethodInfo method in methods) + { + InspectorCallableAttribute attribute = method.GetCustomAttribute(); + 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); + } + } + } +} \ No newline at end of file diff --git a/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs.meta b/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs.meta new file mode 100644 index 0000000..eb6fbc5 --- /dev/null +++ b/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: eab6c2c196ec47a9ae8eb477a6ceb1a7 +timeCreated: 1769195504 \ No newline at end of file diff --git a/Assets/Pacore/Shared/Attributes/InspectorCallable.cs b/Assets/Pacore/Shared/Attributes/InspectorCallable.cs new file mode 100644 index 0000000..ed6e58e --- /dev/null +++ b/Assets/Pacore/Shared/Attributes/InspectorCallable.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/Assets/Pacore/Shared/Attributes/InspectorCallable.cs.meta b/Assets/Pacore/Shared/Attributes/InspectorCallable.cs.meta new file mode 100644 index 0000000..9c353aa --- /dev/null +++ b/Assets/Pacore/Shared/Attributes/InspectorCallable.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f9d7cf5ef25e483bb54ea9e2c1f60b57 +timeCreated: 1769195867 \ No newline at end of file diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 665da31..6a2cce6 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -133,8 +133,8 @@ GameObject: - component: {fileID: 330585546} - component: {fileID: 330585545} - component: {fileID: 330585544} - - component: {fileID: 330585547} - component: {fileID: 330585548} + - component: {fileID: 330585547} m_Layer: 0 m_Name: Main Camera m_TagString: MainCamera @@ -273,7 +273,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: go: {fileID: 0} - Test: 2147483647 + Test: 0 --- !u!1 &410087039 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/TestMonoBehaviour.cs b/Assets/TestMonoBehaviour.cs index ab58d0f..a7c2b22 100644 --- a/Assets/TestMonoBehaviour.cs +++ b/Assets/TestMonoBehaviour.cs @@ -8,8 +8,14 @@ public class TestMonoBehaviour : MonoBehaviour [DetectInspectorChanges("OnTestChange")] 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}"); } }