版本: Unity 6 (6000.0)
语言英语
  • C#

ToolbarOverlay

UnityEditor.Overlays 中的类

/

继承自: Overlays.Overlay


实现接口: ICreateToolbar

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交内容,但我们会阅读用户提出的每个更改建议,并在适用情况下进行更新。

关闭

提交失败

由于某种原因,您的更改建议无法提交。请在几分钟后 <a>重试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

描述

ToolbarOverlayOverlay 的实现,它提供了一个基础,用于放置在水平或垂直工具栏中的叠加层。

ToolbarOverlay 的构造函数接受一个 ID 列表,这些 ID 对应于 UnityEditor.Toolbars.EditorToolbarElement::id。

工具栏由 UnityEditor.Toolbars.EditorToolbarElement 集合组成。这样,重用元素来创建自定义工具栏就变得非常简单。

工具栏叠加层需要特定的样式,因此在大多数情况下,在创建 Visual Elements 时最好继承预定义的 EditorToolbar 类型之一。如果需要自定义 UI,则可以继承任何 UnityEngine.UIElements.VisualElement 类型并自行提供样式。

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使用 toolbarElements 指定此叠加层的内容。

公共方法

CreatePanelContent在面板布局中创建 ToolbarOverlay 内容的根 VisualElement。

继承成员

静态属性

ussClassName此类型元素的 USS 类名。

属性

collapsed定义叠加层是否处于折叠状态。
collapsedIcon定义叠加层处于折叠状态时要使用的自定义图标。
containerWindow包含叠加层的 EditorWindow。
defaultSize设置 defaultSize 以定义叠加层在用户未调整大小时的尺寸。
displayed显示或隐藏叠加层。
displayName用作标题的叠加层名称。
floating如果叠加层是浮动的,则返回 true,如果叠加层停靠在角落或工具栏中,则返回 false。
floatingPosition当浮动时,最靠近停靠位置的叠加层角落的局部位置。
id叠加层的唯一 ID。
isInToolbar如果叠加层停靠在工具栏中,则返回 true。
layout叠加层的首选布局。
maxSize叠加层的最大尺寸。
minSize叠加层的最小尺寸。
rootVisualElement根 VisualElement。
size叠加层的尺寸。

公共方法

关闭从 OverlayCanvas 中删除叠加层。
CreateContent创建一个新的 VisualElement,其中包含此叠加层的内容。
OnCreated在 Overlay Canvas 中实例化叠加层时调用 OnCreated。
OnWillBeDestroyed在要销毁叠加层时调用。
RefreshPopup调整 OverlayPopup 的大小以适合内容。
Undock如果此叠加层当前位于工具栏中,它将被删除并返回到浮动状态。

事件

collapsedChanged当 Overlay.collapsed 值发生变化时调用。
displayedChanged当 Overlay.displayed 值发生变化时调用此回调。
floatingChanged当 floating 的值发生变化时调用。
floatingPositionChanged当 Overlay.floatingPosition 发生变化时调用此事件。
layoutChanged订阅此事件以在 Overlay.Layout 属性修改时收到通知。