Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MethodButtonInvokerDrawer : PropertyDrawer
- {
- bool _shouldDraw = false;
- public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
- {
- if (property.propertyType != SerializedPropertyType.Boolean)
- {
- EditorGUILayout.HelpBox("The <color=blue>MethodButtonInvoker</color> attribute must be assigned to a boolean value!", MessageType.Warning);
- return;
- }
- EditorGUI.PropertyField(position, property, label);
- if (Event.current.type == EventType.Layout)
- {
- _shouldDraw = property.boolValue;
- }
- if (!_shouldDraw) return;
- DrawButtons(property);
- }
- static void DrawButtons(SerializedProperty property)
- {
- var __owner = property.serializedObject.targetObject;
- var __methods = __owner.GetType()
- .GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic |
- BindingFlags.Instance);
- for (var __i = 0; __i < __methods.Length; __i++)
- {
- var __method = __methods[__i];
- var __attribute = __method.GetAttribute<MethodButtonAttribute>();
- if (__attribute == null) continue;
- var __canPress = true;
- switch (__attribute.invokeType)
- {
- case InvokeTypes.EditorOnly:
- __canPress = !Application.isPlaying;
- break;
- case InvokeTypes.PlayMode:
- __canPress = Application.isPlaying;
- break;
- default:
- break;
- }
- GUI.enabled = __canPress;
- if (GUILayout.Button(__method.Name))
- {
- __method.Invoke(__owner, new object[] { });
- }
- if(!__canPress)
- GUI.enabled = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement