使用此类可实现自定义编辑器工具。它是所有编辑器工具继承自的基类。
将此类与 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; } } }
gridSnapEnabled | Use this property to allow the current EditorTool to enable/disable grid snapping. |
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. |
GetInstanceID | Gets the instance ID of the object. |
ToString | 返回对象的名称。 |
Destroy | 移除 GameObject、组件或资源。 |
DestroyImmediate | 立即销毁对象 /obj/。强烈建议您改用 Destroy。 |
DontDestroyOnLoad | 在加载新的 Scene 时,请勿销毁 Object。 |
FindObjectOfType | 返回第一个类型为 type 的已加载的激活对象。 |
FindObjectsOfType | Gets a list of all loaded objects of Type type. |
Instantiate | 克隆 original 对象并返回克隆对象。 |
CreateInstance | 创建脚本化对象的实例。 |
bool | 该对象是否存在? |
operator != | 比较两个对象是否引用不同的对象。 |
operator == | 比较两个对象引用,判断它们是否引用同一个对象。 |