Skip to content

Commit f7ffbb1

Browse files
Update ExampleClass and usage.
1 parent 4cf9ea1 commit f7ffbb1

File tree

4 files changed

+41
-19
lines changed

4 files changed

+41
-19
lines changed

Assets/Demo/Scenes/MethodButton.unity

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ MonoBehaviour:
140140
m_Script: {fileID: 11500000, guid: 0ca9cb6f83d1c1545ab5e8594ea133a5, type: 3}
141141
m_Name:
142142
m_EditorClassIdentifier:
143-
SomeInt: 0
144-
SomeBool: 0
145-
editorFoldout: 1
143+
ExampleInt: 0
144+
ExampleBool: 0
145+
editorFoldout: 0
146146
--- !u!4 &398710250
147147
Transform:
148148
m_ObjectHideFlags: 0

Assets/Demo/Scripts/ExampleClass.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,40 @@ public class ExampleClass : MonoBehaviour
55
public int ExampleInt;
66
public bool ExampleBool;
77

8-
public void DoAThing()
8+
internal void ExampleInternalMethod()
99
{
10-
Debug.Log("That thing be did.");
10+
Debug.Log("Invoking: ExampleClass.ExampleInternalMethod");
1111
}
12-
public void DoAnotherThing()
12+
public void ExamplePublicMethod()
1313
{
14-
Debug.Log("That other thing be did.");
14+
Debug.Log("Invoking: ExampleClass.ExamplePublicMethod");
1515
}
16-
private void DoAThirdThing()
16+
private void ExamplePrivateMethod()
1717
{
18-
Debug.Log("That third thing be did.");
18+
Debug.Log("Invoking: ExampleClass.ExamplePrivateMethod");
1919
}
20+
protected void ExampleProtectedMethod()
21+
{
22+
Debug.Log("Invoking: ExampleClass.ExampleProtectedMethod");
23+
}
24+
25+
26+
/* Add these four lines of code to your class.
27+
* Edit, add/remove, method names to the attribute.
28+
* Names must match the method name passed into the attribute.
29+
*
30+
* The bool "editorFoldout" is used not only as the property that Unity will use to find the attribute,
31+
* but also as the bool for the foldout in the editor.
32+
*
33+
* The #if UNITY_EDITOR preprocessor directive is so that this code is not compiled into the finished build.
34+
* So no need to remvoe it prior to building.
35+
*/
2036

2137
#if UNITY_EDITOR
22-
[MethodButton("DoAThing", "DoAnotherThing", "DoAThirdThing")]
38+
[MethodButton("ExampleInternalMethod",
39+
"ExamplePublicMethod",
40+
"ExamplePrivateMethod",
41+
"ExampleProtectedMethod")]
2342
[SerializeField] private bool editorFoldout;
2443
#endif
2544
}

Images/MethodButtonAttribute.gif

1.12 KB
Loading

README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@ public class ExampleClass : MonoBehaviour
1616
public int ExampleInt;
1717
public bool ExampleBool;
1818

19-
public void DoAThing()
19+
internal void ExampleInternalMethod()
2020
{
21-
Debug.Log("That thing be did.");
21+
Debug.Log("Invoking: ExampleClass.ExampleInternalMethod");
2222
}
23-
public void DoAnotherThing()
23+
public void ExamplePublicMethod()
2424
{
25-
Debug.Log("That other thing be did.");
25+
Debug.Log("Invoking: ExampleClass.ExamplePublicMethod");
2626
}
27-
private void DoAThirdThing()
27+
private void ExamplePrivateMethod()
2828
{
29-
Debug.Log("That third thing be did.");
29+
Debug.Log("Invoking: ExampleClass.ExamplePrivateMethod");
30+
}
31+
protected void ExampleProtectedMethod()
32+
{
33+
Debug.Log("Invoking: ExampleClass.ExampleProtectedMethod");
3034
}
3135

3236

@@ -42,11 +46,10 @@ public class ExampleClass : MonoBehaviour
4246
*/
4347

4448
#if UNITY_EDITOR
45-
[MethodButton("DoAThing", "DoAnotherThing", "DoAThirdThing")]
46-
[SerializeField] private bool editorFoldout;
49+
[MethodButton("ExampleInternalMethod", "ExamplePublicMethod", "ExamplePrivateMethod", "ExampleProtectedMethod")]
50+
[SerializeField] private bool editorFoldout;
4751
#endif
4852
}
49-
5053
```
5154

5255

0 commit comments

Comments
 (0)