EditorToolbarToggle
A toggle displayed as a button. Mainly used inside an Overlay or with EditorToolbarElementAttribute.
Read time 10 minutesLast updated 4 days ago
Definition
- Type: Class
- Namespace: UnityEditor.Toolbars
- Assembly: UnityEditor
- Inherits from: ToolbarToggle
- Implements: IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, ICustomStyle, IBindable, IMixedValueSupport, INotifyValueChanged<bool>
public class EditorToolbarToggle : ToolbarToggle, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, ICustomStyle, IBindable, IMixedValueSupport, INotifyValueChanged<bool>
Remarks
EditorToolbarTogglecheckedEditorToolbarToggleWhile this toggle can be used anywhere in the Editor, the recommended best practice is to use it inside ToolbarOverlay or the more generic Overlay.
Additional Resources: EditorToolbarButton, EditorToolbarDropdown, EditorToolbarDropdownToggle, EditorToolbarFloatField.
Examples
using UnityEditor;using UnityEditor.Overlays;using UnityEditor.Toolbars;using UnityEngine.UIElements;// Define a new element to be used in toolbars.[EditorToolbarElement("MyCustomPlayButton", typeof(SceneView))]public class MyPlayToggle : EditorToolbarToggle{ public MyPlayToggle() { onIcon = EditorGUIUtility.FindTexture("StopButton"); //The icon used when the toggle value is true. offIcon = EditorGUIUtility.FindTexture("PlayButton"); //The icon used when the toggle value is false. text = "Custom"; tooltip = "This is my custom Play mode"; SetValueWithoutNotify(EditorApplication.isPlayingOrWillChangePlaymode); this.RegisterValueChangedCallback(OnValueChanged); RegisterCallback<AttachToPanelEvent>((evt) => RegisterCallback()); RegisterCallback<DetachFromPanelEvent>((evt) => UnregisterCallback()); } void RegisterCallback() { EditorApplication.playModeStateChanged += OnPlayModeStateChanged; } void UnregisterCallback() { EditorApplication.playModeStateChanged -= OnPlayModeStateChanged; } void OnValueChanged(ChangeEvent<bool> evt) { if (evt.newValue) { EditorApplication.EnterPlaymode(); // Do some custom stuff after entering Play mode. // In this example, we open the profiler window when entering Play mode. EditorWindow.GetWindow<ProfilerWindow>(); } else { EditorApplication.ExitPlaymode(); } } void OnPlayModeStateChanged(PlayModeStateChange state) { SetValueWithoutNotify(EditorApplication.isPlayingOrWillChangePlaymode); }}[Overlay(typeof(SceneView), "My Overlay", defaultDisplay = true)]public class OverlayExample : ToolbarOverlay{ // Add my toggle to my custom overlay using the id defined in `EditorToolbarElement`. public OverlayExample() : base("MyCustomPlayButton") { }}
Static Fields
Value | Description |
|---|---|
| ussClassName | USS Class Name used to style the EditorToolbarToggle. |
Constructors
Constructor | Description |
|---|---|
| EditorToolbarToggle() | Constructor. |
| EditorToolbarToggle(System.String) | Constructor. |
| EditorToolbarToggle(System.String,System.String) | Constructor. |
| EditorToolbarToggle(System.String,UnityEngine.Texture2D,UnityEngine.Texture2D) | Constructor. |
| EditorToolbarToggle(UnityEngine.Texture2D) | Constructor. |
| EditorToolbarToggle(UnityEngine.Texture2D,UnityEngine.Texture2D) | Constructor. |
Properties
Property | Description |
|---|---|
| icon | The icon associated with the toggle. |
| offIcon | The icon associated with the toggle when the value is false. |
| onIcon | The icon associated with the toggle when the value is true. |
| text | The text associated with the toggle. |
| textIcon | The text icon associated with the toggle. |
Methods
Method | Description |
|---|---|
| SetValueWithoutNotify | Allow to set a value without being notified of the change, if any. |