Skip to content

Instantly share code, notes, and snippets.

@MattRix
Last active December 22, 2015 21:48
Show Gist options
  • Save MattRix/a2528bb81cf6fc8806bf to your computer and use it in GitHub Desktop.
Save MattRix/a2528bb81cf6fc8806bf to your computer and use it in GitHub Desktop.
Easy inspectors on regular monobehaviours etc
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEditor;
using System.Reflection;
[CustomEditor (typeof(UnityEngine.Object),true)]
[CanEditMultipleObjects]
public class ObjectInspector : Editor
{
public MethodInfo inspectMeth;
public MethodInfo sceneMeth;
public List<MethodInfo>buttonMethods = new List<MethodInfo>();
public void OnEnable()
{
var type = target.GetType();
inspectMeth = target.GetType().GetMethod("OnInspectorGUI",BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic);
sceneMeth = target.GetType().GetMethod("OnSceneGUI",BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic);
var meths = type.GetMethods();
foreach(var meth in meths)
{
if(meth.Name.ToLower().Contains("_button"))
{
buttonMethods.Add(meth);
}
}
}
override public void OnInspectorGUI()
{
DrawDefaultInspector();
if(inspectMeth != null)
{
foreach(var eachTarget in targets)
{
inspectMeth.Invoke(eachTarget, new object[0]);
}
}
foreach(var meth in buttonMethods)
{
string methName = meth.Name.Substring(0,meth.Name.ToLower().IndexOf("_button"));
if(GUILayout.Button(ObjectNames.NicifyVariableName(methName)))
{
foreach(var eachTarget in targets)
{
meth.Invoke(eachTarget, new object[0]);
}
}
}
}
public void OnSceneGUI()
{
if(sceneMeth != null)
{
sceneMeth.Invoke(target, new object[0]);
}
}
}
@MattRix
Copy link
Author

MattRix commented Dec 22, 2015

Just updated it to no longer use an attribute, instead you just name the method "DoSomeStuff_Button" and it'll make a button with "Do Some Stuff"

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