unity編輯器擴充套件#2 GUILayout、EditorGUILayout 控制元件整理
阿新 • • 發佈:2018-12-06
GUILayout
裝飾類控制元件:
GUILayout.FlexibleSpace();
GUILayout.Space(100);//空格,沒什麼好說的
GUILayout.Label("label");
GUILayout.Box(new GUIContent("一個200x200的BOX"),new []{GUILayout.Height(200),GUILayout.Width(200)});
GUILayoutOption:基本每個控制元件方法都有一個可選引數是GUILayoutOption[] Options 這是一個可以控制組件大小之類的選項,在GUILayout類中共有8個。 看名字應該就知道是設定什麼的了。
GUILayout.Height() GUILayout.Width()
GUILayout.MaxHeight() GUILayout.MaxWidth()
GUILayout.MinHeight() GUILayout.MinWidth()
GUILayout.ExpandHeight() GUILayout.ExpandWidth()
滾/滑動類控制元件:
num = EditorGUILayout.Slider ("Slider", num, -3, 3);
scrollBarValue1 = GUILayout.HorizontalScrollbar(scrollBarValue1,0,0,100,new[]{GUILayout.Width(100)});
scrollBarValue2 = GUILayout.VerticalScrollbar(scrollBarValue2,0,0,100,new[]{GUILayout.Height(100)});
scrollValue1=GUILayout.HorizontalSlider(scrollValue1, 0, 100); scrollValue2=GUILayout.VerticalSlider(scrollValue2, 0, 100);
按鈕類控制元件
bool a=GUILayout.Button("一個Button");
bool b= GUILayout.RepeatButton("RepeatButton");
//RepeatButton 在按著的時候一直返回true,鬆開才返回false
gridId = GUILayout.SelectionGrid(gridId, new[] {"1", "2", "3","4","5","6"}, 4);
toolbarid=GUILayout.Toolbar(toolbarid, new[] {"1", "2", "3"});
Field類控制元件
GUILayout.TextField("TextField只能一行");
GUILayout.TextArea("TextArea可以多行\n 第二行");
password = GUILayout.PasswordField(password, '*');
Toggle類控制元件
isToggle = GUILayout.Toggle (isToggle,"Toggle");
區塊佈局類控制元件
//保證塊裡寫的控制元件只在Area規定的範圍內,這裡設定放到右下角
GUILayout.BeginArea(new Rect(position.width-250,position.height-150,250,150));
//...
GUILayout.EndArea();
GUILayout.BeginHorizontal();
//...水平佈局
GUILayout.EndHorizontal();
GUILayout.BeginVertical();
//...垂直佈局
GUILayout.EndVertical();
//顯示的範圍不夠塊內控制元件時可以滑動 這裡限制高度75最多同時顯示3個
scrollPos=GUILayout.BeginScrollView(scrollPos,false,true,GUILayout.Height(75));
//...
GUILayout.EndScrollView();
groupEnabled = EditorGUILayout.BeginToggleGroup ("ToogleGroup",groupEnabled);
//...
EditorGUILayout.EndToggleGroup ();
EditorGUILayout
ps:部分和GUILayout重複的沒有加進來
Slider類控制元件
//可以選擇一個最大最小的範圍的slider
EditorGUILayout.MinMaxSlider("MinMaxSlider",ref sliderValue2,ref sliderValue3,10,20);
//int型別的slider
intSliderValue=EditorGUILayout.IntSlider(intSliderValue,0,10);
//和guilayout重複的
sliderValue=EditorGUILayout.Slider(sliderValue,0f,1);
彈選框類控制元件
//這個可以多選 需要列舉型別
flagtype=(flagTestType)EditorGUILayout.EnumFlagsField(flagtype);
//這個單選 需要列舉型別
popuptype=(popupTestType)EditorGUILayout.EnumPopup(popuptype);
//一個選擇框,每個選擇框裡表示一個Int數
popupindex=EditorGUILayout.IntPopup("IntPopup", popupindex, new[] {"a", "b"}, new[] {1, 2});
//和IntPopup類似但是可以多選
maskindex=EditorGUILayout.MaskField("Mask",maskindex,new []{"a","b"});
按鈕類控制元件
//正常按鈕在滑鼠按鍵擡起MouseUp時返回true,他在MouseDown時就立即返回true
if (EditorGUILayout.DropdownButton(new GUIContent("DropdownButton"),FocusType.Keyboard))
{
Debug.Log("滑鼠按下時出現");
}
Toggle類控制元件
EditorGUILayout.Toggle("toggle",false);
EditorGUILayout.ToggleLeft("ToggleLeft",true);
Field類控制元件
i1=EditorGUILayout.IntField("IntField",i1,GUILayout.Width(100));
d1=EditorGUILayout.DoubleField("DoubleField",d1,GUILayout.Width(100));
//bounds型別輸入框 反正就兩個三維向量
boundsv=EditorGUILayout.BoundsField("BoundsField",boundsv);
boundintv=EditorGUILayout.BoundsIntField("BoundsIntField",boundintv);
//層級和標籤\物體選擇,就是unity中的各種layer tag gameboject
EditorGUILayout.LayerField("LayerField", 1);
EditorGUILayout.TagField("TagField", "一個tag");
EditorGUILayout.ObjectField("ObjectField",GameObject.Find("Cube"), typeof(GameObject),true);
//顯示序列化屬性欄位 比如之前的自定義特性裡的
EditorGUILayout.PropertyField(p)
EditorGUILayout.RectField(new Rect());
EditorGUILayout.RectIntField(new RectInt());
//delay和一般的區別是隻有按下回車或者失去焦點時,才會返回值,就是說你再輸入時不會返回
//Note that the return value will not change until the user has pressed enter or focus is moved away from the text field.
delayint=EditorGUILayout.DelayedIntField("DelayedInt",delayint);
delayfloat=EditorGUILayout.DelayedFloatField("DelayedFloat",delayfloat);
delaydouble=EditorGUILayout.DelayedDoubleField("DelayedDouble",delaydouble);
delaystr=EditorGUILayout.DelayedTextField("DelayedTextField",delaystr);
colorv=EditorGUILayout.ColorField("顏色框", colorv);
acurev=EditorGUILayout.CurveField("曲線", acurev);
//還有這幾個向量
//EditorGUILayout.Vector2Field();
//EditorGUILayout.Vector2IntField();
//EditorGUILayout.Vector3Field();
//EditorGUILayout.Vector3IntField();
//EditorGUILayout.Vector4Field()
其他控制元件
knob=EditorGUILayout.Knob(new Vector2(100, 100), knob, 1, 10, "斤", Color.black, Color.blue, true);
EditorGUILayout.HelpBox("helpBox,這裡寫一些提示、警告、錯誤", MessageType.None);
//不知道幹嘛的,字面意思是分離
EditorGUILayout.Separator();
EditorGUILayout.Space();
EditorGUILayout.LabelField("labelField");
EditorGUILayout.PrefixLabel("PrefixLabel");
//點選變藍的label 可以被選擇和複製
EditorGUILayout.SelectableLabel("SelectableLabel");
區塊佈局類控制元件
//用於解決EditorGUILayout和EditorGUI(或者GUI)混著用的情況
//這樣確保自動佈局不會亂,不會疊在一起
Rect a=EditorGUILayout.GetControlRect();
EditorGUI.LabelField(a,"一個label");
if (EditorGUILayout.BeginFadeGroup(Value))
{
//...ps:這個應該用於開關的特效,因為Value的值不是0或1時,會讓下面所有的控制元件都無法互動
}
EditorGUILayout.EndFadeGroup();