使用此类来实现内置变换工具的专门版本。内置变换工具包括移动、旋转、缩放、矩形和变换。
using System; using System.Collections.Generic; using UnityEditor; using UnityEditor.EditorTools; using UnityEngine; // EditorToolContextAttribute is what registers a context with the UI. [EditorToolContext("Wobbly Transform Tools")] // The icon path can also be used with packages. Ex "Packages/com.wobblestudio.wobblytools/Icons/Transform.png". [Icon("Assets/Examples/Icons/TransformIcon.png")] public class WobbleContext : EditorToolContext { // Tool contexts can also implement an OnToolGUI function that is invoked before tools. This is a good place to // add any custom selection logic, for example. public override void OnToolGUI(EditorWindow _) { } protected override Type GetEditorToolType(Tool tool) { switch (tool) { // Return the type of tool to be used for Tool.Move. The Tool Manager will handle instantiating and // activating the tool. case Tool.Move: return typeof(WobblyMoveTool); // For any tools that are not implemented, return null to disable the tool in the menu. default: return null; } } } // Note that tools used by an EditorToolContext do not need to use EditorToolAttribute. class WobblyMoveTool : EditorTool { struct Selected { public Transform transform; public Vector3 localScale; } Vector3 m_Origin; List<Selected> m_Selected = new List<Selected>(); void StartMove(Vector3 origin) { m_Origin = origin; m_Selected.Clear(); foreach(var trs in Selection.transforms) m_Selected.Add(new Selected() { transform = trs, localScale = trs.localScale }); Undo.RecordObjects(Selection.transforms, "Wobble Move Tool"); } // This is silly example that oscillates the scale of the selected objects as they are moved. public override void OnToolGUI(EditorWindow _) { var evt = Event.current.type; var hot = GUIUtility.hotControl; EditorGUI.BeginChangeCheck(); var p = Handles.PositionHandle(Tools.handlePosition, Tools.handleRotation); if (evt == EventType.MouseDown && hot != GUIUtility.hotControl) StartMove(p); if (EditorGUI.EndChangeCheck()) { foreach (var selected in m_Selected) { selected.transform.position += (p - Tools.handlePosition); var scale = Vector3.one * (Mathf.Sin(Mathf.Abs(Vector3.Distance(m_Origin, p))) * .5f); selected.transform.localScale = selected.localScale + scale; } } } }
GetAdditionalToolTypes | 获取要与内置变换工具显示在同一类别中的其他工具集合。 |
OnActivated | 在该 EditorToolContext 成为活动工具上下文后调用。 |
OnToolGUI | 为该上下文可用的操纵工具集实现任何通用功能。 |
OnWillBeDeactivated | 在该 EditorToolContext 停止成为活动工具上下文之前调用。 |
PopulateMenu | 将菜单项添加到场景视图上下文菜单。 |
ResolveTool | 返回给定上下文中指定工具的匹配 EditorTool 类型。 |
GetEditorToolType | 定义给定工具的 EditorTool 类型。 |
GetInstanceID | 获取对象的实例 ID。 |
ToString | 返回对象的名称。 |
Destroy | 删除游戏对象、组件或资源。 |
DestroyImmediate | 立即销毁对象 obj。强烈建议使用 Destroy 代替。 |
DontDestroyOnLoad | 在加载新场景时不要销毁目标对象。 |
FindAnyObjectByType | 检索 Type 类型的任何活动已加载对象。 |
FindFirstObjectByType | 检索 Type 类型的第一个活动已加载对象。 |
FindObjectsByType | 检索 Type 类型的所有已加载对象的列表。 |
Instantiate | 克隆对象 original 并返回克隆。 |
InstantiateAsync | 捕获原始对象(必须与某个游戏对象相关)的快照,并返回 AsyncInstantiateOperation。 |
CreateInstance | 创建可脚本化对象的实例。 |
bool | 对象是否存在? |
operator != | 比较两个对象是否引用不同的对象。 |
operator == | 比较两个对象引用,以查看它们是否引用同一个对象。 |