-
Notifications
You must be signed in to change notification settings - Fork 18
BehaviourButton
Matheus Lessa Rodrigues edited this page Feb 2, 2018
·
7 revisions
If you need to execute a piece of code while in Unity Editor, you can simply put the attribute [Button]
above a function. Doing that, a button will show on its inspector and clicking it will run that function.
using UnityEngine;
using BitStrap;
public sealed class MyScript : MonoBehaviour
{
[Button]
public void SomeFunction()
{
Debug.Log( "Hello!" );
// Some code here!
}
}
With this sample code, MyScript
's inspector will look like:
By pressing the Some Function
button, Hello!
will be print on the console and whatever other code you put there, will also execute.
Note, however, that the [Button]
attribute won't work with custom editors! When writing custom editors, you can call ButtonAttributeHelper.DrawButtons()
to draw it wherever you want, tho.
using UnityEngine;
using UnityEditor;
using BitStrap;
[CustomEditor( typeof( MyScript )]
public sealed class MyCustomEditor : Editor
{
private ButtonAttributeHelper helper = new ButtonAttributeHelper();
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
helper.DrawButtons();
}
private void OnEnable()
{
helper.Init( target );
}
}
- UMake
- WebApi
- AnimationUtilities
- EditorGraph
- RuntimeConsole
- Editor Tools
- BehaviourButton
- Isolate
- Custom Editors and Property Drawers
- Tween
- Util