Skip to content

Instantly share code, notes, and snippets.

@bcatcho
Last active October 9, 2022 09:15
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bcatcho/8889240ba154d99169fd to your computer and use it in GitHub Desktop.
Save bcatcho/8889240ba154d99169fd to your computer and use it in GitHub Desktop.
// The MIT License (MIT) - https://gist.github.com/bcatcho/1926794b7fd6491159e7
// Copyright (c) 2015 Brandon Catcho
using System;
// Place this file in any folder that is or is a descendant of a folder named "Scripts"
namespace CatchCo
{
// Restrict to methods only
[AttributeUsage(AttributeTargets.Method)]
public class ExposeMethodInEditorAttribute : Attribute
{
}
}
// The MIT License (MIT) - https://gist.github.com/bcatcho/1926794b7fd6491159e7
// Copyright (c) 2015 Brandon Catcho
using UnityEngine;
using UnityEditor;
using System.Reflection;
// Place this file in any folder that is or is a descendant of a folder named "Editor"
namespace CatchCo
{
[CanEditMultipleObjects] // Don't ruin everyone's day
[CustomEditor(typeof(MonoBehaviour), true)] // Target all MonoBehaviours and descendants
public class MonoBehaviourCustomEditor : UnityEditor.Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector(); // Draw the normal inspector
// Currently this will only work in the Play mode. You'll see why
if (Application.isPlaying)
{
// Get the type descriptor for the MonoBehaviour we are drawing
var type = target.GetType();
// Iterate over each private or public instance method (no static methods atm)
foreach (var method in type.GetMethods(BindingFlags.NonPublic|BindingFlags.Public|BindingFlags.Instance))
{
// make sure it is decorated by our custom attribute
var attributes = method.GetCustomAttributes(typeof(ExposeMethodInEditorAttribute), true);
if (attributes.Length > 0)
{
if (GUILayout.Button("Run: " + method.Name))
{
// If the user clicks the button, invoke the method immediately.
// There are many ways to do this but I chose to use Invoke which only works in Play Mode.
((MonoBehaviour)target).Invoke(method.Name, 0f);
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment