开发者可以通过派生 EditorWindows 绘制完全自定义的窗口。
使用 EditorWindow.GetWindow()来打开窗口,在OnGUI中进行绘制。
通过实现 IHasCustomMenu 来添加菜单
可以在我们的窗口中绘制选中对象的预览,就像 Inspector 的 Preview 子窗口
例子:
using UnityEngine;
using UnityEditor;
/// <summary>
/// 自定义窗口
/// </summary>
public class CustomEditorWindow : EditorWindow, IHasCustomMenu
{
[MenuItem("X/CustomWindow")]
static void ShowWindow()
{
CustomEditorWindow wnd = EditorWindow.GetWindow<CustomEditorWindow>();
wnd.Show();
}
/// <summary>
/// 扩展窗口右上角下拉菜单
/// </summary>
/// <param name="menu"></param>
void IHasCustomMenu.AddItemsToMenu(GenericMenu menu)
{
menu.AddDisabledItem(new GUIContent("Disabled"));
menu.AddItem(new GUIContent("Test1"), true, () => { Debug.Log("click Test1"); });
menu.AddItem(new GUIContent("Test2"), false, () => { Debug.Log("click Test2"); });
menu.AddSeparator("Test/");
menu.AddItem(new GUIContent("Test/Test3"), true, () => { Debug.Log("click Test3"); });
}
private Texture _Texture;
private float _FloatValue;
private Editor mEditor;
private GameObject go;
private void Awake()
{
Debug.Log("初始化调用");
_Texture = AssetDatabase.LoadAssetAtPath<Texture>("Assets/unity.png");
}
/// <summary>
/// 绘制窗口
/// </summary>
private void OnGUI()
{
GUILayout.Label("XXXXXX", EditorStyles.boldLabel);
_FloatValue = EditorGUILayout.Slider(_FloatValue, 0, 100);
if (_Texture != null)
GUI.DrawTexture(new Rect(0, 30, 100, 100), _Texture);
// 绘制 GameObject 的预览
if(Selection.activeGameObject)
{
if (Selection.activeGameObject != go)
{
Editor.DestroyImmediate(mEditor);
mEditor = null;
}
if (mEditor == null)
mEditor = Editor.CreateEditor(Selection.activeGameObject);
mEditor.OnPreviewGUI(GUILayoutUtility.GetRect(500, 500), GUIStyle.none);
}
}
private void OnDestroy()
{
Debug.Log("销毁时调用");
}
private void OnFocus()
{
Debug.Log("获得焦点");
}
private void OnLostFocus()
{
Debug.Log("失去焦点");
}
private void OnInspectorUpdate()
{
//Debug.Log("Inspector 每帧更新");
}
private void OnHierarchyChange()
{
Debug.Log("Hierarchy 视图改变时调用");
}
private void OnProjectChange()
{
Debug.Log("Project 视图改变时调用");
}
private void OnSelectionChange()
{
Debug.Log("Project or Hierarchy 视图选择对象时调用");
}
private void Update()
{
//Debug.Log("每帧更新");
}
}