Version: Unity 6.6 Alpha (6000.6)
LanguageEnglish
  • C#

ISupportsEditorTools

interface in UnityEditor.EditorTools


Implements interfaces:ISupportsOverlays

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

Implement this interface to enable EditorTools in the EditorWindow.

When ISupportsEditorTools is implemented, the window is registered with the Editor Tools framework, gains overlay support, and can host tools, contexts, and tool-related overlays. The window can then be used as the target tool owner for EditorToolContexts registered via EditorToolContextAttribute.

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

// Register this window as a tool owner with BasicPointsContext as the default context
[EditorToolOwner(typeof(BasicPointsContext))]
public class BasicPointsEditor : EditorWindow, ISupportsEditorTools
{
    public List<Vector3> points = new();

    Camera m_Camera;
    public Camera handlesCamera => GetOrCreateCamera();

    [MenuItem("Window/Basic Points Editor")]
    static void ShowWindow() => GetWindow<BasicPointsEditor>("Basic Points Editor");

    Camera GetOrCreateCamera()
    {
        if (m_Camera == null)
        {
            var cameraGO = new GameObject();
            cameraGO.hideFlags = HideFlags.HideAndDontSave;

            m_Camera = cameraGO.AddComponent<Camera>();
            m_Camera.orthographic = true;
            m_Camera.orthographicSize = 10f;
            m_Camera.transform.forward = Vector3.forward;
            m_Camera.transform.position = new Vector3(0f, 0f, -10f);
            m_Camera.enabled = false;
        }

        return m_Camera;
    }

    void OnDisable()
    {
        if (m_Camera != null)
            DestroyImmediate(m_Camera.gameObject);
    }
}

// Use the `targetToolOwner` parameter to make this context available only in BasicPointsEditor
[EditorToolContext("Points Editor", targetToolOwner = typeof(BasicPointsEditor))]
public class BasicPointsContext : EditorToolContext
{
    protected override Type GetEditorToolType(Tool tool) => tool switch
    {
        Tool.Move => typeof(MovePointTool),
        _ => null
    };

    // Visualize all points while this context is active
    public override void OnToolGUI(EditorWindow window)
    {
        base.OnToolGUI(window);

        var pointsWindow = window as BasicPointsEditor;
        if (pointsWindow == null)
            return;

        if (Event.current.type == EventType.Repaint)
        {
            using (new Handles.DrawingScope(Color.white))
            {
                for (int i = 0; i < pointsWindow.points.Count; ++i)
                {
                    var point = pointsWindow.points[i];
                    var size = HandleUtility.GetHandleSize(point) * 0.035f;
                    Handles.DotHandleCap(0, point, Quaternion.identity, size, EventType.Repaint);
                }
            }
        }
    }
}

// Make this tool available in BasicPointsEditor by targeting BasicPointsContext via the `targetContext` parameter
[EditorTool("Create Point", targetContext = typeof(BasicPointsContext), group = typeof(CreationToolsGroup))]
public class CreatePointTool : EditorTool
{
    public override void OnToolGUI(EditorWindow window)
    {
        var pointsWindow = window as BasicPointsEditor;
        if (pointsWindow == null)
            return;

        var evt = Event.current;
        if (evt.type == EventType.MouseDown && evt.button == 0)
        {
            var ray = HandleUtility.GUIPointToWorldRay(evt.mousePosition);
            if (new Plane(Vector3.forward, 0f).Raycast(ray, out var d))
                pointsWindow.points.Add(ray.GetPoint(d));
            evt.Use();
        }
    }
}

// Registered via BasicPointsContext.GetEditorToolType — no attribute required.
public class MovePointTool : EditorTool
{
    public override void OnToolGUI(EditorWindow window)
    {
        var pointsWindow = window as BasicPointsEditor;
        if (pointsWindow == null || pointsWindow.points.Count == 0)
            return;

        var center = Vector3.zero;
        for (int i = 0; i < pointsWindow.points.Count; ++i)
            center += pointsWindow.points[i];
        center /= pointsWindow.points.Count;

        EditorGUI.BeginChangeCheck();
        var moved = Handles.PositionHandle(center, Quaternion.identity);
        if (EditorGUI.EndChangeCheck())
        {
            var delta = moved - center;
            for (int i = 0; i < pointsWindow.points.Count; ++i)
                pointsWindow.points[i] += delta;
        }
    }
}

Properties

Property Description
handlesCameraThe Camera to use when rendering EditorTool handles in this window.