Snippets

Oleg Kuzyk Inject buttons for any MonoBehaviour inheritance to Inspector.

Created by Oleh Kuzyk last modified
using System;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;

public static class AttributeReader
{
    public static IEnumerable<MethodInfo> ReadMethods<T>(Type type) where T : Attribute
    {
        return type.GetMethods().Where(m => m.GetCustomAttributes(typeof(T), true).Count() > 0);
    }

    public static T GetMethodAttribute<T>(MethodInfo methodInfo) where T : Attribute
    {
        var list = methodInfo.GetCustomAttributes(true).Where(m => m.GetType() == typeof(T));
        int c = list.Count();
        var r = list.ElementAt(0);
        return c > 0 ? (T)r : null;
    }

}
using System;

[AttributeUsage(AttributeTargets.Method)]
public class ButtonAttribute : Attribute
{
    public string label = "";
    public int order = 100;

    public ButtonAttribute() { }
    public ButtonAttribute(string label)
    {
        this.label = label;
    }
    public ButtonAttribute(int order)
    {
        this.order = order;
    }
    public ButtonAttribute(string label, int order)
    {
        this.label = label;
        this.order = order;
    }
}
using UnityEngine;
using UnityEditor;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;

[CustomEditor(typeof(MonoBehaviour), true)]
public class ButtonInjection : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        DrawButtons();
    }

    private void DrawButtons()
    {
        // Read ButtonAttribute.
        var buttonMethods = AttributeReader.ReadMethods<ButtonAttribute>(target.GetType());
        int n = buttonMethods.Count();
        if (n > 0) {
            // Order it.
            buttonMethods = buttonMethods.OrderBy(m => {
                ButtonAttribute attr = AttributeReader.GetMethodAttribute<ButtonAttribute>(m);
                return attr.order;
            });

            // Draw.
            for (int i = 0; i < n; i++) {
                MethodInfo method = buttonMethods.ElementAt(i);
                ButtonAttribute attr = AttributeReader.GetMethodAttribute<ButtonAttribute>(method);

                string label = attr.label;
                if (string.IsNullOrEmpty(attr.label))
                    label = Regex.Replace(method.Name, @"((?<=\p{Ll})\p{Lu})|((?!\A)\p{Lu}(?>\p{Ll}))", " $0");

                if (GUILayout.Button(label)) {
                    method.Invoke(target, null);
                }
            }
        }
    }

}
using UnityEngine;

public class ButtonInjectionTest : MonoBehaviour
{
	[Button]
	public void Method1()
	{
		Debug.Log("Method1");
	}

	[Button]
	public void Method2()
	{
		Debug.Log("Method2");
	}

	[Button(label = "Custom Label")]
	public void Method3()
	{
		Debug.Log("Method3");
	}

	[Button(label = "Custom Label. Order 1", order = 1)]
	public void Method4()
	{
		Debug.Log("Method4");
	}
}

Comments (1)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.