class in UnityEditor.Overlays
/
Inherits from:Overlays.Overlay
Implements interfaces:ICreateToolbar
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
CloseToolbarOverlay is an implementation of Overlay that provides a base for Overlays that can be placed in horizontal or vertical toolbars.
The constructor for ToolbarOverlay accepts a list of IDs corresponding to UnityEditor.Toolbars.EditorToolbarElement::id.
Toolbars are composed of collections of UnityEditor.Toolbars.EditorToolbarElement. In this way it is very simple to reuse elements to create customized toolbars.
Toolbar overlays require specific styling, so in most cases it is preferable to inherit one of the predefined EditorToolbar types when creating Visual Elements. If custom UI is required, it is valid to inherit any UnityEngine.UIElements.VisualElement type and provide styling yourself.
using UnityEditor; using UnityEditor.Overlays; using UnityEditor.Toolbars; using UnityEngine; // [EditorToolbarElement(Identifier, EditorWindowType)] is used to register toolbar elements for use in ToolbarOverlay // implementations. [EditorToolbarElement(id, typeof(SceneView))] class CreateCubes : EditorToolbarButton, IAccessContainerWindow { // This ID is used to populate toolbar elements. public const string id = "ExampleToolbar/Button"; // IAccessContainerWindow provides a way for toolbar elements to access the `EditorWindow` in which they exist. // Here we use `containerWindow` to focus the camera on our newly instantiated objects after creation. public EditorWindow containerWindow { get; set; } // As this is ultimately just a VisualElement, it is appropriate to place initialization logic in the constructor. // In this method you can also register to any additional events as required. Here we will just set up the basics: // a tooltip, icon, and action. public CreateCubes() { // A toolbar element can be either text, icon, or a combination of the two. Keep in mind that if a toolbar is // docked horizontally the text will be clipped, so usually it's a good idea to specify an icon. text = "Create Cubes"; icon = AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/CreateCubesIcon.png"); tooltip = "Instantiate some cubes in the scene."; clicked += OnClick; } // This method will be invoked when the `CreateCubes` button is clicked. void OnClick() { var parent = new GameObject("Cubes").transform; // When writing editor tools don't forget to be a good citizen and implement Undo! Undo.RegisterCreatedObjectUndo(parent.gameObject, "Create Cubes in Sphere"); for (int i = 0; i < 50; i++) { var cube = GameObject.CreatePrimitive(PrimitiveType.Cube).transform; Undo.RegisterCreatedObjectUndo(cube.gameObject, "Create Cubes in Sphere"); cube.position = Random.insideUnitSphere * 25; cube.rotation = Quaternion.LookRotation(Random.onUnitSphere); Undo.SetTransformParent(cube, parent, "Create Cubes in Sphere"); cube.SetParent(parent); } Selection.activeTransform = parent; if (containerWindow is SceneView view) view.FrameSelected(); } } // Same as above, except this time we'll create a toggle + dropdown toolbar item. [EditorToolbarElement(id, typeof(SceneView))] class DropdownToggleExample : EditorToolbarDropdownToggle, IAccessContainerWindow { public const string id = "ExampleToolbar/DropdownToggle"; // This property is specified by IAccessContainerWindow and is used to access the Overlay's EditorWindow. public EditorWindow containerWindow { get; set; } static int colorIndex = 0; static readonly Color[] colors = new Color[] { Color.red, Color.green, Color.cyan }; DropdownToggleExample() { text = "Color Bar"; tooltip = "Display a color swatch in the top left of the scene view. Toggle on or off, and open the dropdown" + "to change the color."; // When the dropdown is opened, ShowColorMenu is invoked and we can create a popup menu. dropdownClicked += ShowColorMenu; // Subscribe to the Scene View OnGUI callback so that we can draw our color swatch. SceneView.duringSceneGui += DrawColorSwatch; } void DrawColorSwatch(SceneView view) { // Test that this callback is for the Scene View that we're interested in, and also check if the toggle is on // or off (value). if (view != containerWindow || !value) return; Handles.BeginGUI(); GUI.color = colors[colorIndex]; GUI.DrawTexture(new Rect(8, 8, 120, 24), Texture2D.whiteTexture); GUI.color = Color.white; Handles.EndGUI(); } // When the dropdown button is clicked, this method will create a popup menu at the mouse cursor position. void ShowColorMenu() { var menu = new GenericMenu(); menu.AddItem(new GUIContent("Red"), colorIndex == 0, () => colorIndex = 0); menu.AddItem(new GUIContent("Green"), colorIndex == 1, () => colorIndex = 1); menu.AddItem(new GUIContent("Blue"), colorIndex == 2, () => colorIndex = 2); menu.ShowAsContext(); } } // All Overlays must be tagged with the OverlayAttribute [Overlay(typeof(SceneView), "Placement Tools")] // IconAttribute provides a way to define an icon for when an Overlay is in collapsed form. If not provided, the first // two letters of the Overlay name will be used. [Icon("Assets/PlacementToolsIcon.png")] // Toolbar overlays must inherit `ToolbarOverlay` and implement a parameter-less constructor. The contents of a toolbar // are populated with string IDs, which are passed to the base constructor. IDs are defined by // EditorToolbarElementAttribute. public class EditorToolbarExample : ToolbarOverlay { // ToolbarOverlay implements a parameterless constructor, passing 2 EditorToolbarElementAttribute IDs. This will // create a toolbar overlay with buttons for the CreateCubes and DropdownToggleExample examples. // This is the only code required to implement a toolbar overlay. Unlike panel overlays, the contents are defined // as standalone pieces that will be collected to form a strip of elements. EditorToolbarExample() : base( CreateCubes.id, DropdownToggleExample.id) {} }
toolbarElements | Use toolbarElements to specify the contents of this Overlay. |
CreatePanelContent | Creates the root VisualElement of the ToolbarOverlay's content in panel layout. |
ussClassName | USS class name of elements of this type. |
collapsed | Defines whether the overlay is in collapsed form. |
collapsedIcon | Defines a custom icon to use when that overlay is in collapsed form. |
containerWindow | EditorWindow the overlay is contained within. |
defaultSize | Set defaultSize to define the size of an Overlay when it hasn't been resized by the user. |
displayed | Shows or hides the overlay. |
displayName | Name of overlay used as title. |
floating | Returns true if overlay is floating, returns false if overlay is docked in a corner or in a toolbar. |
floatingPosition | Local position of closest overlay corner to closest dockposition when floating. |
id | Overlay unique ID. |
isInToolbar | Returns true if overlay is docked in a toolbar. |
layout | The preferred layout for the Overlay. |
maxSize | Maximum size of the Overlay. |
minSize | Minimum size of the Overlay. |
rootVisualElement | The root VisualElement. |
size | Size of the Overlay. |
Close | Remove the Overlay from its OverlayCanvas. |
CreateContent | Creates a new VisualElement containing the contents of this Overlay. |
OnCreated | OnCreated is invoked when an Overlay is instantiated in an Overlay Canvas. |
OnWillBeDestroyed | Called when an Overlay is about to be destroyed. |
RefreshPopup | Resize the OverlayPopup to fit the content. |
Undock | If this Overlay is currently in a toolbar, it will be removed and return to a floating state. |
collapsedChanged | Invoked when Overlay.collapsed value is changed. |
displayedChanged | This callback is invoked when the Overlay.displayed value has been changed. |
floatingChanged | Called when the value of floating has changed. |
floatingPositionChanged | This event is invoked when Overlay.floatingPosition is changed. |
layoutChanged | Subscribe to this event to be notified when the Overlay.Layout property is modified. |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.