由于建模时的疏忽或者网上找的一些测试模型不规范,会出现导入unity中,模型的世界坐标下的中心点与模型实际的位置差出很多。如下图所示,这个模型的中心点远离模型十万八千里!!
我们通过继承Unity的EditorTool类来实现一个快捷工具将坐标移动到合适的位置
先新建一个类AxisTool让其继承EditorTool类并实现抽象成员toolbarIcon
注:需要引入UnityEditor.EditorTools
using UnityEditor.EditorTools;
public class AxisTool : EditorTool
{
private GUIContent uIContent;
public override GUIContent toolbarIcon
{
get
{
if (uIContent == null)
{
uIContent = new GUIContent();
//鼠标悬停提示
uIContent.tooltip = "AxisTool";
//通过Unity内置图标名称"AvatarPivot"获取Texture
uIContent.image = EditorGUIUtility.IconContent("AvatarPivot").image;
}
return uIContent;
}
}
}
这时Unity还没发生变化,我们通过EditorToolAttribute特性将我们的工具注册到编辑器
[EditorTool("AxisTool", typeof(Transform))]//参数1:工具名 参数2:工具作用于的目标类型
public class AxisTool : EditorTool
这时在Unity的工具窗口就能看到我们自定义的工具的图标了
在OnEnable和OnDisable中处理工具被切换和选择对象切换时的事件
//当前使用工具的对象
private Transform _target;
private void OnEnable()
{
EditorTools.activeToolChanged += TargetChangeEvent;
Selection.selectionChanged += TargetChangeEvent;
}
private void OnDisable()
{
EditorTools.activeToolChanged -= TargetChangeEvent;
Selection.selectionChanged -= TargetChangeEvent;
}
private void TargetChangeEvent()
{
//判断当前激活的工具是否是AxisTool
if (!EditorTools.IsActiveTool(this)) return;
_target = target as Transform;
}
在 OnToolGUI中绘制出工具的窗口
public override void OnToolGUI(EditorWindow window)
{
if (_target == null) return;
Rect rect = new Rect(window.position.width - 200, window.position.height - 170, 160, 50);
Handles.BeginGUI();
GUI.Box(rect, "", "Window");
GUI.enabled = true;
Handles.EndGUI();
}
好麻烦!不写了,感兴趣的自己看源码去,源码链接: