Skip to content

Commit 70f581b

Browse files
Add method button attribute.
1 parent 76accfa commit 70f581b

19 files changed

+644
-0
lines changed

Assets/CustomMethodButton.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/CustomMethodButton/Attributes.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using UnityEngine;
2+
3+
/// <summary>
4+
/// Usage:
5+
/// <para> #if UNITY_EDITOR</para>
6+
/// <para> [MethodButton("MethodName1", "MethodName2", "MethodNameN")] // Must match a public method name!</para>
7+
/// <para> [SerializeField] private bool showButtons; // this bool is mandatory!</para>
8+
/// <para> #endif</para>
9+
/// </summary>
10+
public class MethodButtonAttribute : PropertyAttribute
11+
{
12+
/// <summary>
13+
/// Array of different method names for which a button will be displayed.
14+
/// <para>"MethodOne", MethodTwo" ... "MethodN"</para>
15+
/// </summary>
16+
public string[] MethodNames { get; set; }
17+
18+
public MethodButtonAttribute(params string[] args)
19+
{
20+
MethodNames = args;
21+
}
22+
}

Assets/CustomMethodButton/Attributes/MethodButtonAttribute.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/CustomMethodButton/Drawers.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using UnityEngine;
2+
using UnityEditor;
3+
4+
[CustomPropertyDrawer(typeof(MethodButtonAttribute))]
5+
public class MethodButtonAttributeDrawer : PropertyDrawer
6+
{
7+
private int i;
8+
private float buttonHeight = EditorGUIUtility.singleLineHeight;
9+
private MethodButtonAttribute attr;
10+
11+
public override void OnGUI(Rect position, SerializedProperty editorFoldout, GUIContent label)
12+
{
13+
if (editorFoldout.name.Equals("editorFoldout") == false)
14+
{
15+
LogErrorMessage(editorFoldout);
16+
return;
17+
}
18+
19+
i = 0;
20+
Rect foldoutRect = new Rect(position.x, position.y, position.width, buttonHeight);
21+
editorFoldout.boolValue = EditorGUI.Foldout(foldoutRect, editorFoldout.boolValue, "Buttons", true);
22+
if (editorFoldout.boolValue)
23+
{
24+
i++;
25+
attr = (MethodButtonAttribute)base.attribute;
26+
foreach (var name in attr.MethodNames)
27+
{
28+
i++;
29+
Rect buttonRect = new Rect(position.x, position.y + (buttonHeight * i), position.width, buttonHeight);
30+
if (GUI.Button(buttonRect, name))
31+
{
32+
InvokeMethod(editorFoldout, name);
33+
}
34+
}
35+
}
36+
}
37+
38+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
39+
{
40+
return EditorGUI.GetPropertyHeight(property, label, true) + buttonHeight * i + 2;
41+
}
42+
43+
private void InvokeMethod(SerializedProperty property, string name)
44+
{
45+
Object target = property.serializedObject.targetObject;
46+
target.GetType().GetMethod(name).Invoke(target, null);
47+
}
48+
49+
private void LogErrorMessage(SerializedProperty editorFoldout)
50+
{
51+
Debug.LogError("<color=red><b>Possible improper usage of method button attribute!</b></color>");
52+
#if NET_4_6
53+
Debug.LogError($"Got field name: <b>{editorFoldout.name}</b>, Expected: <b>editorFoldout</b>");
54+
Debug.LogError($"Please see <b>{"Usage"}</b> at <b><i><color=blue>{"https://github/address"}</color></i></b>");
55+
#else
56+
Debug.LogError(string.Format("Got field name: <b>{0}</b>, Expected: <b>editorFoldout</b>", editorFoldout.name));
57+
Debug.LogError("Please see <b>\"Usage\"</b> at <b><i><color=blue>\"https://github/address\"</color></i></b>");
58+
#endif
59+
}
60+
}

Assets/CustomMethodButton/Drawers/MethodButtonAttributeDrawer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Demo.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Demo/Scenes.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)