目录
- 创建一个窗口添加简单控件
- 制作一个可以渐入渐出的窗口控件
- 最后做一个比较炫酷的窗口
创建一个窗口,添加简单控件
在Editor创建脚本GUIWindow1如下,添加一些简单的控件,体验一下他们的功能
using UnityEngine;
using UnityEditor;
public class GUIWindow1 : EditorWindow
{
// 开启一个窗口
[MenuItem ("Window/Example1")]
static void Open ()
{
GetWindow<GUIWindow1> ();
}
bool toogleVale;
void OnGUI ()
{
EditorGUI.BeginChangeCheck ();
// 绘制一个Lable
EditorGUILayout.LabelField ("Example Label");
//绘制toogle存储bool值
toogleVale = EditorGUILayout.ToggleLeft ("Toogle", toogleVale);
//检测窗口改变
if (EditorGUI.EndChangeCheck ()) {
if (toogleVale) {
Debug.Log ("toogleVale :"+toogleVale);
}
}
// 绘制一个TextField
EditorGUILayout.TextField ("toogleVale:"+toogleVale.ToString());
// 显示UI
Display ();
// 设置UI不可用
EditorGUI.BeginDisabledGroup (toogleVale);
Display ();
EditorGUI.EndDisabledGroup ();
GUI.enabled = toogleVale;
Display();
GUI.enabled = !toogleVale;
Display ();
}
void Display ()
{
// 空一行
EditorGUILayout.Space ();
EditorGUILayout.IntSlider (0, 10, 0);
GUILayout.Button ("Button");
}
}
在Unity中选择window–>Example1会打开如下窗口,点击Toggle有一些简单的禁用启用效果:
制作一个可以渐入渐出的窗口控件
功能稍微复杂一点,点击按钮可以控制控件有一个动画效果,创建代码GUIWindow2如下:
using UnityEngine;
using UnityEditor;
using UnityEditor.AnimatedValues;
using UnityEngine.Events;
public class GUIWindow2 : EditorWindow
{
[MenuItem("Window/Example2")]
static void Open ()
{
GetWindow<GUIWindow2> ();
}
// 声明一个动画浮点数
AnimFloat animFloat = new AnimFloat (0.01f);
Texture tex;
void OnGUI ()
{
bool on = animFloat.value == 1;
if (GUILayout.Button (on ? "Close" : "Open", GUILayout.Width (64))) {
animFloat.target = on ? 0.01f : 1;
animFloat.speed = 4f;
// animFloat值变化时重新绘制窗口
var env = new UnityEvent ();
env.AddListener (() => Repaint ());
animFloat.valueChanged = env;
}
// 水平排列控件
EditorGUILayout.BeginHorizontal ();
// 控制控件淡入淡出
EditorGUILayout.BeginFadeGroup (animFloat.value);
Display ();
EditorGUILayout.EndFadeGroup ();
Display ();
EditorGUILayout.EndHorizontal ();
}
bool boolValue;
void Display ()
{
// 垂直排列控件
EditorGUILayout.BeginVertical ();
EditorGUILayout.Space ();
boolValue= EditorGUILayout.ToggleLeft ("Toggle", boolValue);
var options = new []{GUILayout.Width (128), GUILayout.Height (128)};
tex = EditorGUILayout.ObjectField (
tex, typeof(Texture), false, options) as Texture;
GUILayout.Button ("Button");
EditorGUILayout.EndVertical ();
}
}
在Unity中选择window–>Example2会打开如下窗口,点击Open按钮会渐入新的控件,下面是效果图:
最后做一个比较炫酷的窗口
其实并不很炫酷啊,好吧,程序猿美感不好,创建代码GUIWindow3如下:
using UnityEngine;
using UnityEditor;
public class GUIWindow3 : EditorWindow
{
// 开启一个窗口
[MenuItem ("Window/Example3")]
static void Open ()
{
GetWindow<GUIWindow3> ();
}
float angle = 0;
bool on, one, two, three;
int selected;
GameObject go;
Material mat;
Texture texture;
void OnGUI ()
{
// 制定特定位置的GUI
EditorGUI.MultiFloatField (
new Rect (0, 0, 200, EditorGUIUtility.singleLineHeight),
new GUIContent ("Label"),
new GUIContent[] {
new GUIContent ("X"),
new GUIContent ("Y"),
new GUIContent ("Z")
},
new float[] { 0, 1, 2 });
// 空出几行不要盖上上面的GUI,还是GUILayout方便
for (int i = 0; i < 6; i++) {
EditorGUILayout.Space ();
}
// 创建object选择控件 GameObject,Material,AudioClip
go = EditorGUILayout.ObjectField (go, typeof(GameObject), false) as GameObject;
mat = EditorGUILayout.ObjectField (mat, typeof(Material), false) as Material;
EditorGUILayout.ObjectField (null, typeof(AudioClip), false);
// 做一个固定大小的Layout
var options = new []{ GUILayout.Width (64), GUILayout.Height (64) };
texture = EditorGUILayout.ObjectField (texture, typeof(Texture), false, options) as Texture;
EditorGUILayout.ObjectField (null, typeof(Sprite), false, options);
// 创建一个带有层级关系的控件
EditorGUILayout.LabelField ("Parent");
EditorGUI.indentLevel++;
for (int i = 0; i < 3; i++) {
EditorGUILayout.LabelField ("Child"+i);
//EditorGUI.indentLevel++;
}
EditorGUI.indentLevel--;
EditorGUILayout.LabelField ("Parent");
EditorGUI.indentLevel++;
for (int i = 0; i < 3; i++) {
EditorGUILayout.LabelField ("Child"+i);
//EditorGUI.indentLevel--;
}
// 创建一个拉动旋钮
angle = EditorGUILayout.Knob (Vector2.one * 100,
angle, 0, 360, "度>>上下拉动旋钮", Color.blue, Color.red, true);
// 创建一个横向排列的按钮
using (new EditorGUILayout.HorizontalScope ()) {
// 按钮的使用
if (GUILayout.Button ("Button1")) {
Debug.Log ("点击了btn1");
}
// 设置按钮颜色
using (new BackgroundColorScope (Color.green)) {
GUILayout.Button ("Button");
}
GUILayout.Button ("Button2");
using (new BackgroundColorScope (Color.yellow)) {
GUILayout.Button ("Button");
}
}
// 创建一个toggle
on = GUILayout.Toggle (on, on ? "on" : "off", "button");
// 复选框
using (new EditorGUILayout.HorizontalScope ()) {
one = GUILayout.Toggle (one, "1", EditorStyles.miniButtonLeft);
two = GUILayout.Toggle (two, "2", EditorStyles.miniButtonMid);
three = GUILayout.Toggle (three, "3", EditorStyles.miniButtonRight);
}
// 单选框
selected = GUILayout.Toolbar (selected, new string[]{ "1", "2", "3" });
selected = GUILayout.Toolbar (selected,
new string[]{ "a", "b", "c" }, EditorStyles.toolbarButton);
selected = GUILayout.SelectionGrid (selected,
new string[]{ "1", "2", "3" }, 1, "PreferencesKeysElement");
}
}
public class BackgroundColorScope : GUI.Scope
{
private readonly Color color;
public BackgroundColorScope(Color color)
{
this.color = GUI.backgroundColor;
GUI.backgroundColor = color;
}
protected override void CloseScope()
{
GUI.backgroundColor = Color.cyan;
}
}
最后上图,其实这些只是一个使用介绍,并不具有实际功能,至于用它们都能实现什么功能,还需要骚年们脑洞大开啊,上图如下: