Created
June 20, 2022 14:58
-
-
Save fangzhangmnm/d6a4f53bb2661b1bf8266ddfcf28dcc5 to your computer and use it in GitHub Desktop.
Revisions
-
fangzhangmnm created this gist
Jun 20, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,52 @@ //credits :https://github.com/madsbangh/EasyButtons/ using UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif using System; using System.Linq; using System.Reflection; [AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)] public sealed class ButtonAttribute : Attribute { } #if UNITY_EDITOR public static class EasyButtonsEditorExtensions { public static void DrawEasyButtons(this Editor editor) { var methods = editor.target.GetType() .GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) .Where(m => m.GetParameters().Length == 0); foreach (var method in methods) { var ba = (ButtonAttribute)Attribute.GetCustomAttribute(method, typeof(ButtonAttribute)); if (ba != null) { GUILayout.Space(10); var buttonName = ObjectNames.NicifyVariableName(method.Name); if (GUILayout.Button(buttonName)) { foreach (var t in editor.targets) { method.Invoke(t, null); } editor.Repaint(); } GUILayout.Space(10); } } } } [CanEditMultipleObjects] [CustomEditor(typeof(UnityEngine.Object), true)] public class ObjectEditor : Editor { public override void OnInspectorGUI() { this.DrawEasyButtons(); DrawDefaultInspector(); } } #endif