Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


EditorToolbarToggle

A toggle displayed as a button. Mainly used inside an Overlay or with EditorToolbarElementAttribute.
Read time 9 minutesLast updated 5 days ago

Definition

public class EditorToolbarToggle : ToolbarToggle, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, ICustomStyle, IBindable, INotifyValueChanged<bool>, IMixedValueSupport

Remarks

EditorToolbarToggle
is visually represented as a button but has the behavior of a toggle. While true, the button highlights in a different color and the element has the
checked
pseudo class.
EditorToolbarToggle
includes built-in functionality to set an icon for both the true and false state and text content.
While this toggle can be used anywhere in the Editor, the recommended best practice is to use it inside ToolbarOverlay or the more generic Overlay.

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

ussClassNameUSS Class Name used to style the EditorToolbarToggle.

Constructors

Properties

Property

Description

iconThe icon associated with the toggle.
offIconThe icon associated with the toggle when the value is false.
onIconThe icon associated with the toggle when the value is true.
textThe text associated with the toggle.
textIconThe text icon associated with the toggle.

Methods

Method

Description

SetValueWithoutNotifyAllow to set a value without being notified of the change, if any.

Inheritance

Inherited Members