赞
踩
每GUILayout控件或EditorGUILayout控件都有这个重载接口,通常在倒数第一个参数,可以是传入一个数组,也可以是单独一个:
// 数组模式
GUILayout.Label("测试1", new []{GUILayout.Width(100), GUILayout.Height(100)})
// 单个
GUILayout.Label("测试2", GUILayout.Width(100))
根据自己的需求选择使用哪种重载方式
每个空间的重载方式中都会有一个带有GUIStyle的参数,通常在接口的倒数第二个参数,使用时我们可以new一个GUIStyle对象出来设置,也可以使用Unity内置的样式字符串进行设置;
Unity有一套内置的GUIStyle集合可以进行参考:
public class GUIStyleViewer : EditorWindow { Vector2 scvPos = Vector2.zero; string search = ""; private static GUIStyleViewer window; [MenuItem("Demo/GUIStyleViewer", false, 100)] public static void OpenWindow() { window = GetWindow<GUIStyleViewer>(true, "Unity内置GUIStyle查看器"); } public void OnGUI() { GUILayout.BeginHorizontal("HelpBox"); GUILayout.Space(30); search = EditorGUILayout.TextField("", search, "SearchTextField", GUILayout.MaxWidth(position.x)); GUILayout.Label("", "SearchCancelButtonEmpty"); GUILayout.EndHorizontal(); scvPos = GUILayout.BeginScrollView(scvPos); foreach (GUIStyle style in GUI.skin.customStyles) { if (style.name.ToLower().Contains(search.ToLower())) { DrawStyleItem(style); } } GUILayout.EndScrollView(); } void DrawStyleItem(GUIStyle style) { GUILayout.BeginHorizontal("box"); GUILayout.Space(40); EditorGUILayout.SelectableLabel(style.name); GUILayout.Box("", style, GUILayout.Width(100)); GUILayout.FlexibleSpace(); if (GUILayout.Button("复制到剪贴板")) { EditorGUIUtility.systemCopyBuffer = style.name; } GUILayout.EndHorizontal(); GUILayout.Space(10); } }
代码参考:https://www.jianshu.com/p/6571b70f3ab6
通常情况下我们编写的控件代码都是从上往下按顺序排列显示的(前提是你没有使用Rect来控制控件的显示),而布局控件则是可以让你的控件横向排列或者纵向排列:
EditorGUILayout.BeginVertical();
EditorGUILayout.LabelField("Label 标签1", " - Label");
EditorGUILayout.LabelField("Label 标签2", " - Label");
EditorGUILayout.EndVertical();
// 使用布局控件
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Label 标签1", " - Label");
EditorGUILayout.LabelField("Label 标签2", " - Label");
EditorGUILayout.EndHorizontal();
这两个是成对出现的,有开始就有结束,在这两个函数范围内的控件都将受到影响
public float fadeGroupValue = 0; public void OnGUI(){ // FadeGroup 组件由一个float值进行控制, // value 的取值范围为 0~1 // value = 0的时候,组内的所有内容不会显示 // value = 1的时候,组内的所有内容都会显示 // 组件的范围会跟随value的变化而变化,如果底部有其他控件,这个控件会跟随紧贴组的底部 fadeGroupValue = EditorGUILayout.Slider("控制FadeGroup:", fadeGroupValue, 0, 1); if (EditorGUILayout.BeginFadeGroup(fadeGroupValue)) { GUILayout.Label("文本1"); GUILayout.Label("文本2"); GUILayout.Label("文本3"); GUILayout.Label("文本4"); GUILayout.Label("文本5"); GUILayout.Label("文本6"); } EditorGUILayout.EndFadeGroup(); GUILayout.Label("底部"); }
这两个是成对出现的,有开始就有结束,在这两个函数范围内的控件都将受到影响
// 滚动界面位置,x代表横向位置,y代表纵向位置
public Vector2 scrollViewPos = Vector2.zero;
public void OnGUI(){
// isAlwaysHor/isAlwaysVer 默认为false
// 有其他重载接口没有这两个值
// 滚动条的样式可以自定义,通过使用GUIStyle类
scrollViewPos = EditorGUILayout.BeginScrollView(scrollViewPos, true, true);
EditorGUILayout.LabelField("**1********2*********3**");
EditorGUILayout.LabelField("**1********2*********3**---------------");
EditorGUILayout.EndScrollView();
}
Unity必须有对应的目标平台,否则只会显示默认的平台
public BuildTargetGroup buildTargetGroup = BuildTargetGroup.Standalone;
public void OnGUI(){
buildTargetGroup = EditorGUILayout.BeginBuildTargetSelectionGrouping();
if (buildTargetGroup == BuildTargetGroup.Standalone) { EditorGUILayout.LabelField("自定义默认设置"); }
if (buildTargetGroup == BuildTargetGroup.Android) { EditorGUILayout.LabelField("自定义Android设置"); }
EditorGUILayout.EndBuildTargetSelectionGrouping();
}
public bool foldoutState = false; public bool inspactorFoldoutState = true; public Transform selectTransform = null; public bool foldoutHeaderState = true; public Transform selectTransform_Header = null; public string label_Header = "Option"; public void OnGUI(){ // 普通折叠栏(不可以添加菜单) foldoutState = EditorGUILayout.Foldout(foldoutState, "折叠栏", true); if (foldoutState) { EditorGUILayout.LabelField("---------------文本1"); EditorGUILayout.LabelField("---------------文本2"); } // 将所选GameObject上的某个组件的检视面板拿到自定义的窗口中显示 GameObject obj = Selection.activeGameObject; if (obj) { selectTransform = obj.transform; inspactorFoldoutState = EditorGUILayout.InspectorTitlebar(inspactorFoldoutState, selectTransform); if (inspactorFoldoutState) { selectTransform.position = EditorGUILayout.Vector3Field("Position:", selectTransform.position); } } // 可以在折叠栏中设置菜单栏,添加菜单选项做特殊操作 foldoutHeaderState = EditorGUILayout.BeginFoldoutHeaderGroup(foldoutHeaderState, label_Header, null, FoldoutHeaderGroupMenuList); if (obj) { selectTransform_Header = obj.transform; if (foldoutHeaderState) { EditorGUILayout.Vector3Field("Position:", selectTransform_Header.position); } } else { foldoutHeaderState = false; label_Header = "请选择一个GameObject"; } EditorGUILayout.EndFoldoutHeaderGroup(); } // 菜单 public void FoldoutHeaderGroupMenuList(Rect rect) { var menu = new GenericMenu(); menu.AddItem(new GUIContent("Reset"), false, OnItemClicked); menu.DropDown(rect); } // 菜单点击事件 public void OnItemClicked() { if (Selection.activeTransform == null) return; Undo.RecordObject(Selection.activeTransform, "Moving to center of world"); Selection.activeTransform.position = Vector3.zero; }
public void OnGUI(){
GUILayout.BeginArea(new Rect(position.width - 100, position.height - 100, 100, 100));
GUILayout.Box("测试Box", new[] { GUILayout.Width(100), GUILayout.Height(100)});
GUILayout.EndArea();
}
EditorGUILayout.LabelField("Label 标签");
EditorGUILayout.LabelField("Label 标签", "标签内容");
EditorGUILayout.SelectableLabel("可复制的只读文本内容。");
public string tempFiled = "";
public string tempArea = "";
public string passwordFiled = "";
public void OnGUI(){
tempFiled = EditorGUILayout.TextField("单行输入:", tempFiled);
tempArea = EditorGUILayout.TextArea(tempArea);
passwordFiled = EditorGUILayout.PasswordField("密码输入:", passwordFiled);
}
public int intFiled = 0; public float floatFiled = 0.0f; public double doubleFiled = 0; public Vector3 vector3Filed = Vector3.zero; public Vector3Int vector3IntFiled = Vector3Int.zero; public Vector2 vector2Filed = Vector2.zero; public Vector2Int vector2IntFiled = Vector2Int.zero; public Vector4 vector4Filed = Vector4.zero; public Bounds boundFiled = new Bounds(Vector3.zero, Vector3.zero); public BoundsInt boundIntFiled = new BoundsInt(Vector3Int.zero, Vector3Int.zero); public void OnGUI(){ intFiled = EditorGUILayout.IntField("输入一个整数:", intFiled); floatFiled = EditorGUILayout.FloatField("输入一个Float:", floatFiled); doubleFiled = EditorGUILayout.DoubleField("输入一个Double:", doubleFiled); vector3Filed = EditorGUILayout.Vector3Field("输入一个3维向量:", vector3Filed); vector3IntFiled = EditorGUILayout.Vector3IntField("输入一个3维整数向量:", vector3IntFiled); vector2Filed = EditorGUILayout.Vector2Field("输入一个2维向量:", vector2Filed); vector2IntFiled = EditorGUILayout.Vector2IntField("输入一个2维整数向量:", vector2IntFiled); vector4Filed = EditorGUILayout.Vector4Field("输入一个4维向量:", vector4Filed); boundFiled = EditorGUILayout.BoundsField("输入一个包围盒:", boundFiled); boundIntFiled = EditorGUILayout.BoundsIntField("输入一个整数包围盒:", boundIntFiled); }
public enum TempEnum { None = 0, // 0 表示全都不选,这里可以自定义名称(覆盖None) Everything = ~0, // ~0 表示全选,这里可以自定义名称(覆盖Everything) OneAndTwo = One|Two, // 如果需要一个选项包含两个/多个选项 名称 | 名称... (中间用|间隔) One = 1<<0, // 普通枚举 - 从 0 开始,1左移: 1<<索引 Two = 1<<1, Three = 1<<2, } public TempEnum enumFlagsFiled = TempEnum.One; public void OnGUI(){ //返回值不是纯粹的 枚举值, 而是一个二进制数, //我的枚举共 6 项,8位二进制数可以代表我的返回值 //选择 None 返回值为 0000 0000 //选择 Everything 返回值为 1111 1111 //选择 One 返回值为 0000 0001 //选择 Two 返回值为 0000 0010 //同时选择One, Two 或选择 OneAndTwo 返回值为 0000 0011 enumFlagsFiled = (TempEnum)EditorGUILayout.EnumFlagsField("枚举类型多选下拉框:", enumFlagsFiled); } ... // 多选框如何取选择的值(组合值/=0的值/=~0的值都不会被计算进索引,所以这里严格来说只有3个枚举) for (int i = 0; i < 3; i++) { int temp = 1 << i; // 先对索引值进行左移 if (((int)enumFlagsFiled & temp) == temp) // 拿当前值与索引进行按位与,如果结果与索引值相同,则表示被选中了 { Debug.LogError("被选中 = " + i); } else { Debug.Log("没有被选中 = " + i); } }
public enum TempEnum2
{
One = 0,
Two = 1,
Three = 2,
}
public TempEnum2 enumPopup = TempEnum2.One;
public void OnGUI(){
// 返回值是一个二进制的数,需要使用强制转换
enumPopup = (TempEnum2)EditorGUILayout.EnumPopup("枚举类型单选下拉框:", enumPopup);
}
public int intPopupValue = 0;
public TempEnum2 enumPopup = TempEnum2.One;
public void OnGUI(){
intPopupValue = EditorGUILayout.IntPopup("整数类型单选下拉框:", intPopupValue, new[] { "IntPopup_1", "IntPopup_2", "IntPopup_3" }, new[] { 1, 2, 3});
}
public string[] intMaskFiledStrs = new string[] { "IntPopup_1", "IntPopup_2", "IntPopup_3" }; public int intMaskFiled = 0; public void OnGUI(){ intMaskFiled = EditorGUILayout.MaskField("整数类型多选下拉框:", intMaskFiled, intMaskFiledStrs); } ... // 多选框获取选择的索引 for (int i = 0; i < intMaskFiledStrs.Length; i++) { int temp = 1 << i; if ((intMaskFiled & temp) == temp) { Debug.Log($"{intMaskFiledStrs[i]} 被选中"); } else { Debug.Log($"{intMaskFiledStrs[i]} 没有被选中"); } }
public LayerMask layerFiled = 0;
public string tagFiled = "Player";
public void OnGUI(){
layerFiled = EditorGUILayout.LayerField("选择一个Layer:", layerFiled);
tagFiled = EditorGUILayout.TagField("选择一个Tag:", tagFiled);
}
public Color colorFiled = Color.white;
public void OnGUI(){
colorFiled = EditorGUILayout.ColorField("颜色框", colorFiled);
}
public Object objectFiled = null;
public void OnGUI(){
objectFiled = EditorGUILayout.ObjectField("选择一个对象:", objectFiled, typeof(GameObject), true);
}
public AnimationCurve animCurveFiled = AnimationCurve.Linear(0, 0, 1, 1);
public void OnGUI(){
animCurveFiled = EditorGUILayout.CurveField("曲线", animCurveFiled);
}
public void OnGUI(){ // 默认文字按钮样式 - 最常见的样式(默认背景图 + 文字) GUILayout.Button("单击按钮"); // 默认图片按钮样式 - 默认背景图 + 自定义图片 Texture btnSp = Resources.Load<Texture>("btn_ljqw"); GUILayout.Button(btnSp); // 新定义一个GUIStyle,设置自定义样式 // imagePosition字段有4种模式: GUIStyle tempGUIStyle = new GUIStyle(); tempGUIStyle.imagePosition = ImagePosition.ImageLeft; // 1.ImageLeft - 图片在文字的左边(不会有默认背景图) GUILayout.Button(new GUIContent("按钮1 - ImageLeft", btnSp), tempGUIStyle); GUILayout.Space(10); // 2.ImageAbove - 图片在文字的上方(不是盖在上边)(不会有默认背景图) tempGUIStyle.imagePosition = ImagePosition.ImageAbove; GUILayout.Button(new GUIContent("按钮1 - ImageAbove", btnSp), tempGUIStyle); GUILayout.Space(10); // 3.ImageOnly - 仅显示图片(不会有默认背景图) tempGUIStyle.imagePosition = ImagePosition.ImageOnly; GUILayout.Button(new GUIContent("按钮1 - ImageOnly", btnSp), tempGUIStyle); GUILayout.Space(10); // 4.TextOnly - 仅显示文字(不会有默认背景图) tempGUIStyle.imagePosition = ImagePosition.TextOnly; GUILayout.Button(new GUIContent("按钮1 - TextOnly", btnSp), tempGUIStyle); // 按住按钮 - 接口同GUILayout.Button GUILayout.RepeatButton("按住按钮"); }
public int selectGridIdx = 0;
public string[] selectGridStrs = new string[] { "按钮1", "按钮2", "按钮3", "按钮4" };
public string selectGridShowStr = "";
public void OnGUI(){
GUILayout.BeginVertical();
selectGridIdx = GUILayout.SelectionGrid(selectGridIdx, selectGridStrs, 2);
if (GUILayout.Button("打印"))
{
selectGridShowStr = string.Format("你选择了{0}, 索引是{1}", selectGridStrs[selectGridIdx], selectGridIdx);
}
GUILayout.Label(selectGridShowStr);
GUILayout.EndVertical();
}
public int selectGridIdx = 0;
public string[] selectGridStrs = new string[] { "按钮1", "按钮2", "按钮3", "按钮4" };
public string selectGridShowStr = "";
public void OnGUI(){
GUILayout.BeginVertical();
selectGridIdx = GUILayout.Toolbar(selectGridIdx, selectGridStrs);
if (GUILayout.Button("打印"))
{
selectGridShowStr = string.Format("你选择了{0}, 索引是{1}", selectGridStrs[selectGridIdx], selectGridIdx);
}
GUILayout.Label(selectGridShowStr);
GUILayout.EndVertical();
}
public bool toggleValue = false;
public void OnGUI(){
// 两个接口只是样式不同而已,这里就用同一个字段展示一下
toggleValue = EditorGUILayout.Toggle("开关", toggleValue);
toggleValue = EditorGUILayout.ToggleLeft("开关", toggleValue);
}
public bool toggleGroupValue_1 = true;
public bool toggleGroupValue_2 = false;
public void OnGUI(){
// 开关组内的东西需要在BeginToggleGroup和EndToggleGroup之间
// 当开关组的值为false时,组内的空间皆不可操作更改
toggleGroupValue_1 = EditorGUILayout.BeginToggleGroup("Pos", toggleGroupValue_1);
vector3Filed = EditorGUILayout.Vector3Field("位置修改:", vector3Filed);
EditorGUILayout.EndToggleGroup();
toggleGroupValue_2 = EditorGUILayout.BeginToggleGroup("Scale", toggleGroupValue_2);
vector3IntFiled = EditorGUILayout.Vector3IntField("缩放修改:", vector3IntFiled);
EditorGUILayout.EndToggleGroup();
}
public float sliderValue = 0; public int intSliderValue = 0; public float leftSliderValue = 0; public float rightSliderValue = 1; public float horizontalSliderValue = 0; public float verticalSliderValue = 0; public void OnGUI(){ sliderValue = EditorGUILayout.Slider("普通滑动条:", sliderValue, -5, 5); intSliderValue = EditorGUILayout.IntSlider("整数滑动条:", intSliderValue, -5, 5); // 按住中间,滑动不会改变min和max的差值 // 按住左边/右边滑块,分别改变min和max值 EditorGUILayout.MinMaxSlider("双滑块滑动条:", ref minSliderValue, ref maxSliderValue, -5, 5); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Min = ", minSliderValue.ToString()); EditorGUILayout.LabelField("Max = ", maxSliderValue.ToString()); EditorGUILayout.EndHorizontal(); horizontalSliderValue = GUILayout.HorizontalSlider(horizontalSliderValue, 0, 100); EditorGUILayout.Space(20); verticalSliderValue = GUILayout.VerticalSlider(verticalSliderValue, 0, 100, GUILayout.Height(100)); EditorGUILayout.LabelField("水平滑动条的值:", horizontalSliderValue.ToString()); EditorGUILayout.LabelField("垂直滑动条的值:", verticalSliderValue.ToString()); }
public float horizontalScrollBarValue = 0;
public float verticalScrollBarValue = 0;
public void OnGUI(){
horizontalScrollBarValue = GUILayout.HorizontalScrollbar(horizontalScrollBarValue, 0, 0, 100, GUILayout.Width(200));
GUILayout.BeginHorizontal();
verticalScrollBarValue = GUILayout.VerticalScrollbar(verticalScrollBarValue, 0, 0, 100, GUILayout.Height(200));
GUILayout.Label(string.Format("水平滚动条值:{0}\n垂直滚动条值:{1}", horizontalScrollBarValue, verticalScrollBarValue));
GUILayout.EndHorizontal();
}
public void OnGUI(){
Texture btnSp = Resources.Load<Texture>("btn_ljqw");
GUIStyle boxStyle = new GUIStyle();
boxStyle.imagePosition = ImagePosition.ImageAbove;
GUILayout.Box(new GUIContent("测试Box", btnSp, "ToolTips" ), new[] { GUILayout.Width(200), GUILayout.Height(200)});
}
public void OnGUI(){
EditorGUILayout.HelpBox("这是tips内容 - Warning", MessageType.Warning);
EditorGUILayout.HelpBox("这是tips内容 - Info", MessageType.Info);
EditorGUILayout.HelpBox("这是tips内容 - None", MessageType.None);
EditorGUILayout.HelpBox("这是tips内容 - Error", MessageType.Error);
EditorGUILayout.HelpBox("这是tips内容 - Error - wide", MessageType.Error, true);
}
public void OnGUI(){
EditorGUILayout.Space(10);
}
如果在BeginHorizontal组内使用,则表示左右对齐窗口边缘,
如果在BeginVertical组内使用,则表示上下对齐窗口边缘。
public void OnGUI(){ GUILayout.BeginHorizontal(); GUILayout.BeginVertical(); GUILayout.Button("按钮左上角", GUILayout.Width(100)); GUILayout.FlexibleSpace(); //上下对齐 GUILayout.Button("按钮左下角", GUILayout.Width(100)); GUILayout.EndVertical(); GUILayout.FlexibleSpace(); // 左右对齐 GUILayout.BeginVertical(); GUILayout.Button("按钮右上角", GUILayout.Width(100)); GUILayout.FlexibleSpace(); //上下对齐 GUILayout.Button("按钮右下角", GUILayout.Width(100)); GUILayout.EndVertical(); GUILayout.EndHorizontal(); }
暂时就这么多了,如果有错误的,欢迎各位大佬前来指正!!!
参考:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。