unity编辑器扩展#2 GUILayout、EditorGUILayout 控件整理

4 篇文章 3 订阅

 

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

 

折叠功能

#region 折叠类

//折叠的相关控件 返回bool类型表示开、关
foldout = EditorGUILayout.Foldout(foldout, "折叠Label");
if (foldout)
{
   val1=EditorGUILayout.IntField("111", val1);
   val2=EditorGUILayout.IntField("222", val2);
}

foldout2=EditorGUILayout.InspectorTitlebar(foldout2,GameObject.Find("Cube"));
if (foldout2)
{
   val3=EditorGUILayout.IntField("333", val3);
   val4=EditorGUILayout.IntField("444", val4);
}

#endregion
 
  • 39
    点赞
  • 156
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
Unity编辑器扩展是指通过自定义脚本来扩展Unity编辑器的功能和界面。在Unity中,可以使用编辑器扩展来创建自定义的面板、工具栏、菜单项等,以便更好地支持开发者的工作流程和需求。 在编辑器扩展中,有几个重要的类需要了解。首先是Editor类,它是所有自定义编辑器的基类,通过继承Editor类可以创建自定义的Inspector面板,用于编辑特定组件的属性。其次是EditorWindow类,它是创建自定义窗口的基类,可以用于创建独立的编辑器工具窗口。然后是EditorGUI和EditorGUILayout类,它们用于在自定义面板中创建UI元素,如按钮、文本框、滑动条等。这两个类的区别在于EditorGUI需要手动计算UI元素的位置和大小,而EditorGUILayout会自动进行排版和计算坐标。此外,还有一些其他常用的类,如GUI、GUILayout、EditorGUIUtility、EditorUtility、AssetDatabase、SceneView和Handles等,它们提供了各种用于编辑器扩展的功能和工具。 使用编辑器扩展可以提高开发效率,使开发者能够更方便地进行编辑器自定义和工作流程优化。通过自定义面板和工具,可以根据项目需求添加额外的功能和工具,提供更好的用户体验和开发环境。同时,编辑器扩展也为开发者提供了更多的自由度和创造力,可以根据自己的需求进行定制和扩展。 总结起来,Unity编辑器扩展是通过自定义脚本来扩展Unity编辑器的功能和界面,可以创建自定义的面板、工具栏、菜单项等,提供更好的开发环境和用户体验。常用的类包括Editor、EditorWindow、EditorGUI、EditorGUILayout、GUI、GUILayout、EditorGUIUtility、EditorUtility、AssetDatabase、SceneView和Handles等。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值