Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


EditorToolAttribute

Registers an EditorTool as either a Global tool or a Component tool for a specific target type.
Read time 6 minutesLast updated 6 days ago

Definition

[AttributeUsage(AttributeTargets.Class, Inherited = false)]public sealed class EditorToolAttribute : ToolAttribute, _Attribute

Remarks

A Global tool works on any selection. A Global tool is also always available from the top toolbar. A Component tool, like a CustomEditor, is only available for selections that match a target type.
using System.Collections.Generic;using System.Linq;using UnityEditor;using UnityEngine;using UnityEngine.Rendering;public static class Drawing{ public static void DrawPoints(Transform transform, IList<Vector3> positions, Color color, float size) { if (positions == null || !positions.Any()) return; var matrix = transform.localToWorldMatrix; DrawHandleCaps(matrix, positions, color * .3f, size, CompareFunction.Greater); DrawHandleCaps(matrix, positions, color, size, CompareFunction.LessEqual); } public static void DrawHandleCaps(Matrix4x4 transform, IList<Vector3> positions, Color color, float size, CompareFunction compare) { if (Event.current.type != EventType.Repaint) return; var camera = Camera.current; Vector3 sideways = (camera == null ? Vector3.right : camera.transform.right); Vector3 up = (camera == null ? Vector3.up : camera.transform.up); var prevColor = Handles.color; Handles.color = color; var prevCompare = Handles.zTest; Handles.zTest = compare; var world = transform.MultiplyPoint3x4(positions[0]); // DotHandleCap call sets up the material and GL state, so just re-use it to avoid expensive setup for each quad // Done this way because HandleUtility.ApplyWireMaterial is private Handles.DotHandleCap(0, world, Quaternion.identity, HandleUtility.GetHandleSize(world) * size, EventType.Repaint); GL.Begin(GL.QUADS); foreach (var p in positions) { var position = transform.MultiplyPoint(p); var handleSize = HandleUtility.GetHandleSize(position) * size; GL.Color(color); GL.Vertex(position + (sideways + up) * handleSize); GL.Vertex(position + (sideways - up) * handleSize); GL.Vertex(position - (sideways + up) * handleSize); GL.Vertex(position - (sideways - up) * handleSize); } GL.End(); Handles.color = prevColor; Handles.zTest = prevCompare; }}
You can also use tool variants to group similar tools into a single button in the Tools overlay. Refer to 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 {}}

Constructors

Inheritance

Inherited Members