Skip to content

Commit d321e22

Browse files
committed
Possibility to define custom validation method in the EditorButton attribute
1 parent 7684620 commit d321e22

3 files changed

Lines changed: 76 additions & 7 deletions

File tree

Assets/Editor Toolbox/Editor/Drawers/Toolbox/Decorator/EditorButtonAttributeDrawer.cs

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

Assets/Editor Toolbox/Runtime/Attributes/Toolbox/DecoratorAttributes/EditorButtonAttribute.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ public EditorButtonAttribute(string methodName, string extraLabel = null, Button
2020
}
2121

2222
public string MethodName { get; private set; }
23-
23+
/// <summary>
24+
/// If not <see langword="null"/> will be used to retrive validation method.
25+
/// </summary>
26+
public string ValidateMethodName { get; set; }
2427
public string ExtraLabel { get; private set; }
25-
2628
public string Tooltip { get; set; }
27-
2829
public ButtonActivityType ActivityType { get; private set; }
2930
}
3031

Assets/Examples/Scripts/SampleBehaviour4.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class SampleBehaviour4 : MonoBehaviour
1616

1717
[Label("Button", skinStyle: SkinStyle.Box)]
1818

19-
[EditorButton(nameof(TestMethod), Tooltip = "Custom Tooltip")]
19+
[EditorButton(nameof(TestMethod), Tooltip = "Custom Tooltip", ValidateMethodName = nameof(ValidationMethod))]
2020
[EditorButton(nameof(TestCoroutine), "<b>Test Coroutine</b>", activityType: ButtonActivityType.OnPlayMode)]
2121
[EditorButton(nameof(TestStaticMethod), activityType: ButtonActivityType.OnEditMode)]
2222
public int var1;
@@ -26,6 +26,11 @@ private void TestMethod()
2626
Debug.Log(nameof(TestMethod) + " is called");
2727
}
2828

29+
private bool ValidationMethod()
30+
{
31+
return var1 == 0;
32+
}
33+
2934
private IEnumerator TestCoroutine()
3035
{
3136
Debug.Log("Coroutine started");

0 commit comments

Comments
 (0)