版本: Unity 6 (6000.0)
语言English
  • C#

EditorTool

UnityEditor.EditorTools 中的类

/

继承自:ScriptableObject

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交,但我们会阅读用户提出的每个建议更改,并在适用时进行更新。

关闭

提交失败

由于某些原因,您的建议更改无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

描述

使用此类实现编辑器工具。这是所有编辑器工具继承的基类。

将此类与 EditorToolAttribute 一起使用,以向编辑器注册自定义编辑器工具。

工具主要有两种类型:全局和组件。有关如何将工具指定为全局或组件的信息,请参阅 EditorToolAttribute

全局工具类似于内置的移动、旋转、缩放工具。它们始终可用,并与当前 Selection 中的内容一起使用。

组件工具类似于 CustomEditor,特定于 Component。当活动选择包含可编辑类型时,这些工具可用。

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); } } }

属性

gridSnapEnabled使用此属性允许当前 EditorTool 启用/禁用网格捕捉。
target正在检查的对象。
targets正在检查的对象数组。
toolbarIcon此自定义编辑器工具的图标和工具提示。如果未实现此函数,则工具栏将显示目标类型的检查器图标。如果未定义目标类型,则工具栏将显示工具模式图标。

公共方法

IsAvailable根据编辑器的状态检查自定义编辑器工具是否可用。
OnActivated此 EditorTool 成为活动工具后调用。
OnToolGUI使用此方法实现自定义编辑器工具。
OnWillBeDeactivated此 EditorTool 停止成为活动工具之前调用。
PopulateMenu向场景视图上下文菜单添加菜单项。

继承的成员

属性

hideFlags对象是否应隐藏、与场景一起保存或用户可修改?
name对象的名称。

公共方法

GetInstanceID获取对象的实例 ID。
ToString返回对象的名称。

静态方法

Destroy移除 GameObject、组件或资源。
DestroyImmediate立即销毁对象 obj。强烈建议使用 Destroy 代替。
DontDestroyOnLoad加载新场景时不要销毁目标对象。
FindAnyObjectByType检索类型为 type 的任何活动加载对象。
FindFirstObjectByType检索类型为 type 的第一个活动加载对象。
FindObjectsByType检索类型为 type 的所有加载对象的列表。
Instantiate克隆对象 original 并返回克隆。
InstantiateAsync捕获原始对象(必须与某个 GameObject 相关)的快照,并返回 AsyncInstantiateOperation。
CreateInstance创建可脚本化对象的实例。

运算符

bool对象是否存在?
operator !=比较两个对象是否引用不同的对象。
operator ==比较两个对象引用是否引用同一个对象。

消息

Awake创建 ScriptableObject 实例时调用。
OnDestroy当可脚本化对象将要被销毁时调用此函数。
OnDisable当可脚本化对象超出范围时调用此函数。
OnEnable加载对象时调用此函数。
OnValidate仅编辑器功能,当加载脚本或检查器中的值发生更改时,Unity 会调用此功能。
Reset重置为默认值。