Advertisement
Guest User

Untitled

a guest
Dec 5th, 2013
4,960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Initial Concept by http://www.reddit.com/user/zaikman
  2. // Revised by http://www.reddit.com/user/quarkism
  3.  
  4. using System;
  5. using System.Linq;
  6. using Mvvm.Extensions;
  7. using UnityEngine;
  8. #if UNITY_EDITOR
  9. using UnityEditor;
  10. #endif
  11. using System.Reflection;
  12.  
  13. /// <summary>
  14. /// Stick this on a method
  15. /// </summary>
  16. [System.AttributeUsage(System.AttributeTargets.Method)]
  17. public class EditorButtonAttribute : PropertyAttribute
  18. {
  19. }
  20.  
  21. #if UNITY_EDITOR
  22. [CustomEditor(typeof (MonoBehaviour), true)]
  23. public class EditorButton : Editor
  24. {
  25.     public override void OnInspectorGUI()
  26.     {
  27.         base.OnInspectorGUI();
  28.  
  29.         var mono = target as MonoBehaviour;
  30.  
  31.         var methods = mono.GetType()
  32.             .GetMembers(BindingFlags.Instance | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public |
  33.                         BindingFlags.NonPublic)
  34.             .Where(o => Attribute.IsDefined(o, typeof (EditorButtonAttribute)));
  35.  
  36.         foreach (var memberInfo in methods)
  37.         {
  38.             if (GUILayout.Button(memberInfo.Name))
  39.             {
  40.                 var method = memberInfo as MethodInfo;
  41.                 method.Invoke(mono, null);
  42.             }
  43.         }
  44.     }
  45. }
  46. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement