Skip to content

Instantly share code, notes, and snippets.

@IvanMurzak
Last active October 2, 2017 07:14
Show Gist options
  • Save IvanMurzak/084f553d6813277d486f8df9e9b21449 to your computer and use it in GitHub Desktop.
Save IvanMurzak/084f553d6813277d486f8df9e9b21449 to your computer and use it in GitHub Desktop.
Files in "Editor" directory: - MonoBehaviourCustomEditor.cs
using System;
[AttributeUsage(AttributeTargets.Method)]
public class ExposeMethodInUnityEditorAttribute : Attribute
{
}
using System;
[AttributeUsage(AttributeTargets.Method)]
public class ExposeMethodInUnityEditorPlayingOnlyAttribute : Attribute
{
}
using UnityEngine;
using UnityEditor;
using System.Reflection;
using System;
namespace Project.Template.Editor
{
[CanEditMultipleObjects]
[CustomEditor(typeof(MonoBehaviour), true)]
public class MonoBehaviourCustomEditor : UnityEditor.Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
var type = target.GetType();
foreach (var method in type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
{
var editorAttributes = method.GetCustomAttributes(typeof(ExposeMethodInUnityEditorAttribute), true);
if (editorAttributes.Length > 0)
{
DrawButton(method.Name, type, target);
}
else if (Application.isPlaying)
{
var editorPlayingOnlyAttributes = method.GetCustomAttributes(typeof(ExposeMethodInUnityEditorPlayingOnlyAttribute), true);
if (editorPlayingOnlyAttributes.Length > 0)
{
DrawButton(method.Name, type, target);
}
}
}
}
private static void DrawButton(string methodName, Type type, object target)
{
if (GUILayout.Button(methodName))
{
type.GetMethod(methodName).Invoke(target, null);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment