# MainToolbarElementAttribute

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

## Definition

* **Type:** Class
* **Namespace:** [UnityEditor.Toolbars](/engine/6000.6/script-reference/unityeditor/toolbars.md)
* **Assembly:** UnityEditor.CoreModule
* **Inherits from:** [Attribute](https://learn.microsoft.com/dotnet/api/system.attribute)

```csharp
[AttributeUsage(AttributeTargets.Method)]
public sealed class MainToolbarElementAttribute : Attribute
```

## Remarks

Methods tagged with this attribute return a [MainToolbarElement](/engine/6000.6/script-reference/unityeditor/toolbars/maintoolbarelement.md) or one of its child types. It's also possible to return an IEnumerable of [MainToolbarElement](/engine/6000.6/script-reference/unityeditor/toolbars/maintoolbarelement.md) to group multiple elements together (See example below). Each [MainToolbarElement](/engine/6000.6/script-reference/unityeditor/toolbars/maintoolbarelement.md) 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](/engine/6000.6/script-reference/unityeditor/toolbars/maintoolbarbutton.md), [MainToolbarToggle](/engine/6000.6/script-reference/unityeditor/toolbars/maintoolbartoggle.md), [MainToolbarDropdown](/engine/6000.6/script-reference/unityeditor/toolbars/maintoolbardropdown.md), [MainToolbarSlider](/engine/6000.6/script-reference/unityeditor/toolbars/maintoolbarslider.md), [MainToolbarLabel](/engine/6000.6/script-reference/unityeditor/toolbars/maintoolbarlabel.md).

## Examples

```csharp
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 Fields

| Value                                                                                                                          | Description                                                                                                                                                                                                               |
| ------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [defaultMenuPriority](/engine/6000.6/script-reference/unityeditor/toolbars/maintoolbarelementattribute/defaultmenupriority.md) | The default value for 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                                                                                                |
| ------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------- |
| [defaultDockIndex](/engine/6000.6/script-reference/unityeditor/toolbars/maintoolbarelementattribute/defaultdockindex.md)       | Specify the order in which custom elements are docked in the main toolbar.                                 |
| [defaultDockPosition](/engine/6000.6/script-reference/unityeditor/toolbars/maintoolbarelementattribute/defaultdockposition.md) | Use this property to specify the default dock position of the element: the left, the middle, or the right. |
| [menuPriority](/engine/6000.6/script-reference/unityeditor/toolbars/maintoolbarelementattribute/menupriority.md)               | Menu priority defines the order that main toolbar elements are displayed in within the toolbar's menu.     |
| [path](/engine/6000.6/script-reference/unityeditor/toolbars/maintoolbarelementattribute/path.md)                               | The unique identifier of this toolbar element.                                                             |

## Inheritance

**Inherited Members:**

* [Attribute.GetCustomAttributes(MemberInfo, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-memberinfo-system-type\))
* [Attribute.GetCustomAttributes(MemberInfo, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-memberinfo-system-type-system-boolean\))
* [Attribute.GetCustomAttributes(MemberInfo)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-memberinfo\))
* [Attribute.GetCustomAttributes(MemberInfo, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-memberinfo-system-boolean\))
* [Attribute.IsDefined(MemberInfo, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.isdefined#system-attribute-isdefined\(system-reflection-memberinfo-system-type\))
* [Attribute.IsDefined(MemberInfo, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.isdefined#system-attribute-isdefined\(system-reflection-memberinfo-system-type-system-boolean\))
* [Attribute.GetCustomAttribute(MemberInfo, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattribute#system-attribute-getcustomattribute\(system-reflection-memberinfo-system-type\))
* [Attribute.GetCustomAttribute(MemberInfo, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattribute#system-attribute-getcustomattribute\(system-reflection-memberinfo-system-type-system-boolean\))
* [Attribute.GetCustomAttributes(ParameterInfo)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-parameterinfo\))
* [Attribute.GetCustomAttributes(ParameterInfo, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-parameterinfo-system-type\))
* [Attribute.GetCustomAttributes(ParameterInfo, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-parameterinfo-system-type-system-boolean\))
* [Attribute.GetCustomAttributes(ParameterInfo, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-parameterinfo-system-boolean\))
* [Attribute.IsDefined(ParameterInfo, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.isdefined#system-attribute-isdefined\(system-reflection-parameterinfo-system-type\))
* [Attribute.IsDefined(ParameterInfo, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.isdefined#system-attribute-isdefined\(system-reflection-parameterinfo-system-type-system-boolean\))
* [Attribute.GetCustomAttribute(ParameterInfo, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattribute#system-attribute-getcustomattribute\(system-reflection-parameterinfo-system-type\))
* [Attribute.GetCustomAttribute(ParameterInfo, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattribute#system-attribute-getcustomattribute\(system-reflection-parameterinfo-system-type-system-boolean\))
* [Attribute.GetCustomAttributes(Module, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-module-system-type\))
* [Attribute.GetCustomAttributes(Module)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-module\))
* [Attribute.GetCustomAttributes(Module, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-module-system-boolean\))
* [Attribute.GetCustomAttributes(Module, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-module-system-type-system-boolean\))
* [Attribute.IsDefined(Module, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.isdefined#system-attribute-isdefined\(system-reflection-module-system-type\))
* [Attribute.IsDefined(Module, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.isdefined#system-attribute-isdefined\(system-reflection-module-system-type-system-boolean\))
* [Attribute.GetCustomAttribute(Module, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattribute#system-attribute-getcustomattribute\(system-reflection-module-system-type\))
* [Attribute.GetCustomAttribute(Module, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattribute#system-attribute-getcustomattribute\(system-reflection-module-system-type-system-boolean\))
* [Attribute.GetCustomAttributes(Assembly, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-assembly-system-type\))
* [Attribute.GetCustomAttributes(Assembly, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-assembly-system-type-system-boolean\))
* [Attribute.GetCustomAttributes(Assembly)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-assembly\))
* [Attribute.GetCustomAttributes(Assembly, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattributes#system-attribute-getcustomattributes\(system-reflection-assembly-system-boolean\))
* [Attribute.IsDefined(Assembly, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.isdefined#system-attribute-isdefined\(system-reflection-assembly-system-type\))
* [Attribute.IsDefined(Assembly, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.isdefined#system-attribute-isdefined\(system-reflection-assembly-system-type-system-boolean\))
* [Attribute.GetCustomAttribute(Assembly, Type)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattribute#system-attribute-getcustomattribute\(system-reflection-assembly-system-type\))
* [Attribute.GetCustomAttribute(Assembly, Type, bool)](https://learn.microsoft.com/dotnet/api/system.attribute.getcustomattribute#system-attribute-getcustomattribute\(system-reflection-assembly-system-type-system-boolean\))
* [Attribute.Equals(Object)](https://learn.microsoft.com/dotnet/api/system.attribute.equals)
* [Attribute.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.attribute.gethashcode)
* [Attribute.Match(Object)](https://learn.microsoft.com/dotnet/api/system.attribute.match)
* [Attribute.IsDefaultAttribute()](https://learn.microsoft.com/dotnet/api/system.attribute.isdefaultattribute)
* [Attribute.TypeId()](https://learn.microsoft.com/dotnet/api/system.attribute.typeid)
