Version: Unity 6.3 LTS (6000.3)
LanguageEnglish
  • C#

EditorToolAttribute

class in UnityEditor.EditorTools

/

Inherits from:EditorTools.ToolAttribute

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Registers an EditorTool as either a Global tool or a Component tool for a specific target type.

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

Constructor Description
EditorToolAttributeRegisters an EditorTool as either a Global tool or a CustomEditor tool.

Inherited Members

Static Properties

PropertyDescription
defaultPriorityThe default value for ToolAttribute.toolPriority and ToolAttribute.variantPriority. Specify a priority lower than this value to display a tool before the default entries, or specify a higher value to display it after the default entries.

Properties

PropertyDescription
allowPersistentTargetsAllows the tool to target persistent objects. This is only compatible with ScriptableObjects. When you develop tools that target persistent objects, let users know whether they're editing an in-scene or a persistent object.
displayNameThe name that displays in menus.
groupTool groups place logically similar tools under a single header in the Tools Overlay.
targetContextIf provided, the EditorTool will only be made available when the ToolManager.activeContextType is equal to targetContext.
targetTypeSet to the type that this EditorTool or EditorToolContext can edit. Set to null if the tool is not specific to a Component and should be available at any time.
toolPriorityTool priority defines the order that tools are displayed in within the Tools Overlay.
variantGroupTool variants are used to group logically similar tools into a single button in the Tools Overlay.
variantPriorityThe variant priority defines the order that tools are displayed in when they are displayed in a ToolAttribute.variantGroup dropdown.