参考
Unity Editor 基础篇(三):自定义窗口
【Unity编辑器】扩展总结四:创建编辑器窗体
一、EditorWindow示例
整体效果image.png
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using System.IO;
public class MyFirstWindow : EditorWindow
{
//用于储存记录Bug人的名字
string bugReporterName = "";
//用于描述Bug信息
string description = "";
//用于储存 Bug 对象
GameObject buggyGameObject;
//利用构造函数来设置窗口的名字
MyFirstWindow()
{
this.titleContent = new GUIContent("Bug Reporter");
}
[MenuItem("Tool/Bug Reporter")]
static void showWindow()
{
EditorWindow.GetWindow(typeof(MyFirstWindow));
}
//绘制窗口界面的函数
private void OnGUI()
{
GUILayout.BeginVertical();
//绘制标题
GUILayout.Space(10);
GUI.skin.label.fontSize = 24;
GUI.skin.label.alignment = TextAnchor.MiddleCenter;
GUILayout.Label("Bug Reporter");
//绘制文本
GUILayout.Space(10);
bugReporterName = EditorGUILayout.TextField("Bug Name", bugReporterName);
//绘制当前正在编辑的场景
GUILayout.Space(10);
GUI.skin.label.fontSize = 12;
GUI.skin.label.alignment = TextAnchor.UpperLeft;
GUILayout.Label("Currently Scene:" + EditorSceneManager.GetActiveScene().name);
//绘制当前时间
GUILayout.Space(10);
GUILayout.Label("Time:" + System.DateTime.Now);
//绘制对象
GUILayout.Space(10);
buggyGameObject = (GameObject)EditorGUILayout.ObjectField(
"Buggy Game Object", buggyGameObject, typeof(GameObject), true);
//绘制描述文本区域
GUILayout.Space(10);
GUILayout.BeginHorizontal();
GUILayout.Label("Describtion", GUILayout.MaxWidth(80));
description = EditorGUILayout.TextArea(description, GUILayout.MaxHeight(75));
GUILayout.EndHorizontal();
EditorGUILayout.Space();
//添加名为"Save Bug"按钮
if (GUILayout.Button("Save Bug"))
{
SaveBug();
}
//添加名为"Save Bug With Screenshot"按钮
if (GUILayout.Button("Save Bug With Screenshot"))
{
SaveBugWithScreenshot();
}
GUILayout.EndVertical();
}
void SaveBug()
{
Directory.CreateDirectory("Assets/BugReports/" + bugReporterName);
var now = System.DateTime.Now.ToString("yyyy年MM月dd HH时mm分ss秒");
var path = "Assets\\BugReports\\" + bugReporterName + "\\" + now + ".txt";
StreamWriter sw = new StreamWriter(path);
sw.WriteLine(bugReporterName);
sw.WriteLine(System.DateTime.Now.ToString());
sw.WriteLine(EditorSceneManager.GetActiveScene().name);
sw.WriteLine(description);
//刷新缓存
sw.Flush();
//关闭流
sw.Close();
}
void SaveBugWithScreenshot()
{
SaveBug();
var now = System.DateTime.Now.ToString("yyyy年MM月dd HH时mm分ss秒");
var path = "Assets/BugReports/" + bugReporterName + "/" + now + ".png";
ScreenCapture.CaptureScreenshot(path);
}
}
1.设置窗口的名字
//利用构造函数来设置窗口的名字
MyFirstWindow()
{
this.titleContent = new GUIContent("Bug Reporter");
}
image.png
2.GUI skin
//绘制标题
GUILayout.Space(10);
GUI.skin.label.fontSize = 24;
GUI.skin.label.alignment = TextAnchor.MiddleCenter;
GUILayout.Label("Bug Reporter");
GUI.skin.label.fontSize 、GUI.skin.label.alignment 用于设置标题的字体大小和对齐格式,具体从GUI skin了解
image.png
image.png
3.绘制对象槽
//绘制对象
GUILayout.Space(10);
buggyGameObject = (GameObject)EditorGUILayout.ObjectField(
"Buggy Game Object", buggyGameObject, typeof(GameObject), true);
- 第一个参数用于设置卡槽的标题名字
- 第二个参数用于设置字段显示的物体
- 第三个参数用于设置显示的类型
- 第四个参数用于设置是否允许指定场景中的物件
4.截屏
【Unity】关于ScreenCapture.CaptureScreenshot截屏的尝试
二、ScriptableWizard
Unity为开发者提供了一个简单的快速创建对话框窗体的方式,只需要继承自ScriptableWizard类,我们容易发现ScriptableWizard实际上是继承自EditorWindow,只是做了一层封装,该类窗体一般用于快捷功能的操作,例如统一修改场景中多个对象的位置等信息。
using UnityEngine;
using UnityEditor;
public class WindowExample1 : ScriptableWizard
{
public string msg = "";
//显示窗体
[MenuItem("MyWindow/First Window")]
private static void ShowWindow()
{
ScriptableWizard.DisplayWizard<WindowExample1>("WindowExample1", "确定", "取消");
}
//显示时调用
private void OnEnable()
{
Debug.Log("OnEnable");
}
//更新时调用
private void OnWizardUpdate()
{
Debug.Log("OnWizardUpdate");
if (string.IsNullOrEmpty(msg))
{
errorString = "请输入信息内容";//错误提示
helpString = "";//帮助提示
}
else
{
errorString = "";
helpString = "请点击确认按钮";
}
}
//点击确定按钮时调用
private void OnWizardCreate()
{
Debug.Log("OnWizardCreate");
}
//点击第二个按钮时调用
private void OnWizardOtherButton()
{
Debug.Log("OnWizardOtherButton");
}
//当ScriptableWizard需要更新其GUI时,将调用此函数以绘制内容
//为GUI绘制提供自定义行为,默认行为是按垂直方向排列绘制所有公共属性字段
//一般不重写该方法,按照默认绘制方法即可
protected override bool DrawWizardGUI()
{
return base.DrawWizardGUI();
}
//隐藏时调用
private void OnDisable()
{
Debug.Log("OnDisable");
}
//销毁时调用
private void OnDestroy()
{
Debug.Log("OnDestroy");
}
}
image.png
三、PopupWindowContent
用于实现在编辑器中弹出窗口,弹窗类继承自PopupWindowContent类,当弹窗失去焦点时,就会自动关闭。如图,点击Popup Options按钮就会展开面板,点击其它地方就会收起。
image.png
using UnityEngine;
using UnityEditor;
public class WindowExample3 : EditorWindow
{
private static WindowExample3 window;
private PopWindowExample popWindow = new PopWindowExample();
private Rect buttonRect;
//显示窗体
[MenuItem("MyWindow/Third Window")]
private static void ShowWindow()
{
window = EditorWindow.GetWindow<WindowExample3>("Window Example 3");
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
{
bool toggle = true;
//开启弹窗时调用
public override void OnOpen()
{
Debug.Log("OnOpen");
}
//绘制弹窗内容
public override void OnGUI(Rect rect)
{
EditorGUILayout.LabelField("PopWindow");
toggle = EditorGUILayout.Toggle("Toggle", toggle);
}
//关闭弹窗时调用
public override void OnClose()
{
Debug.Log("OnClose");
}
public override Vector2 GetWindowSize()
{
//设置弹窗的尺寸
return new Vector2(200, 100);
}
}
网友评论