近段时间在学习扩展unity编辑器窗口
Unity支持自行创建窗口,也支持自定义窗口布局。在Project视图中创建一个Editor文件夹,在文件夹中再创建一条脚本。
自定义窗口需要让脚本继承EditorWindow再设置MenuItem,此时在Unity导航菜单栏中GameObjec->window就可创建一个自定义窗口。
using UnityEngine;
using UnityEditor;
publicclassMyEditor:EditorWindow
{
[MenuItem("GameObject/caymanwindow")]
staticvoidAddWindow()
{
Rect wr =newRect(0,0,500,500);
MyEditor window =(MyEditor)EditorWindow.GetWindowWithRect(typeof(MyEditor),wr,true,"widow name");
window.Show();
}
}
1.LabelField制作一个标签字段(通常用于显示只读信息) LabelField(string label1,string label2,GUILayoutOption[] options) //参数:label1标签字段前面的标签 label2显示在右侧的标签 options额外布局属性可选列表 --无返回值
publicclass myEditor3 :EditorWindow{
[MenuItem("cayman/tempShow")]
staticvoid newWelcome()
{
myEditor3 window3 =(myEditor3)EditorWindow.GetWindow(typeof(myEditor3),true,"Eam");
window3.Show();
}
voidOnGUI()
{
EditorGUILayout.LabelField("Time since start: ",EditorApplication.timeSinceStartup.ToString());
this.Repaint();
}
}
效果:
2.Toggle开关按钮 Toggle(bool value,GUILayoutOption[] options) Toggle(string label,bool value,GUILayoutOption[] options) //参数:label开关按钮前面的可选标签 value开关按钮的显示状态 options额外布局属性的可选列表 //返回:bool,开关按钮的选择状态
bool showBtn =true;
voidOnGUI()
{
showBtn =EditorGUILayout.Toggle("Show Button", showBtn);
if(showBtn)
{
if(GUILayout.Button("Close"))
this.Close();
}
}
效果:
3.TextField文本字段 TextField(string text,GUILayoutOption[] options) TextField(string label,string text,GUIStyle style,GUILayoutOption[] options) TextField(string text,GUIStyle style,GUILayoutOption[] options) TextField(GUIContent label,string text,GUILayoutOption[] options) TextField(string label,string text,GUILayoutOption[] options) TextField(GUIContent label,string text,GUIStyle style,GUILayoutOption[] options) //参数:label可选标签 text编辑的文本 style可选样式 options额外布局属性的可选列表 //返回:string,用户输入的文本 复制代码 //通过字段,自动改变选择物体的名字
string objectName ="";
voidOnGUI()
{
GUILayout.Label("Select an object in the hierarchy view");
if(Selection.activeGameObject)
Selection.activeGameObject.name =EditorGUILayout.TextField("Object Name: ",Selection.activeGameObject.name);
this.Repaint();
}
}
效果:
4.TextArea文本区域 TextArea(string text,GUILayoutOption[] options) TextArea(string text,GUIStyle style,GUILayoutOption[] options) //参数:text可编辑的文本 style可选样式 options额外布局属性的可选列表 //返回:string,用户输入的文本 //在编辑器窗口可视化脚本,这可扩展保存脚本
string text ="Nothing Opened...";
TextAsset txtAsset;
Vector2 scroll;
voidOnGUI()
{
TextAsset newTxtAsset =EditorGUILayout.ObjectField("添加", txtAsset,typeof(TextAsset),true)asTextAsset;
if(newTxtAsset != txtAsset)
ReadTextAsset(newTxtAsset);
scroll =EditorGUILayout.BeginScrollView(scroll);
text =EditorGUILayout.TextArea(text,GUILayout.Height(position.height -30));
EditorGUILayout.EndScrollView();
}
voidReadTextAsset(TextAsset txt){
text = txt.text;
txtAsset = txt;
}
}
效果:
5.SelectableLabel 可选择标签(通常用于显示只读信息,可以被复制粘贴) SelectableLabel(string text,GUILayoutOption[] options) SelectableLabel(string text,GUIStyle style,GUILayoutOption[] options) //参数:text显示的文本 style可选样式 options额外布局属性的可选列表 无返回值
string text="123";
voidOnGUI()
{
EditorGUILayout.SelectableLabel(text);
}
6.PasswordField 密码字段 PasswordField (string password, GUILayoutOption[] options) PasswordField (string password,GUIStyle style, GUILayoutOption[] options) PasswordField (string label,string password,GUILayoutOption[] options) PasswordField (string password,GUIStyle style,GUILayoutOption[] options) PasswordField (GUIContent label,string password, GUILayoutOption[] options) PasswordField (GUIContent label,string password,GUIStyle style,GUILayoutOption[] options) //参数:label开关按钮前面的可选标签 password编辑的密码 style可选样式 options指定额外布局属性的可选列表 //返回:string,用户输入的密码
string text ="Some text here";
function OnGUI(){
text =EditorGUILayout.PasswordField("Type Something:",text);
EditorGUILayout.LabelField("Written Text:", text);
}
}
效果:
7.制作一个文本字段用于输入小数/整数。 FloatField 浮点数字段:返回小数,由用户输入的值 IntField 整数字段:返回整数,由用户输入的值
int clones=1;
voidOnGUI(){
clones=EditorGUILayout.IntField("Number of clones:", clones);
if(GUILayout.Button("Clone!"))
for(var i =0; i < clones; i++)
Instantiate(Selection.activeGameObject,Vector3.zero,Quaternion.identity);
}
}
效果
8.Slider 滑动条 –IntSlider 整数滑动条 MinMaxSlider 最小最大滑动条 Slider(float leftValue,float rightValue,GUILayoutOption[] options) Slider(string label,float leftValue,float rightValue,GUILayoutOption[] options) Slider(GUIContent label,float value,float leftValue,float rightValue,GUILayoutOption[] options) //参数:label开关按钮前的可选标签 value编辑的值 leftValue滑动条最左边的值 rightValue滑动条最右边的值 options。。。 //返回:float,由用户设置的值
float scale =0.0f;
voidOnGUI()
{
scale =EditorGUILayout.Slider(scale,1,100);
}
voidOnInspectorUpdate()
{
if(Selection.activeTransform)
Selection.activeTransform.localScale =newVector3(scale, scale, scale);
}
float minVal =-10.0f;
float minLimit =-20.0f;
float maxVal =10.0f;
float maxLimit =20.0f;
voidOnGUI()
{
EditorGUILayout.LabelField("Min Val:", minVal.ToString());
EditorGUILayout.LabelField("Max Val:", maxVal.ToString());
EditorGUILayout.MinMaxSlider(ref minVal,ref maxVal, minLimit, maxLimit);
if(GUILayout.Button("Move!"))
PlaceRandomly();
}
voidPlaceRandomly()
{
if(Selection.activeTransform)
Selection.activeTransform.position =
newVector3(Random.Range(minVal, maxVal),
Random.Range(minVal, maxVal),
Random.Range(minVal, maxVal));
else
Debug.LogError("Select a GameObject to randomize its position.");
}
效果:
9.Popup弹出选择菜单 Popup(int selectedIndex,string[] displayOptions,GUILayoutOption[] paroptions) Popup(int selectedIndex,string[] displayOptions,GUIStyle style,GUILayoutOption[] paroptions) Popup(string label,int selectedIndex,string[] displayOptions,GUILayoutOption[] paroptions) Popup(GUIContent label,int selectedIndex,string[] displayOptions,GUILayoutOption[] paroptions)。。。。 //参数:label字段前面可选标签 selectedIndex字段选项的索引 displayedOptions弹出菜单选项的数组 style可选样式 options。。 //返回:int,用户选择的选项索引
string[] options ={"Cube","Sphere","Plane"};
int index =0;
voidOnGUI()
{
index =EditorGUILayout.Popup(index, options);
if(GUILayout.Button("Create"))
InstantiatePrimitive();
}
void InstantiatePrimitive()
{
switch (index)
{
case 0:
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = Vector3.zero;
break;
case 1:
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = Vector3.zero;
break;
case 2:
GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
plane.transform.position = Vector3.zero;
break;
default:
Debug.LogError("Unrecognized Option");
break;
}
}
效果:
10.EnumPopup 枚举弹出选择菜单(效果同上) //返回System.Enum,用户选择的枚举选项。 复制代码
enum OPTIONS
{
CUBE =0,
SPHERE =1,
PLANE =2
}
publicclass myEditor3 :EditorWindow{
OPTIONS op=OPTIONS.CUBE;
[MenuItem("cayman/tempShow")]
staticvoid newWelcome()
{
myEditor3 window3 =(myEditor3)EditorWindow.GetWindow(typeof(myEditor3),true,"Eam");
window3.Show();
}
voidOnGUI()
{
op =(OPTIONS)EditorGUILayout.EnumPopup("Primitive to create:", op);
}
}
11.IntPopup 整数弹出选择菜单 IntPopup(string label,int selectedValue,string[] displayOptions,int[] optionValues,GUIStyle style,GUILayoutOption[] paramsOptions)… //参数:label字段前面的可选标签 selectedValue字段选项的索引 displayOptions弹出菜单項数组 optionValues每个选项带有值的数组。。 //返回:int,用户选择的选项的值
int selectedSize =1;
string[] names ={"Normal","Double","Quadruple"};
int[] sizes ={1,2,4};
voidOnGUI()
{
selectedSize =EditorGUILayout.IntPopup("Resize Scale: ", selectedSize, names, sizes);
if(GUILayout.Button("Scale"))
ReScale();
}
voidReScale()
{
if(Selection.activeTransform)
Selection.activeTransform.localScale =newVector3(selectedSize, selectedSize, selectedSize);
elseDebug.LogError("No Object selected, please select an object to scale.");
}
效果:
12.TagField 标签字段 LayerField层字段 1/ TagField(string label,string tag,GUIStyle style,GUILayoutOption[] paramsOptions)… //参数:label字段前面的可选标签 tag显示字段的标签 。。 //返回:string,用户选择的标签 2/ LayerField(string label,int layer,GUIStyle style,GUILayoutOption[] paramsOptions)… 参数:label字段前面的可选标签 layer显示在该字段的层。。 //返回:int,用户选择的层 复制代码
string tagStr ="";
int selectedLayer=0;
voidOnGUI()
{
tagStr =EditorGUILayout.TagField("Tag for Objects:", tagStr);
if(GUILayout.Button("Set Tag!"))
SetTags();
if(GUILayout.Button("Set Layer!"))
SetLayer();
}
voidSetTags(){
foreach(GameObject go inSelection.gameObjects)
go.tag = tagStr;
}
voidSetLayer(){
foreach(GameObject go inSelection.gameObjects)
go.laye= tagStr;
}
效果:
13.ObjectField 物体字段(拖拽物体或拾取器选择物体) ObjectField(string label,Object obj,Type objType,bool allowSceneObjects,GUILayoutOption[] paramsOptions) //label字段前面的可选标签 obj字段显示的物体 objType物体的类型 allowSceneObjects允许指定场景物体… //返回:Object,用户设置的物体
Object source;
Texture myme;
voidOnGUI()
{
EditorGUILayout.BeginHorizontal();
source =EditorGUILayout.ObjectField("hiahia",source,typeof(Object));
myme= (Texture)EditorGUILayout.ObjectField("hehe",myme,typeof(Texture));
EditorGUILayout.EndHorizontal();
}
效果:
14.Vector2Field 二维向量字段 Vector3Field 三维向量字段(略,同2维) Vector2Field (string label,Vector2 value,GUILayoutOption[] options) //参数:label字段前面的可选标签 value编辑的值 options… //返回:Vector2,由用户输入的值
float distance =0;
Vector2 p1, p2;
voidOnGUI()
{
p1 =EditorGUILayout.Vector2Field("Point 1:", p1);
p2 =EditorGUILayout.Vector2Field("Point 2:", p2);
EditorGUILayout.LabelField("Distance:", distance.ToString());
}
voidOnInspectorUpdate()
{
distance =Vector2.Distance(p1, p2);
this.Repaint();
}
效果:
15.ColorField 颜色字段 ColorField (string label,Color value,…) //参数:label字段前面的可选标签 value编辑的值 //返回:Color,由用户输入的值
Color matColor =Color.white;
voidOnGUI()
{
matColor =EditorGUILayout.ColorField("New Color", matColor);
if(GUILayout.Button("Change!"))
ChangeColors();
}
voidChangeColors(){
if(Selection.activeGameObject)
foreach(var t inSelection.gameObjects)
if(t.GetComponent<Renderer>())
t.GetComponent<Renderer>().sharedMaterial.color = matColor;
}
效果:
|