Last active
December 22, 2015 21:48
-
-
Save MattRix/a2528bb81cf6fc8806bf to your computer and use it in GitHub Desktop.
Easy inspectors on regular monobehaviours etc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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"