第一步,两个创建函数:
1、EditorWindow.GetWindow(Type t,bool utility,string title,focus):可通过鼠标动态的延伸窗口。
参数:t:窗口类型,注意是一定要继承自 EditorWindow。
tility:窗口是否浮动,如果是就不能内嵌到unity其他窗口中去,如果不是就能嵌入其他窗口,默认为内嵌入式
itle:窗口的标题,如果为空的话就采用类的名称来当标题,默认类的名称
focus:是否可以获得焦点,如果再次点击会给窗口一个焦点。
2、EditorWindow.GetWindowWithRect(Type t,Rect rect,bool utility,string title,bool focus):大小固定,无法改变。
参数:t:窗口类型,注意是一定要继承自 EditorWindow。
rect:窗口的大小。
utility:窗口是否浮动,如果是就不能内嵌到unity其他窗口中去,如果不是就能嵌入其他窗口,默认为内嵌入式
title:窗口的标题,如果为空的话就采用类的名称来当标题,默认类的名称
focus:是否可以获得焦点,如果再次点击会给窗口一个焦点。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class TestTool : MonoBehaviour
{
[MenuItem("我是菜单栏的/我是第一级/新建窗口俩 _b", false, 1)]
public static void 我是第二个()
{
Window myWindow = EditorWindow.GetWindow(typeof(Window), false, "可动", true) as Window;
myWindow.Show();
Rect _rect = new Rect(0, 0, 200, 300);
Window2 window = EditorWindow.GetWindowWithRect(typeof(Window2), _rect, true, "不可动") as Window2;
window.Show();
}
}
public class Window :EditorWindow
{
}
public class Window2 : EditorWindow
{
}
第二步,编辑新窗口内容:
在继承EditorWindow的子类中,我们可以使用 GUI 和 GUILayout 控件,还可以使用编辑器专用控件 EditorGUI 和 EditorGUILayout 在 OnGUI() 回调函数中给窗口添加内容
引用一下其他人文章里的话
绘制窗口四个类:GUI、GUILayout、EditorGUI、EditorGUILayout。
GUI、EditorGUI 和 GUILayout、EditorGUILayout 的区别是:前者是固定布局,布局需要我们写代码控制,后者是自动布局。
GUI、GUILayout 和 EditorGUI、EditorGUILayout 的区别是:前者更多的用在平常调试中,后者在编辑器中使用。前者也可以使用在编辑器中
这个网上的例子太多了,我就不用自己再记忆了
public class Window :EditorWindow
{
private bool val;
private float distance = 0f;
private string text;
private string password;
private Vector3 sPoint;
private Texture texture;
public Color color = Color.red;
private void OnGUI()
{
sPoint = EditorGUILayout.Vector3Field("点:", sPoint);
text = EditorGUILayout.TextField("输入文字:", text);
if (GUILayout.Button("弹提示", GUILayout.Width(80)))
{
this.ShowNotification(new GUIContent("弹出提示信息,自动消失的那种"));
}
if (GUILayout.Button("关闭提示", GUILayout.Width(80)))
{
this.RemoveNotification();
}
//文本框
EditorGUILayout.LabelField("鼠标在窗口的位置", Event.current.mousePosition.ToString());
//选择贴图
texture = EditorGUILayout.ObjectField("添加贴图", texture, typeof(Texture), true) as Texture;
password = EditorGUI.PasswordField(new Rect(100, 30, 100, 20), password);
GUI.Label(new Rect(100, 0, 100, 30), password);
GUI.Label(new Rect(70, 30, 100, 30), "密码");
val = EditorGUILayout.Toggle("复选框", val);
color = EditorGUILayout.ColorField("颜色:", color);
}
}
第三步,EditorWindow里面的一些回调
private void OnFocus()
{
Debug.Log("当窗口获得焦点时调用一次");
}
private void OnLostFocus()
{
Debug.Log("当窗口丢失焦点时调用一次");
}
private void OnHierarchyChange()
{
Debug.Log("当Hierarchy视图中的任何对象发生改变时调用一次");
}
private void OnProjectChange()
{
Debug.Log("当Project视图中的资源发生改变时调用一次");
}
private void OnInspectorUpdate()
{
Debug.Log("窗口面板的更新");
//这里开启窗口的重绘,不然窗口信息不会刷新
this.Repaint();
}
private void OnSelectionChange()
{
//当窗口处于开启状态,并且在Hierarchy视图中选择某游戏对象时调用
foreach (Transform t in Selection.transforms)
{
//有可能是多选,这里开启一个循环打印选中游戏对象的名称
Debug.Log("OnSelectionChange" + t.name);
}
}
private void OnDestroy()
{
Debug.Log("当窗口关闭时调用");
}