Version: Unity 6.5 Alpha (6000.5)
LanguageEnglish
  • C#

MainToolbarElementAttribute

class in UnityEditor.Toolbars

Suggest a change

Success!

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.

Close

Submission failed

For 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.

Close

Cancel

Description

Registers a static method you can use to extend the main toolbar with custom UI elements.

Methods tagged with this attribute return a MainToolbarElement or one of its child types. It's also possible to return an IEnumerable of MainToolbarElement to group multiple elements together (See example below). Each MainToolbarElement describes the content and behaviour of a toolbar element and the main toolbar queries these methods when it needs to create or update its toolbar elements.

Additional resources: MainToolbarButton, MainToolbarToggle, MainToolbarDropdown, MainToolbarSlider, MainToolbarLabel.

using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Toolbars;
using UnityEngine;
using UnityEngine.UIElements;

public class MyCustomElement 
{
    const string k_ToolbarElementName = "Examples/Analysis Windows";
    static (string, string, string)[] s_Elements = 
        new(string name, string tooptip, string menuPath)[]
        {
            // Open profiler window
            ("Profiler", "Open the Profiler window","Window/Analysis/Profiler"),
            // Open frame debugger window
            ("Frame Debugger", "Open the Frame Debugger window","Window/Analysis/Frame Debugger"),
            // Open physics debugger window
            ("Physics Debugger", "Open the Physics Debugger window","Window/Analysis/Physics Debugger")
        };

    static bool s_DisplayAsButtons = true;
    
    [MainToolbarElement(k_ToolbarElementName, defaultDockPosition = MainToolbarDockPosition.Middle)]
    static IEnumerable<MainToolbarElement> CreateAnalysisWindowsBar()
    {
        if (s_DisplayAsButtons)
        {
            foreach (var element in s_Elements)
            {
                yield return new MainToolbarButton(
                    new MainToolbarContent(element.Item1, element.Item2),
                    () => EditorApplication.ExecuteMenuItem(element.Item3))
                {
                    populateContextMenu = PopulateContextMenu,
                };
            }
        }
        else
        {
            yield return new MainToolbarDropdown(
                new MainToolbarContent("Analysis", "Open the list of available analysis windows"),
                ShowDropdownMenu)
            {
                populateContextMenu = PopulateContextMenu,
            };
        }
    }

    static void PopulateContextMenu(DropdownMenu menu)
    {
        menu.AppendAction(
            L10n.Tr(s_DisplayAsButtons ? "Display as Dropdown" : "Display as Buttons"),
            UpdateDisplayType);
    }

    static void UpdateDisplayType(DropdownMenuAction _)
    {
        s_DisplayAsButtons = !s_DisplayAsButtons;
        MainToolbar.Refresh(k_ToolbarElementName);
    }

    static void ShowDropdownMenu(Rect dropDownRect)
    {
        var menu = new GenericMenu();
        foreach (var element in s_Elements)
        {
            menu.AddItem(new GUIContent(element.Item1), false, () =>
            {
                EditorApplication.ExecuteMenuItem(element.Item3);
            });
        }
        menu.DropDown(dropDownRect);
    }
}

Static Properties

Property Description
defaultMenuPriorityThe default value for MainToolbarElement.menuPriority. Specify a priority lower than this value to display a main toolbar element's menu item before the default entries, or specify a higher value to display it after the default entries.

Properties

Property Description
defaultDockIndexSpecify the order in which custom elements are docked in the main toolbar.
defaultDockPositionUse this property to specify the default dock position of the element: the left, the middle, or the right.
menuPriorityMenu priority defines the order that main toolbar elements are displayed in within the toolbar's menu.
pathThe unique identifier of this toolbar element.
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961