Version: 2022.3

EditorTool

class in UnityEditor.EditorTools

/

继承自:ScriptableObject

切换到手册

描述

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

将此类与 EditorToolAttribute 结合使用可向编辑器注册自定义编辑器工具。

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 &amp;&amp; !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 &amp;&amp; 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); } } }

变量

gridSnapEnabledUse this property to allow the current EditorTool to enable/disable grid snapping.
target所检查的对象。
targets正在检查的对象的数组。
toolbarIcon用于自定义编辑器工具的图标和工具提示。如果此函数未实现,工具栏将显示用于目标类型的 Inspector 图标。如果未定义目标类型,工具栏将显示 Tool Mode 图标。

公共函数

IsAvailable根据编辑器状态检查自定义编辑器工具是否可用。
OnActivatedInvoked after this EditorTool becomes the active tool.
OnToolGUI使用此方法可实现自定义编辑器工具。
OnWillBeDeactivatedInvoked before this EditorTool stops being the active tool.

继承的成员

变量

hideFlags该对象应该隐藏、随场景一起保存还是由用户修改?
name对象的名称。

公共函数

GetInstanceIDGets the instance ID of the object.
ToString返回对象的名称。

静态函数

Destroy移除 GameObject、组件或资源。
DestroyImmediate立即销毁对象 /obj/。强烈建议您改用 Destroy。
DontDestroyOnLoad在加载新的 Scene 时,请勿销毁 Object。
FindAnyObjectByTypeRetrieves any active loaded object of Type type.
FindFirstObjectByTypeRetrieves the first active loaded object of Type type.
FindObjectOfType返回第一个类型为 type 的已加载的激活对象。
FindObjectsByTypeRetrieves a list of all loaded objects of Type type.
FindObjectsOfTypeGets a list of all loaded objects of Type type.
Instantiate克隆 original 对象并返回克隆对象。
CreateInstance创建脚本化对象的实例。

运算符

bool该对象是否存在?
operator !=比较两个对象是否引用不同的对象。
operator ==比较两个对象引用,判断它们是否引用同一个对象。

消息

Awake当 ScriptableObject 脚本启动时调用此函数。
OnDestroy当脚本化对象将销毁时调用此函数。
OnDisable当脚本化对象超出范围时调用此函数。
OnEnable当对象加载时调用此函数。
OnValidateEditor-only function that Unity calls when the script is loaded or a value changes in the Inspector.
Reset重置为默认值。