Version: 2021.3
LanguageEnglish
  • C#

EditorTool

class in UnityEditor.EditorTools

/

Inherits from:ScriptableObject

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

Use this class to implement editor tools. This is the base class from which all editor tools are inherited.

Use this class with EditorToolAttribute to register custom editor tools with the Editor.

There are two basic types of tools, Global and Component. See EditorToolAttribute for information on how to designate a tool as Global or Component.

Global tools are like the built-in Move, Rotate, Scale tools. They are always available, and work with whatever is in the current Selection.

Component tools, similar to CustomEditor, are specific to a Component. These tools are made available when the active selection contains an editable type.

using UnityEditor;
using UnityEditor.EditorTools;
using UnityEditor.ShortcutManagement;
using UnityEngine;

// Example MonoBehaviour that oscillates a transform position between two points. public class Platform : MonoBehaviour { [SerializeField] Vector3 m_Start = new Vector3(-10, 0f, 0f);

[SerializeField] Vector3 m_End = new Vector3(10f, 0f, 0f);

[SerializeField] float m_Speed = .2f;

public Vector3 start { get => m_Start; set => m_Start = value; }

public Vector3 end { get => m_End; set => m_End = value; }

public float speed { get => m_Speed; set => m_Speed = value; }

void Update() { SnapToPath(Time.time); }

public void SnapToPath(float time) { transform.position = Vector3.Lerp(m_Start, m_End, (Mathf.Sin(time * m_Speed) + 1) * .5f); } }

// The second argument in the EditorToolAttribute flags this as a Component tool. That means that it will be instantiated // and destroyed along with the selection. EditorTool.targets will contain the selected objects matching the type. [EditorTool("Platform Tool", typeof(Platform))] class PlatformTool : EditorTool, IDrawSelectedHandles { // Enable or disable preview animation bool m_AnimatePlatforms;

// Global tools (tools that do not specify a target type in the attribute) are lazy initialized and persisted by // a ToolManager. Component tools (like this example) are instantiated and destroyed with the current selection. void OnEnable() { // Allocate unmanaged resources or perform one-time set up functions here }

void OnDisable() { // Free unmanaged resources, state teardown. }

// The second "context" argument accepts an EditorWindow type. [Shortcut("Activate Platform Tool", typeof(SceneView), KeyCode.P)] static void PlatformToolShortcut() { if (Selection.GetFiltered<Platform>(SelectionMode.TopLevel).Length > 0) ToolManager.SetActiveTool<PlatformTool>(); else Debug.Log("No platforms selected!"); }

// Called when the active tool is set to this tool instance. Global tools are persisted by the ToolManager, // so usually you would use OnEnable and OnDisable to manage native resources, and OnActivated/OnWillBeDeactivated // to set up state. See also `EditorTools.{ activeToolChanged, activeToolChanged }` events. public override void OnActivated() { SceneView.lastActiveSceneView.ShowNotification(new GUIContent("Entering Platform Tool"), .1f); }

// Called before the active tool is changed, or destroyed. The exception to this rule is if you have manually // destroyed this tool (ex, calling `Destroy(this)` will skip the OnWillBeDeactivated invocation). public override void OnWillBeDeactivated() { SceneView.lastActiveSceneView.ShowNotification(new GUIContent("Exiting Platform Tool"), .1f); }

// Equivalent to Editor.OnSceneGUI. public override void OnToolGUI(EditorWindow window) { if (!(window is SceneView sceneView)) return;

Handles.BeginGUI(); using (new GUILayout.HorizontalScope()) { using (new GUILayout.VerticalScope(EditorStyles.helpBox)) { m_AnimatePlatforms = EditorGUILayout.Toggle("Animate Platforms", m_AnimatePlatforms); // To animate platforms we need the Scene View to repaint at fixed intervals, so enable `alwaysRefresh` // and scene FX (need both for this to work). In older versions of Unity this is called `materialUpdateEnabled` sceneView.sceneViewState.alwaysRefresh = m_AnimatePlatforms; if (m_AnimatePlatforms && !sceneView.sceneViewState.fxEnabled) sceneView.sceneViewState.fxEnabled = true;

if (GUILayout.Button("Snap to Path")) foreach (var obj in targets) if (obj is Platform platform) platform.SnapToPath((float)EditorApplication.timeSinceStartup); }

GUILayout.FlexibleSpace(); } Handles.EndGUI();

foreach (var obj in targets) { if (!(obj is Platform platform)) continue;

if (m_AnimatePlatforms && Event.current.type == EventType.Repaint) platform.SnapToPath((float)EditorApplication.timeSinceStartup);

EditorGUI.BeginChangeCheck(); var start = Handles.PositionHandle(platform.start, Quaternion.identity); var end = Handles.PositionHandle(platform.end, Quaternion.identity); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(platform, "Set Platform Destinations"); platform.start = start; platform.end = end; } } }

// IDrawSelectedHandles interface allows tools to draw gizmos when the target objects are selected, but the tool // has not yet been activated. This allows you to keep MonoBehaviour free of debug and gizmo code. public void OnDrawHandles() { foreach (var obj in targets) { if (obj is Platform platform) Handles.DrawLine(platform.start, platform.end, 6f); } } }

Properties

gridSnapEnabledUse this property to allow the current EditorTool to enable/disable grid snapping.
targetThe object being inspected.
targetsAn array of the objects being inspected.
toolbarIconThe icon and tooltip for this custom editor tool. If this function is not implemented, the toolbar displays the Inspector icon for the target type. If no target type is defined, the toolbar displays the Tool Mode icon.

Public Methods

IsAvailableChecks whether the custom editor tool is available based on the state of the editor.
OnActivatedInvoked after this EditorTool becomes the active tool.
OnToolGUIUse this method to implement a custom editor tool.
OnWillBeDeactivatedInvoked before this EditorTool stops being the active tool.

Inherited Members

Properties

hideFlagsShould the object be hidden, saved with the Scene or modifiable by the user?
nameThe name of the object.

Public Methods

GetInstanceIDGets the instance ID of the object.
ToStringReturns the name of the object.

Static Methods

DestroyRemoves a GameObject, component or asset.
DestroyImmediateDestroys the object obj immediately. You are strongly recommended to use Destroy instead.
DontDestroyOnLoadDo not destroy the target Object when loading a new Scene.
FindAnyObjectByTypeRetrieves any active loaded object of Type type.
FindFirstObjectByTypeRetrieves the first active loaded object of Type type.
FindObjectOfTypeReturns the first active loaded object of Type type.
FindObjectsByTypeRetrieves a list of all loaded objects of Type type.
FindObjectsOfTypeGets a list of all loaded objects of Type type.
InstantiateClones the object original and returns the clone.
CreateInstanceCreates an instance of a scriptable object.

Operators

boolDoes the object exist?
operator !=Compares if two objects refer to a different object.
operator ==Compares two object references to see if they refer to the same object.

Messages

AwakeCalled when an instance of ScriptableObject is created.
OnDestroyThis function is called when the scriptable object will be destroyed.
OnDisableThis function is called when the scriptable object goes out of scope.
OnEnableThis function is called when the object is loaded.
OnValidateEditor-only function that Unity calls when the script is loaded or a value changes in the Inspector.
ResetReset to default values.