@@ -9,6 +9,30 @@ namespace Toolbox.Editor.Drawers
99{
1010 public class EditorButtonAttributeDrawer : ToolboxDecoratorDrawer < EditorButtonAttribute >
1111 {
12+ private bool IsCoroutine ( MethodInfo method )
13+ {
14+ return method . ReturnType == typeof ( IEnumerator ) ;
15+ }
16+
17+ private MethodInfo GetMethod ( EditorButtonAttribute attribute , Object [ ] targetObjects , string methodName )
18+ {
19+ var methodInfo = ReflectionUtility . GetObjectMethod ( methodName , targetObjects ) ;
20+ if ( methodInfo == null )
21+ {
22+ ToolboxEditorLog . AttributeUsageWarning ( attribute , string . Format ( "{0} method not found." , methodName ) ) ;
23+ return null ;
24+ }
25+
26+ var parameters = methodInfo . GetParameters ( ) ;
27+ if ( parameters . Length > 0 )
28+ {
29+ ToolboxEditorLog . AttributeUsageWarning ( attribute , string . Format ( "{0} method has to be parameterless." , methodName ) ) ;
30+ return null ;
31+ }
32+
33+ return methodInfo ;
34+ }
35+
1236 private bool IsClickable ( ButtonActivityType activityType )
1337 {
1438 switch ( activityType )
@@ -26,9 +50,48 @@ private bool IsClickable(ButtonActivityType activityType)
2650 return true ;
2751 }
2852
29- private bool IsCoroutine ( MethodInfo method )
53+ private bool IsClickable ( EditorButtonAttribute attribute , Object [ ] targetObjects )
3054 {
31- return method . ReturnType == typeof ( IEnumerator ) ;
55+ if ( ! IsClickable ( attribute . ActivityType ) )
56+ {
57+ return false ;
58+ }
59+
60+ var validateMethodName = attribute . ValidateMethodName ;
61+ if ( string . IsNullOrEmpty ( validateMethodName ) )
62+ {
63+ return true ;
64+ }
65+
66+ var validateMethodInfo = GetMethod ( attribute , targetObjects , validateMethodName ) ;
67+ if ( validateMethodInfo == null )
68+ {
69+ return true ;
70+ }
71+
72+ var returnType = validateMethodInfo . ReturnType ;
73+ if ( returnType != typeof ( bool ) )
74+ {
75+ ToolboxEditorLog . AttributeUsageWarning ( attribute , "Validation method is invalid, return type has to be 'bool'." ) ;
76+ return true ;
77+ }
78+
79+ for ( var i = 0 ; i < targetObjects . Length ; i ++ )
80+ {
81+ var target = targetObjects [ i ] ;
82+ if ( target == null )
83+ {
84+ continue ;
85+ }
86+
87+ var result = ( bool ) validateMethodInfo . Invoke ( target , null ) ;
88+ if ( ! result )
89+ {
90+ return false ;
91+ }
92+ }
93+
94+ return true ;
3295 }
3396
3497 private void CallMethods ( EditorButtonAttribute attribute , Object [ ] targetObjects )
@@ -78,7 +141,7 @@ protected override void OnGuiCloseSafe(EditorButtonAttribute attribute)
78141 return ;
79142 }
80143
81- var disable = ! IsClickable ( attribute . ActivityType ) ;
144+ var disable = ! IsClickable ( attribute , targetObjects ) ;
82145 using ( new EditorGUI . DisabledScope ( disable ) )
83146 {
84147 var label = string . IsNullOrEmpty ( attribute . ExtraLabel )
0 commit comments