Skip to content

Instantly share code, notes, and snippets.

@mashlol
Created August 1, 2024 01:05
Show Gist options
  • Save mashlol/92e66afb616ee2f1c66158cf0c2c8280 to your computer and use it in GitHub Desktop.
Save mashlol/92e66afb616ee2f1c66158cf0c2c8280 to your computer and use it in GitHub Desktop.
Super Simple Unity Editor Button
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
[AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class ButtonAttribute : Attribute {}
[CustomEditor(typeof(UnityEngine.Object), true)]
[CanEditMultipleObjects]
public class BetterObjectEditor : Editor {
public override void OnInspectorGUI() {
DrawDefaultInspector();
var methods = target.GetType().GetMethods(
BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.Public |
BindingFlags.NonPublic
);
foreach (MethodInfo method in methods) {
if (method.GetCustomAttribute<ButtonAttribute>() == null)
continue;
if (GUILayout.Button(method.Name))
method.Invoke(target, new object[0]);
}
}
}
@mashlol
Copy link
Author

mashlol commented Aug 1, 2024

Usage:

public class MyBehaviour : MonoBehaviour {
  
  [Button]
  public void MyMethod() {
    Debug.Log("Ran MyMethod");
  }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment