Guest User

Unity3d EditorButton

a guest
Dec 5th, 2013
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  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. /// This attribute can only be applied to fields because its
  15. /// associated PropertyDrawer only operates on fields (either
  16. /// public or tagged with the [SerializeField] attribute) in
  17. /// the target MonoBehaviour.
  18. /// </summary>
  19. [System.AttributeUsage(System.AttributeTargets.Method)]
  20. public class EditorButtonAttribute : PropertyAttribute
  21. {
  22. }
  23.  
  24. #if UNITY_EDITOR
  25. [CustomEditor(typeof (MonoBehaviour), true)]
  26. public class EditorButton : Editor
  27. {
  28.     public override void OnInspectorGUI()
  29.     {
  30.         base.OnInspectorGUI();
  31.  
  32.         var mono = target as MonoBehaviour;
  33.  
  34.         var methods = mono.GetType()
  35.             .GetMembers(BindingFlags.Instance | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public |
  36.                         BindingFlags.NonPublic)
  37.             .Where(o => Attribute.IsDefined(o, typeof (EditorButtonAttribute)));
  38.  
  39.         foreach (var memberInfo in methods)
  40.         {
  41.             if (GUILayout.Button(memberInfo.Name))
  42.             {
  43.                 var method = memberInfo as MethodInfo;
  44.                 method.Invoke(mono, null);
  45.             }
  46.         }
  47.     }
  48. }
  49. #endif
Add Comment
Please, Sign In to add comment