From 2af88c3147f773372ca49f8e6fc06b03ab0af6fe Mon Sep 17 00:00:00 2001 From: oldpeper <96619050+oldpeper@users.noreply.github.com> Date: Tue, 22 Oct 2024 11:30:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8C=89=E9=94=AE=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- d2/Assets/Script/Tool/InspectorButton.cs | 63 +++++++++++++++++++ d2/Assets/Script/Tool/InspectorButton.cs.meta | 11 ++++ .../Script/Tool/InspectorButtonAttribute.cs | 16 +++++ .../Tool/InspectorButtonAttribute.cs.meta | 11 ++++ 4 files changed, 101 insertions(+) create mode 100644 d2/Assets/Script/Tool/InspectorButton.cs create mode 100644 d2/Assets/Script/Tool/InspectorButton.cs.meta create mode 100644 d2/Assets/Script/Tool/InspectorButtonAttribute.cs create mode 100644 d2/Assets/Script/Tool/InspectorButtonAttribute.cs.meta diff --git a/d2/Assets/Script/Tool/InspectorButton.cs b/d2/Assets/Script/Tool/InspectorButton.cs new file mode 100644 index 00000000..a0674457 --- /dev/null +++ b/d2/Assets/Script/Tool/InspectorButton.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections; +using System.Linq; +using System.Reflection; +using UnityEditor; +using UnityEngine; + +[CustomEditor(typeof(MonoBehaviour), true)] +[CanEditMultipleObjects] +public class InspectorButton : Editor +{ + public override void OnInspectorGUI() + { + base.OnInspectorGUI(); + + var mono = target as MonoBehaviour; + if (mono == null) + return; + + var methods = mono.GetType() + .GetMethods( + BindingFlags.Public | BindingFlags.NonPublic | + BindingFlags.Instance | BindingFlags.Static + ).Where(method => + Attribute.IsDefined(method, typeof(InspectorButtonAttribute)) + ).ToArray(); + + foreach (var method in methods) + { + var attr = method.GetCustomAttribute(); + DrawButton(method, attr.Name); + } + } + + private void DrawButton(MethodInfo methodInfo, string methodName) + { + if (string.IsNullOrEmpty(methodName)) + methodName = methodInfo.Name; + + EditorGUILayout.BeginHorizontal(); + + if (GUILayout.Button( + methodName, + GUILayout.ExpandWidth(true) + )) + { + foreach (var targetObj in targets) + { + var mono = targetObj as MonoBehaviour; + if (mono == null) + continue; + + var val = methodInfo.Invoke(mono, new object[] { }); + if (val is IEnumerator coroutine) + mono.StartCoroutine(coroutine); + else if (val != null) + Debug.Log($"{methodName}调用结果: {val}"); + } + } + + EditorGUILayout.EndHorizontal(); + } +} \ No newline at end of file diff --git a/d2/Assets/Script/Tool/InspectorButton.cs.meta b/d2/Assets/Script/Tool/InspectorButton.cs.meta new file mode 100644 index 00000000..05327c11 --- /dev/null +++ b/d2/Assets/Script/Tool/InspectorButton.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9ebbe62bb83f82141a34aeeef4052a8c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/d2/Assets/Script/Tool/InspectorButtonAttribute.cs b/d2/Assets/Script/Tool/InspectorButtonAttribute.cs new file mode 100644 index 00000000..9d2578c9 --- /dev/null +++ b/d2/Assets/Script/Tool/InspectorButtonAttribute.cs @@ -0,0 +1,16 @@ +using UnityEngine; + +[System.AttributeUsage(System.AttributeTargets.Method)] +public class InspectorButtonAttribute : PropertyAttribute +{ + public readonly string Name; + + public InspectorButtonAttribute() + { + } + + public InspectorButtonAttribute(string name) + { + Name = name; + } +} \ No newline at end of file diff --git a/d2/Assets/Script/Tool/InspectorButtonAttribute.cs.meta b/d2/Assets/Script/Tool/InspectorButtonAttribute.cs.meta new file mode 100644 index 00000000..7e7ad657 --- /dev/null +++ b/d2/Assets/Script/Tool/InspectorButtonAttribute.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 39d98e97145b3eb42ba141705d3b7ec9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: