一:实现思路
Unity编辑器中的所有窗口的绘制都继承自EditorWindow类,并且需要引入UnityEditor的命名空间,使用类中的方法自定义任何所需的窗口
二:代码实现
——窗口Window
[MenuItem("工具栏层级")]
private static void ShowWindow()
{
GetWindow(typeof(TestWindow), true, "测试窗口");
}
——标签Label
private void OnGUI()
{
GUI.skin.label.fontSize = 20;
GUI.skin.label.fontStyle = FontStyle.Bold;
GUI.skin.label.alignment = TextAnchor.MiddleCenter;
GUILayout.Label("测试窗口");
GUILayout.Label(Resources.Load<Texture>("Resources文件夹下的图片路径"));
}
Project目录下右键—Create—GUI Skin可以看到所有可修改的属性
——文本字段TextField
string txt;
private void OnGUI()
{
txt = EditorGUILayout.TextField("输入文本:", txt);
}
——密码字段PasswordField
string txt;
private void OnGUI()
{
txt = EditorGUILayout.PasswordField("密码:", txt);
}
——整数字段IntField、浮点数字段FloatField
int value_int;
float value_float;
private void OnGUI()
{
value_int = EditorGUILayout.IntField("输入整数:", value_int);
value_float = EditorGUILayout.FloatField("输入浮点数:", value_float);
}
——二维向量字段Vector2Field、三维向量字段Vector3Field
private Vector3 v3;
private Vector3 v2;
private void OnGUI()
{
v3 = EditorGUILayout.Vector3Field("三维向量", v3);
v2 = EditorGUILayout.Vector2Field("二维向量", v2);
}
——物体字段ObjectField
GameObject go;
Camera camera;
private void OnGUI()
{
go = (GameObject)EditorGUILayout.ObjectField("赋值物体", go, typeof(GameObject), true);
camera = (Camera)EditorGUILayout.ObjectField("赋值相机", camera, typeof(Camera), true);
}
——颜色字段ColorField
private Color color;
private void OnGUI()
{
color = EditorGUILayout.ColorField("颜色", color);
}
——标签字段TagField
private string tag;
private void OnGUI()
{
tag = EditorGUILayout.TagField("tag", tag);
}
——层级字段LayerField
private int layer;
private void OnGUI()
{
layer = EditorGUILayout.LayerField("layer", layer);
}
——按钮Button
private void OnGUI()
{
if (GUILayout.Button("按钮", GUILayout.Width(50), GUILayout.Height(50)))
{
//按下按钮后执行的方法
}
}
——多行文本TextArea
string txt;
private void OnGUI()
{
GUILayout.BeginHorizontal();
GUILayout.Label("输入文本:",GUILayout.MaxWidth(50));
txt = EditorGUILayout.TextArea(txt, GUILayout.MaxHeight(50));
GUILayout.EndHorizontal();
}
——单选框Toggle
bool b;
private void OnGUI()
{
b = EditorGUILayout.Toggle("单选框", b);
}
——开关组
GameObject go;
string txt;
bool b;
private void OnGUI()
{
b = EditorGUILayout.BeginToggleGroup("开启区域", b);
txt = EditorGUILayout.TextField("输入文本:", txt);
go = (GameObject)EditorGUILayout.ObjectField("赋值物体", go, typeof(GameObject), true);
EditorGUILayout.EndToggleGroup();
}
——可选择文本SelectableLabel(用于需要复制的文本)
private void OnGUI()
{
EditorGUILayout.SelectableLabel("微信号:597094538");
}
——滑动条Slider、整数滑动条IntSlider
int value_int;
float value_float;
private void OnGUI()
{
value_float = EditorGUILayout.Slider("滑动条", value_float, 1, 10);
value_int = EditorGUILayout.IntSlider("滑动条", value_int, 1, 10);
}
——字符串类型的弹出选择菜单Popup
private int index = 0;
string[] values = { "value1", "value2", "value3" };
private void OnGUI()
{
index = EditorGUILayout.Popup("选择", index, values);
}
——枚举类型的弹出选择菜单Popup
public enum Value
{
Value1,
Value2,
Value3,
}
private Value value;
private void OnGUI()
{
value = (Value)EditorGUILayout.EnumPopup("选择", value);
}
——工具栏Toolbar
string[] values = { "Menu1", "Menu2", "Menu3" };
private int index = 0;
//string[] values = { "value1", "value2", "value3" };
private void OnGUI()
{
index = GUILayout.Toolbar(index, values);
if (index == 0)
{
EditorGUILayout.LabelField("this is menu1");
}
else if (index == 1)
{
EditorGUILayout.LabelField("this is menu2");
}
}
——显示提示信息
private void OnGUI()
{
if (GUILayout.Button("显示提示信息"))
{
ShowNotification(new GUIContent("this is a Notification"));
}
if (GUILayout.Button("关闭提示信息"))
{
RemoveNotification();
}
}
——显示帮助信息HelpBox
private void OnGUI()
{
EditorGUILayout.HelpBox("帮助信息", MessageType.None);
EditorGUILayout.HelpBox("警告提示", MessageType.Warning);
}
——打开本地文件夹
private string path;
private void OnGUI()
{
EditorGUILayout.TextField(path);
if (GUILayout.Button("存储路径"))
{
path = EditorUtility.SaveFolderPanel("Path to Save", path, Application.dataPath);
}
}
三:绘制一个自定义的Debug窗口
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.IO;
public class DebugWindow : EditorWindow
{
[MenuItem("Tools/Show DebugWindow")]
private static void ShowWindow()
{
GetWindow(typeof(DebugWindow), true, "Debug窗口", true);
}
int toolbarIndex;
string[] toolbarStr = { "Error反馈窗口", "Warning反馈窗口" };
bool haveError;
bool haveErrorDes;
string errorName;
GameObject errorGo;
string errorDes;
string[] errorType = { "普通", "一般", "严重" };
private int errorTypeIndex;
string errorPath;
private void OnGUI()
{
toolbarIndex = GUILayout.Toolbar(toolbarIndex, toolbarStr);
if (toolbarIndex == 0)
{
//标题标签
GUI.skin.label.fontSize = 20;
GUI.skin.label.fontStyle = FontStyle.Bold;
GUI.skin.label.alignment = TextAnchor.MiddleCenter;
GUILayout.Label("Error反馈窗口");
haveError = EditorGUILayout.Toggle("是否有Error", haveError);
if (haveError)
{
//问题名
errorName = EditorGUILayout.TextField("问题名", errorName);
if (errorName != null)
{
if (errorName.Length == 0 || errorName == "")
{
EditorGUILayout.HelpBox("问题名不能为空", MessageType.Error);
}
else if (errorName.Length >= 5)
{
EditorGUILayout.HelpBox("问题名过长", MessageType.Warning);
}
else
{
EditorGUILayout.HelpBox("问题名合法", MessageType.Info);
}
}
//出现问题的时间
EditorGUILayout.LabelField("当前时间", System.DateTime.Now.ToString());
//出现问题的场景
EditorGUILayout.LabelField("当前场景", SceneManager.GetActiveScene().name);
//出现问题的物体
errorGo = (GameObject)EditorGUILayout.ObjectField("问题物体", errorGo, typeof(GameObject), true);
//问题描述
haveErrorDes = EditorGUILayout.BeginToggleGroup("是否添加问题描述", haveErrorDes);
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("问题描述:", GUILayout.MaxWidth(50));
EditorGUILayout.TextArea(errorDes, GUILayout.MaxHeight(50));
EditorGUILayout.EndHorizontal();
EditorGUILayout.EndToggleGroup();
//问题类型
errorTypeIndex = EditorGUILayout.Popup("问题类型", errorTypeIndex, errorType);
//问题保存路径
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("选择路径"))
{
errorPath = EditorUtility.SaveFolderPanel("Path to Save", errorPath, Application.dataPath);
}
EditorGUILayout.TextField(errorPath);
EditorGUILayout.EndHorizontal();
GUILayout.Space(50);
//保存到本地
if (GUILayout.Button("保存"))
{
if (errorPath == null)
{
ShowNotification(new GUIContent("请先选择保存路径"), 1);
}
else
{
Directory.CreateDirectory(errorPath + "/BugReports");
StreamWriter sw = new StreamWriter(errorPath + "/BugReports/" + errorName + ".txt");
sw.WriteLine("Error or Warning:" + toolbarStr[toolbarIndex]);
sw.WriteLine("问题名:" + errorName);
sw.WriteLine("出现问题的时间:" + System.DateTime.Now.ToString());
sw.WriteLine("出现问题的场景:" + SceneManager.GetActiveScene().name);
sw.WriteLine("问题描述:" + errorDes);
sw.WriteLine("问题类型:" + errorType);
sw.WriteLine("问题存储路径:" + errorPath);
sw.Close();
}
}
}
}
else if (toolbarIndex == 1)
{
GUI.skin.label.fontSize = 20;
GUI.skin.label.fontStyle = FontStyle.Bold;
GUI.skin.label.alignment = TextAnchor.MiddleCenter;
GUILayout.Label("Warning反馈窗口");
//其他的与Error反馈窗口基本相同
}
}
}