使用此类可实现自定义编辑器工具。它是所有编辑器工具继承自的基类。
将此类与 EditorToolAttribute 结合使用可向编辑器注册自定义编辑器工具。
using System; using UnityEngine; using UnityEditor; using UnityEditor.EditorTools;
// Tagging a class with the EditorTool attribute and no target type registers a global tool. Global tools are valid for any selection, and are accessible through the top left toolbar in the editor. [EditorTool("Platform Tool")] class PlatformTool : EditorTool { // Serialize this value to set a default value in the Inspector. [SerializeField] Texture2D m_ToolIcon;
GUIContent m_IconContent;
void OnEnable() { m_IconContent = new GUIContent() { image = m_ToolIcon, text = "Platform Tool", tooltip = "Platform Tool" }; }
public override GUIContent toolbarIcon { get { return m_IconContent; } }
// This is called for each window that your tool is active in. Put the functionality of your tool here. public override void OnToolGUI(EditorWindow window) { EditorGUI.BeginChangeCheck();
Vector3 position = Tools.handlePosition;
using (new Handles.DrawingScope(Color.green)) { position = Handles.Slider(position, Vector3.right); }
if (EditorGUI.EndChangeCheck()) { Vector3 delta = position - Tools.handlePosition;
Undo.RecordObjects(Selection.transforms, "Move Platform");
foreach (var transform in Selection.transforms) transform.position += delta; } } }
target | 所检查的对象。 |
targets | 正在检查的对象的数组。 |
toolbarIcon | 用于自定义编辑器工具的图标和工具提示。如果此函数未实现,工具栏将显示用于目标类型的 Inspector 图标。如果未定义目标类型,工具栏将显示 Tool Mode 图标。 |
IsAvailable | 根据编辑器状态检查自定义编辑器工具是否可用。 |
OnActivated | Invoked after this EditorTool becomes the active tool. |
OnToolGUI | 使用此方法可实现自定义编辑器工具。 |
OnWillBeDeactivated | Invoked before this EditorTool stops being the active tool. |