Started adding inspector callable
This commit is contained in:
55
Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs
Normal file
55
Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs.meta
Normal file
3
Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: eab6c2c196ec47a9ae8eb477a6ceb1a7
|
||||
timeCreated: 1769195504
|
||||
16
Assets/Pacore/Shared/Attributes/InspectorCallable.cs
Normal file
16
Assets/Pacore/Shared/Attributes/InspectorCallable.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f9d7cf5ef25e483bb54ea9e2c1f60b57
|
||||
timeCreated: 1769195867
|
||||
@@ -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
|
||||
|
||||
@@ -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}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user