将 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 工具。 |
defaultPriority | ToolAttribute.toolPriority 和 ToolAttribute.variantPriority 的默认值。指定低于此值的优先级以在默认条目之前显示工具,或指定更高的值以在默认条目之后显示它。 |
displayName | 在菜单中显示的名称。 |
targetContext | 如果提供,EditorTool 仅在 ToolManager.activeContextType 等于 targetContext 时可用。 |
targetType | 设置为此 EditorTool 或 EditorToolContext 可以编辑的类型。如果工具不特定于组件,并且应该在任何时候都可用,则将其设置为 null。 |
toolPriority | 工具优先级定义工具在工具叠加中显示的顺序。 |
variantGroup | 工具变体用于将逻辑上相似的工具分组到工具叠加中的单个按钮中。 |
variantPriority | 变体优先级定义工具在显示在 ToolAttribute.variantGroup 下拉菜单中时的显示顺序。 |