Unity支持自行建立窗口,也支持自定義窗口布局。在Project視圖中建立一個Editor文件夾,在文件夾中再建立一條腳本。html
自定義窗口須要讓腳本繼承EditorWindow再設置MenuItem,此時在Unity導航菜單欄中GameObjec->window就可建立一個自定義窗口。數組
0.窗口:dom
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