标签:
Unity支持自行创建窗口,也支持自定义窗口布局。在Project视图中创建一个Editor文件夹,在文件夹中再创建一条脚本。
自定义窗口需要让脚本继承EditorWindow再设置MenuItem,此时在Unity导航菜单栏中GameObjec->window就可创建一个自定义窗口。
0.窗口:
using UnityEngine;
using UnityEditor;//引入编辑器命名空间
publicclassMyEditor:EditorWindow
{
[MenuItem("GameObject/caymanwindow")]
staticvoidAddWindow()
{
//创建窗口
Rect wr =newRect(0,0,500,500);
//另一种方式:myEditor window = (myEditor)EditorWindow.GetWindow(typeof(myEditor), true, "cayman");
MyEditor window =(MyEditor)EditorWindow.GetWindowWithRect(typeof(MyEditor),wr,true,"widow name");
window.Show();
}
//[MenuItem("GameObject/caymanwindow", true)] //如果没有选择物体,禁用菜单
// static bool ValidateSelection() {
// return Selection.activeGameObject != null;
// }
}
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();//实时刷新
}
}

//如果开关控件被选择,显示一个按钮。 bool showBtn =true; voidOnGUI() { showBtn =EditorGUILayout.Toggle("Show Button", showBtn); if(showBtn) { if(GUILayout.Button("Close")) this.Close(); } }
//通过字段,自动改变选择物体的名字 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();//实时刷新 } }

//在编辑器窗口可视化脚本,这可扩展保存脚本 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; } }

string text="123"; voidOnGUI() { EditorGUILayout.SelectableLabel(text); //文本:可以选择然后复制粘贴 }
//创建密码字段并可视化在密码字段有什么键入。 string text ="Some text here"; function OnGUI(){ text =EditorGUILayout.PasswordField("Type Something:",text); EditorGUILayout.LabelField("Written Text:", text); } }

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);
}
}

//缩放选择的游戏物体,在1-100之间
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."); }

string[] options ={"Cube","Sphere","Plane"}; int index =0; voidOnGUI() { index =EditorGUILayout.Popup(index, options); if(GUILayout.Button("Create")) InstantiatePrimitive(); } voidInstantiatePrimitive(){ switch(index){ case0: GameObject cube=GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.position =Vector3.zero; break; case1: GameObject sphere=GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.transform.position =Vector3.zero; break; case2: GameObject plane=GameObject.CreatePrimitive(PrimitiveType.Plane); plane.transform.position =Vector3.zero; break; default: Debug.LogError("Unrecognized Option"); break; } } }

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); } }
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."); }

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; }


Object source; Texture myme; voidOnGUI() { EditorGUILayout.BeginHorizontal(); source =EditorGUILayout.ObjectField("hiahia",source,typeof(Object)); myme= (Texture)EditorGUILayout.ObjectField("hehe",myme,typeof(Texture));//注意类型转换 EditorGUILayout.EndHorizontal(); }

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(); }

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; }


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,"widown name"); window.Show();}//输入文字的内容privatestring text;//选择贴图的对象privateTexture texture;float myFloat =1.23f;private bool kaiguan;//开关private bool groupEnabled;//区域开关publicvoidAwake(){//在资源中读取一张贴图 texture =Resources.Load("1")asTexture;}//绘制窗口时调用voidOnGUI(){//输入框控件 text =EditorGUILayout.TextField("输入文字:",text);//3.制作一个文本字段if(GUILayout.Button("打开通知",GUILayout.Width(200))){//打开一个通知栏this.ShowNotification(newGUIContent("This is a Notification"));}if(GUILayout.Button("关闭通知",GUILayout.Width(200))){//关闭通知栏this.RemoveNotification();}//文本框显示鼠标在窗口的位置EditorGUILayout.LabelField("鼠标在窗口的位置",Event.current.mousePosition.ToString());//1.制作一个标签字段(通常用于显示只读信息)showBtn = EditorGUILayout.Toggle("开关", showBtn);
//选择贴图 texture =EditorGUILayout.ObjectField("添加贴图",texture,typeof(Texture),true)asTexture; groupEnabled =EditorGUILayout.BeginToggleGroup("Optional Settings", groupEnabled);//起始----------------------------//这里放开关区域内内容 myFloat =EditorGUILayout.Slider("Slider", myFloat,-3,3);//滑动条 kaiguan=EditorGUILayout.Toggle("开关", kaiguan);//2.开关EditorGUILayout.EndToggleGroup();//结束-------------------------------------if(GUILayout.Button("关闭窗口",GUILayout.Width(200))){//关闭窗口this.Close();}}voidOnFocus(){Debug.Log("当窗口获得焦点时调用一次");}voidOnLostFocus(){Debug.Log("当窗口丢失焦点时调用一次");}voidOnHierarchyChange(){Debug.Log("当Hierarchy视图中的任何对象发生改变时调用一次");}voidOnProjectChange(){Debug.Log("当Project视图中的资源发生改变时调用一次");}voidOnInspectorUpdate()//实时刷新面板{//Debug.Log("窗口面板的更新");//这里开启窗口的重绘,不然窗口信息不会刷新this.Repaint();}voidOnSelectionChange(){//当窗口出去开启状态,并且在Hierarchy视图中选择某游戏对象时调用foreach(Transform t inSelection.transforms){//有可能是多选,这里开启一个循环打印选中游戏对象的名称Debug.Log("OnSelectionChange"+ t.name);}}voidOnDestroy(){Debug.Log("当窗口关闭时调用");}}//http://www.ceeger.com/Script/EditorGUILayout/EditorGUILayout.html
标签:
原文地址:http://www.cnblogs.com/caymanlu/p/5722549.html