Unity内置了两个类:Editor和EditorWindow。其中Editor主要负责某个GameObject在Scene视图下的UI显示,EditorWindow主要生成一个Window作为插件功能使用。
Editor
使用Editor功能需要一个脚本如TestTool.cs继承MonoBehavior,然后使用TestEditor继承Editor来实现其功能。
需要在TestEditor上添加关键字CustomEditor来表明是对应于哪个脚本,当TestTool.cs挂载的GameObject被选中时TestEditor就会执行。 TestEditor需要放在名为Editor的目录下,路径无关。
TestEditor可以重新实现OnSceneGUI和OnInspectorGUI来绘制自己的GUI。
1 OnSceneGUI
在OnSceneGUI函数是当GameObject被选中每帧执行的。当Scene窗口的Gizmos关闭不执行。
可以获取Event事件,不过这个事件需要在当前挂载TestTool.cs的GameObject被选中时才能接收到。
[CustomEditor(typeof(TestTool))]
public class TestEditor : Editor
{
private void OnSceneGUI()
{
Event e = Event.current;
if(e.isKey)
{
if(e.type == EventType.KeyDown)
{
Debug.Log("YES");
}
}
}
}
在OnSceneGUI函数中可以重新绘制GUI,使用Handle获取相关信息,绘制SceneGUI可以使用GUILayout接口:
string testValue = "";
private void OnSceneGUI()
{
TestTool