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

EditorToolAttribute

UnityEditor.EditorTools 中的类

/

继承自:EditorTools.ToolAttribute

建议更改

成功!

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

关闭

提交失败

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

关闭

取消

描述

EditorTool 注册为全局工具或特定目标类型的 组件 工具。

全局工具适用于任何选择。全局工具也始终从顶部工具栏可用。组件工具(如 CustomEditor)仅适用于与目标类型匹配的选择。

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;
using UnityEditor.EditorTools;
using UnityEngine.Rendering;

// By passing `typeof(MeshFilter)` as the second argument, we register VertexTool as a CustomEditor tool to be presented
// when the current selection contains a MeshFilter component.
[EditorTool("Show Vertices", typeof(MeshFilter))]
class VertexTool : EditorTool, IDrawSelectedHandles
{
    struct TransformAndPositions
    {
        public Transform transform;
        public Vector3[] positions;
    }

    IEnumerable<TransformAndPositions> m_Vertices;
    GUIContent m_ToolbarIcon;

    public override GUIContent toolbarIcon
    {
        get
        {
            if (m_ToolbarIcon == null)
                m_ToolbarIcon = new GUIContent(
                    AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/Examples/Icons/VertexTool.png"),
                    "Vertex Visualization Tool");
            return m_ToolbarIcon;
        }
    }

    void OnEnable()
    {
        m_Vertices = targets.Select(x =>
        {
            return new TransformAndPositions()
            {
                transform = ((MeshFilter)x).transform,
                positions = ((MeshFilter)x).sharedMesh.vertices
            };
        }).ToArray();
    }

    public override void OnToolGUI(EditorWindow window)
    {
        foreach (var entry in m_Vertices)
        {
            var matrix = entry.transform.localToWorldMatrix;
            Drawing.DrawHandleCaps(matrix, entry.positions, Color.cyan * .3f, .05f, CompareFunction.Greater);
            Drawing.DrawHandleCaps(matrix, entry.positions, Color.cyan, .05f, CompareFunction.LessEqual);
        }
    }

    public void OnDrawHandles()
    {
        if(!ToolManager.IsActiveTool(this))
            return;

        foreach (var entry in m_Vertices)
        {
            var matrix = entry.transform.localToWorldMatrix;
            Drawing.DrawHandleCaps(matrix, entry.positions, Color.cyan * .3f, .03f, CompareFunction.Greater);
            Drawing.DrawHandleCaps(matrix, entry.positions, Color.cyan, .03f, CompareFunction.LessEqual);
        }
    }
}

您还可以使用工具变体将类似的工具分组到工具叠加中的单个按钮中。请参阅 ToolAttribute.variantGroup

using System;
using UnityEditor;
using UnityEditor.EditorTools;
using UnityEngine;

namespace GlobalToolVariants
{
    // Define 3 tools that should be shown as a single button in the Tools Overlay.
    struct ShapeVariantGroup {}

    [EditorTool("Line (Custom Global)", variantGroup = typeof(ShapeVariantGroup), variantPriority = 2)]
    [Icon("Assets/Examples/Icons/Variant-Line.png")]
    class Line : EditorTool {}

    [EditorTool("Circle (Custom Global)", variantGroup = typeof(ShapeVariantGroup), variantPriority = 1)]
    [Icon("Assets/Examples/Icons/Variant-Circle.png")]
    class Circle : EditorTool {}

    [EditorTool("Square (Custom Global)", variantGroup = typeof(ShapeVariantGroup), variantPriority = 0)]
    [Icon("Assets/Examples/Icons/Variant-Square.png")]
    class Square : EditorTool {}
}

构造函数

EditorToolAttribute将 EditorTool 注册为全局工具或 CustomEditor 工具。

继承的成员

静态属性

defaultPriorityToolAttribute.toolPriority 和 ToolAttribute.variantPriority 的默认值。指定低于此值的优先级以在默认条目之前显示工具,或指定更高的值以在默认条目之后显示它。

属性

displayName在菜单中显示的名称。
targetContext如果提供,EditorTool 仅在 ToolManager.activeContextType 等于 targetContext 时可用。
targetType设置为此 EditorTool 或 EditorToolContext 可以编辑的类型。如果工具不特定于组件,并且应该在任何时候都可用,则将其设置为 null。
toolPriority工具优先级定义工具在工具叠加中显示的顺序。
variantGroup工具变体用于将逻辑上相似的工具分组到工具叠加中的单个按钮中。
variantPriority变体优先级定义工具在显示在 ToolAttribute.variantGroup 下拉菜单中时的显示顺序。