Advertisement
Guest User

Untitled

a guest
May 26th, 2020
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.19 KB | None | 0 0
  1. public class MethodButtonInvokerDrawer : PropertyDrawer
  2.     {
  3.         bool _shouldDraw = false;
  4.        
  5.         public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  6.         {
  7.             if (property.propertyType != SerializedPropertyType.Boolean)
  8.             {
  9.                 EditorGUILayout.HelpBox("The <color=blue>MethodButtonInvoker</color> attribute must be assigned to a boolean value!", MessageType.Warning);
  10.                 return;
  11.             }
  12.  
  13.             EditorGUI.PropertyField(position, property, label);
  14.  
  15.             if (Event.current.type == EventType.Layout)
  16.             {
  17.                 _shouldDraw = property.boolValue;
  18.             }
  19.            
  20.             if (!_shouldDraw) return;
  21.  
  22.             DrawButtons(property);
  23.         }
  24.  
  25.         static void DrawButtons(SerializedProperty property)
  26.         {
  27.             var __owner = property.serializedObject.targetObject;
  28.  
  29.             var __methods = __owner.GetType()
  30.                 .GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic |
  31.                             BindingFlags.Instance);
  32.  
  33.             for (var __i = 0; __i < __methods.Length; __i++)
  34.             {
  35.                 var __method = __methods[__i];
  36.                 var __attribute = __method.GetAttribute<MethodButtonAttribute>();
  37.                 if (__attribute == null) continue;
  38.  
  39.                 var __canPress = true;
  40.                
  41.                 switch (__attribute.invokeType)
  42.                 {
  43.                     case InvokeTypes.EditorOnly:
  44.                         __canPress = !Application.isPlaying;
  45.                         break;
  46.                     case InvokeTypes.PlayMode:
  47.                         __canPress = Application.isPlaying;
  48.                         break;
  49.                     default:
  50.                         break;
  51.                 }
  52.  
  53.                 GUI.enabled = __canPress;
  54.                
  55.                 if (GUILayout.Button(__method.Name))
  56.                 {
  57.                     __method.Invoke(__owner, new object[] { });
  58.                 }
  59.  
  60.                 if(!__canPress)
  61.                     GUI.enabled = true;
  62.             }
  63.         }
  64.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement