当前位置:   article > 正文

Unity编辑器基础 EditorGUILayout (大部分用法)

editorguilayout

累了不多说  (大家可以将代码复制可以看的更清楚更明白)

如图

效果图

关于效果图最后它的代码我隐藏掉了如何想看看可以自行打开

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. public class Mybianyi : EditorWindow
  6. {
  7. string PasswordField = "";
  8. string m_textArea = "";
  9. float sliders = 0;
  10. int slidera = 0;
  11. string BeginToggleGroup = "BeginToggleGroup";
  12. bool ToggleGroup = false;
  13. string Textfield = "";
  14. bool fg = false;
  15. float sum = 0;
  16. int count = 0;
  17. string tag = "aaa";
  18. int Layerfield=0;
  19. string[] pathname = new string[] { "All", "Asset", "..." };
  20. float minVal = 1;
  21. float maxVal = 2;
  22. float minLimit = -5;
  23. float maxLimit = 5;
  24. static Vector3 center = new Vector3(1, 2, 3);
  25. static Vector3 size1 = new Vector3(1, 2, 3);
  26. Bounds _bounds = new Bounds(center, size1);
  27. Color m_color = Color.white;
  28. AnimationCurve m_curve = AnimationCurve.Linear(0, 0, 10, 10);
  29. Vector2 size = new Vector2(100,100);
  30. int flags = 0;
  31. string[] options = new string[] { "CanJump", "CanShoot", "CanSwim" ,"Canabc","Canacc"};
  32. GameObject game ;
  33. bool showFoldout;
  34. Vector2 m_vector2 = new Vector2();
  35. Vector3 m_vector3 = new Vector3();
  36. Vector4 m_vector4 = new Vector4();
  37. Transform selectedTransform;
  38. GameObject selectedGameObject;
  39. bool fold;
  40. bool fold2;
  41. [MenuItem("MyWindow/Window")]
  42. static void window()
  43. {
  44. Mybianyi mybianyi = GetWindow<Mybianyi>();
  45. mybianyi.Show();
  46. }
  47. private void OnGUI()
  48. {
  49. // GUILayout.Width 控制在窗口中物体所在的宽
  50. // GUILayout.Height 控制在窗口中物体所在的高
  51. //它们的返回的类型为GUILayoutOption
  52. #region GUILayout.Label 提示语句
  53. GUILayout.Label("我的编译器(My compiler)", GUILayout.Width(50), GUILayout.Height(10)); //提示语句
  54. #endregion
  55. #region GUILayout.Button( 按钮
  56. GUILayout.Label("按钮");
  57. if( GUILayout.Button("按钮", GUILayout.Width(40), GUILayout.Height(40)))
  58. {
  59. }
  60. #endregion
  61. #region GUILayout.TextField 文本
  62. GUILayout.Label("文本(可以输入)");
  63. Textfield = GUILayout.TextField(Textfield);//单行
  64. //参数2 maxLength 最大有效长度
  65. // Textfield = GUILayout.TextField(Textfield,5);
  66. #endregion
  67. #region GUILayout.Space 空行
  68. //参数为float类型代表空行的距离
  69. GUILayout.Space(10);
  70. #endregion
  71. #region EditorGUILayout.Toggle 开关(跟Toggle一样)
  72. fg = EditorGUILayout.Toggle("Toggle", fg);//开关
  73. #endregion
  74. #region GUILayout.BeginHorizontal 横向
  75. GUILayout.BeginHorizontal();//可以在里面存放多个如果不规定大小系统会平均分配大小
  76. GUILayout.Button("按钮");
  77. Textfield = GUILayout.TextField(Textfield);
  78. GUILayout.EndHorizontal();//结束语一定要有
  79. #endregion
  80. #region GUILayout.BeginVertical 纵向
  81. GUILayout.BeginVertical();//可以在里面存放多个如果不规定大小系统会平均分配大小
  82. GUILayout.Button("按钮");
  83. Textfield = GUILayout.TextField(Textfield);
  84. GUILayout.EndVertical();//结束语一定要有
  85. #endregion
  86. #region GUILayout.HorizontalSlider(横) GUILayout.VerticalSlider(纵) Slider(分横纵 上横下纵)
  87. sum = GUILayout.HorizontalSlider(sum, 0, 10);
  88. // sum = GUILayout.VerticalSlider(sum, 0, 10);
  89. GUILayout.Space(20);
  90. #endregion
  91. #region EditorGUILayout.Popup 下拉
  92. count = EditorGUILayout.Popup("下拉:",count,pathname);
  93. #endregion
  94. #region GUILayout.BeginScrollView 滑动列表
  95. //两个true可以让横纵两条线显示出了
  96. //两个false可以让横纵两条线不显示出来
  97. size = GUILayout.BeginScrollView(size,true,true);
  98. GUILayout.EndScrollView();
  99. #endregion
  100. #region EditorGUILayout.BoundsField (边界输入) EditorGUILayout.ColorField(颜色输入) EditorGUILayout.CurveField(曲线输入) 输入框
  101. //BoundsField 边界输入框
  102. _bounds = EditorGUILayout.BoundsField("BoundsField:", _bounds);
  103. //ColorField 颜色输入框
  104. m_color = EditorGUILayout.ColorField("ColorField:", m_color);
  105. //CurveField 曲线输入框
  106. m_curve = EditorGUILayout.CurveField("CurveField:", m_curve);
  107. #endregion
  108. #region EditorGUILayout.TagField tag(标签)
  109. tag = EditorGUILayout.TagField("TagField:", tag);
  110. #endregion
  111. #region EditorGUILayout.LayerField(可以获取所有的Layer)
  112. //Layerfield 可以获取所有的Layer
  113. Layerfield = EditorGUILayout.LayerField("LayerField:", Layerfield);
  114. #endregion
  115. #region EditorGUILayout.MaskField (下拉可以多选)
  116. flags = EditorGUILayout.MaskField("MaskField:", flags, options);
  117. //Debug.Log(flags);// 除了数组的第一个是1 后面全是2的幂(幂为对应的下标) 如果多选它们会相加 系统默认会添加Nothing (对应的值0) 和Everything(-1
  118. #endregion
  119. #region EditorGUILayout.ObjectField(选择物体)
  120. game = (GameObject) EditorGUILayout.ObjectField(game,typeof(GameObject),true);//typeof(类型) 确定好类型系统会自动帮我找到所有的关于这个类型的物体
  121. #endregion
  122. #region EditorGUILayout.Foldout 折叠
  123. showFoldout = EditorGUILayout.Foldout(showFoldout, "折叠子物体:");
  124. if (showFoldout)
  125. {
  126. EditorGUI.indentLevel++;//缩进级别
  127. EditorGUILayout.LabelField("折叠块内容1");
  128. EditorGUI.indentLevel++;
  129. EditorGUILayout.LabelField("折叠块内容2");
  130. EditorGUI.indentLevel--;
  131. EditorGUI.indentLevel--;
  132. EditorGUILayout.LabelField("折叠块内容3");
  133. }
  134. #endregion
  135. #region EditorGUILayout.BeginToggleGroup(开关)
  136. ToggleGroup = EditorGUILayout.BeginToggleGroup(BeginToggleGroup, ToggleGroup);
  137. Textfield = GUILayout.TextField(Textfield);
  138. EditorGUILayout.EndToggleGroup();
  139. #endregion
  140. #region GUILayout.FlexibleSpace(布局之间左右对齐)
  141. EditorGUILayout.BeginHorizontal();//开始最外层横向布局
  142. GUILayout.FlexibleSpace();//布局之间左右对齐
  143. GUILayout.Label("-----------------分割线-----------------");
  144. GUILayout.FlexibleSpace();//布局之间左右对齐
  145. EditorGUILayout.EndHorizontal();
  146. #endregion
  147. #region EditorGUILayout.HelpBox(提示语句)
  148. EditorGUILayout.HelpBox("HelpBox Error:", MessageType.Error);//红色错误号
  149. EditorGUILayout.HelpBox("HelpBox Info:", MessageType.Info);//白色提示号
  150. EditorGUILayout.HelpBox("HelpBox None:", MessageType.None);//解释号
  151. EditorGUILayout.HelpBox("HelpBox Warning:", MessageType.Warning);//黄色警告号
  152. #endregion
  153. #region EditorGUILayout.Slider(Slider)
  154. sliders = EditorGUILayout.Slider("Slider:",sliders,0,10);
  155. #endregion
  156. #region EditorGUILayout.TextArea(text 自适应高)
  157. m_textArea = EditorGUILayout.TextArea(m_textArea);//可以多行
  158. #endregion
  159. #region GUILayout.PasswordField(可以改变成对应的符号)
  160. EditorGUILayout.BeginHorizontal();
  161. GUILayout.Label("密码text", GUILayout.Width(60));
  162. PasswordField = GUILayout.PasswordField(PasswordField, '*');//可以改变成对应的符号
  163. EditorGUILayout.EndHorizontal();
  164. #endregion
  165. #region EditorGUILayout.Vector2Field EditorGUILayout.Vector3Field EditorGUILayout.Vector4Field
  166. m_vector2 = EditorGUILayout.Vector2Field("Vector2:", m_vector2);
  167. m_vector3 = EditorGUILayout.Vector3Field("Vector3:", m_vector3);
  168. m_vector4 = EditorGUILayout.Vector4Field("Vector4:", m_vector4);
  169. #endregion
  170. #region EditorGUILayout.SelectableLabel (可以复制粘贴)
  171. EditorGUILayout.SelectableLabel("SelectableLabel");
  172. #endregion
  173. #region EditorGUILayout.MinMaxSlider (取值范围)
  174. EditorGUILayout.LabelField("Min Val:", minVal.ToString());
  175. EditorGUILayout.LabelField("Max Val:", maxVal.ToString());
  176. EditorGUILayout.MinMaxSlider("MinMaxSlider", ref minVal, ref maxVal, minLimit, maxLimit);
  177. //现在最小 现在最大 最小长度 最大长度
  178. #endregion
  179. #region EditorGUILayout.IntSlider(只能是整数)
  180. slidera = EditorGUILayout.IntSlider("IntSlider:", slidera, 1, 10);
  181. #endregion
  182. #region EditorGUILayout.InspectorTitlebar(将物体返回回来)
  183. //Transform selectedTransform = Selection.activeGameObject.transform;
  184. //GameObject selectedGameObject = Selection.activeGameObject;//选择物体(GameObject)
  185. //fold = EditorGUILayout.InspectorTitlebar(fold, selectedTransform);
  186. //fold2 = EditorGUILayout.InspectorTitlebar(fold2, selectedGameObject);
  187. #endregion
  188. }
  189. }

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号