创建编辑器窗体
Unity提供了几种可以创建编辑器窗体的方式,开发者可以针对不同使用情况来创建。
1、继承自ScriptableWizard类创建对话框窗体
Unity为开发者提供了一个简单的快速创建对话框窗体的方式,只需要继承自ScriptableWizard类,我们容易发现ScriptableWizard实际上是继承自EditorWindow,只是做了一层封装,该类窗体一般用于快捷功能的操作,例如统一修改场景中多个对象的位置等信息。
代码示例
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class Test4_WindowExample : ScriptableWizard
{
public string StrName = "123";
public int Age = 18;
[MenuItem("MyWindows/Window1")]
public static void ShowWindow()
{
ScriptableWizard.DisplayWizard<Test4_WindowExample>("Window1_ScriptableWizard", "确定", "取消");
}
private void OnEnable()
{
Debug.Log("窗口打开");
}
private void OnDisable()
{
Debug.Log("Disable");
}
private void OnDestroy()
{
Debug.Log("Destroy");
}
private void OnFocus()
{
Debug.Log("OnFocus");
}
private void OnLostFocus()
{
Debug.Log("LostFocus");
}
private void OnInspectorUpdate()
{
Debug.Log("Update");
}
private void OnWizardCreate()
{
Debug.Log("Create");
}
private void OnWizardOtherButton()
{
Debug.Log("OtherBtn");
}
private void OnSelectionChange()
{
Debug.Log("SelectionChange");
if(Selection.activeObject)
Debug.Log(Selection.activeObject.name);
}
private void OnWizardUpdate()
{
Debug.Log("WizardUpdate");
}
//private void OnGUI()
//{
// if (GUI.Button(new Rect(0, 0, 120, 40), "确定12"))
// {
// Debug.Log("OnClicked");
// }
//}
}
效果展示
2、继承自EditorWindow类创建自定义窗体
创建自定义的编辑器窗体都需要继承自EditorWindow类,它可以自由浮动,也可以作为选项卡停靠,就像Unity编辑器中其他的窗体一样。编辑器窗体一般由菜单项打开,通常包含某一类的功能。作为一个编辑器类,需要放在Editor文件夹中。
代码示例
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class Test4_Window2 : EditorWindow
{
private static Test4_Window2 window;
[MenuItem("MyWindows/Window2")]
public static void ShowWindow()
{
window = EditorWindow.GetWindow<Test4_Window2>("Window_EditorWindow");
window.Show();
}
private void OnEnable()
{
Debug.Log("OnEnable");
}
private void OnDisable()
{
Debug.Log("OnDisable");
}
private void OnDestroy()
{
Debug.Log("OnDestroy");
}
private void OnFocus()
{
Debug.Log("OnFocus");
}
private void OnLostFocus()
{
Debug.Log("OnLostFocus");
}
private void OnInspectorUpdate()
{
Debug.Log("OnUpdate");
}
private void OnSelectionChange()
{
Debug.Log("OnSelectionChange");
}
private void OnGUI()
{
Debug.Log("OnGUI");
}
}
效果展示
3、继承自PopupWindowContent类创建弹窗
代码示例
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class Test4_PopWindowContent : EditorWindow
{
private static Test4_PopWindowContent window;
private PopWindowExample popWindow = new PopWindowExample();
private Rect buttonRect;
[MenuItem("MyWindows/Window3")]
public static void ShowWindow()
{
window = EditorWindow.GetWindow<Test4_PopWindowContent>();
window.Show();
}
private void OnGUI()
{
GUILayout.Label("Popup example", EditorStyles.boldLabel);
if (GUILayout.Button("Popup Options", GUILayout.Width(200)))
{
PopupWindow.Show(buttonRect, popWindow);
}
//获取GUILayout最后用于控件的矩形
if (Event.current.type == EventType.Repaint)
buttonRect = GUILayoutUtility.GetLastRect();
}
}
public class PopWindowExample : PopupWindowContent
{
private bool toggle = true;
public override void OnOpen()
{
Debug.Log("OnOpen");
}
public override void OnClose()
{
Debug.Log("OnClose");
}
public override Vector2 GetWindowSize()
{
return new Vector2(200, 100);
}
public override void OnGUI(Rect rect)
{
EditorGUILayout.LabelField("PopWindow");
EditorGUILayout.LabelField("弹出框");
toggle = EditorGUILayout.Toggle(toggle);
}
}
效果展示