当前位置:   article > 正文

【Unity编辑器扩展基础】、GUILayout_unity guilayout.textarea layout

unity guilayout.textarea layout

小结:

1、TextArea、TextField等文本输入框不能复制,TextArea可以换行,可以自适应宽。

2、参数可以传GUIContent的都可以显示图片或文字。

3、 GUILayout.Window需要调用BeginWindows、 EndWindows才能显示窗口。

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. /// <summary>
  6. /// Unity 5.6
  7. /// GUILayoutOption参数:
  8. /// GUILayout.Width, GUILayout.Height, GUILayout.MinWidth, GUILayout.MaxWidth, GUILayout.MinHeight, GUILayout.MaxHeight, GUILayout.ExpandWidth, GUILayout.ExpandHeight
  9. /// </summary>
  10. public class GUILayoutControlExample : EditorWindow
  11. {
  12. [MenuItem("Examples/GUILayoutControlExample")]
  13. static void Init()
  14. {
  15. GUILayoutControlExample window = (GUILayoutControlExample)EditorWindow.GetWindow(typeof(GUILayoutControlExample));
  16. window.Show();
  17. }
  18. void OnEnable()
  19. {
  20. #region Box
  21. m_boxTexture = Resources.Load("Textures/EquipIcon/12001") as Texture;
  22. m_boxLable = "GUILayout的Box控件!改变窗口大小后文本可以自适应";
  23. #endregion
  24. }
  25. // Box
  26. Texture m_boxTexture;
  27. string m_boxLable ;
  28. //Label
  29. string m_lableText = "GUILayout的Label控件!改变窗口大小后文本不能自适应";
  30. //PasswordField
  31. string m_passwordFieldText = "";
  32. // SelectionGrid
  33. int selGridInt = 0;
  34. string[] selStrings = new string[] { "radio1", "radio2", "radio3", "radio4" };
  35. int selectIndex = -1;
  36. //BeginScrollView
  37. public Vector2 scrollPosition;
  38. public string longString = "This is a long-ish string";
  39. //BeginArea
  40. Rect area = new Rect(500, 20, 200, 200);
  41. //GUILayout.Window
  42. Rect windowRect = new Rect(200, 50, 200, 50);
  43. //Scrollbar
  44. public float vSbarValue;
  45. public float hSbarValue;
  46. //Slider
  47. public float vSliderValue;
  48. public float hSliderValue;
  49. //TextArea
  50. string m_textAtea = "TextArea 文本框";
  51. //TextField
  52. string m_textField = "TextField 输入框";
  53. //Toggle
  54. bool m_toggle = true;
  55. //ToolBar
  56. int toolbarInt = 0;
  57. int toolbarIndex = -1;
  58. string[] toolbarStrings = new string[] { "Toolbar1", "Toolbar2", "Toolbar3" };
  59. void OnGUI()
  60. {
  61. //参数可以传GUIContent的都可以显示图片或文字
  62. #region Box
  63. //显示的图片大小就是资源大小,文本可以自适应
  64. if (m_boxTexture != null)
  65. {
  66. GUILayout.Box(m_boxTexture);
  67. //GUILayout.Box(new GUIContent(m_boxLable, m_boxTexture));
  68. }
  69. GUILayout.Box(m_boxLable);
  70. #endregion
  71. #region Button 自动布局按钮
  72. //Button:鼠标按下抬起是一次有效点击,按下抬起才返回True。
  73. //RepeatButton:鼠标按下后会一直返回True。(如果没有调用Repaint函数,按下、抬起返回True)
  74. if (GUILayout.Button("Button"))
  75. {
  76. Debug.Log("点击按钮");
  77. Repaint();
  78. }
  79. if (GUILayout.RepeatButton("RepeatButton"))
  80. {
  81. Debug.Log("点击RepeatButton按钮");
  82. Repaint();
  83. }
  84. #endregion
  85. //FlexibleSpace 自适应间距
  86. GUILayout.FlexibleSpace();
  87. GUILayout.Label("Lable:"+ m_lableText);
  88. //PasswordField 传入的string需要先初始化一个默认值,不然会报空异常
  89. m_passwordFieldText = GUILayout.PasswordField(m_passwordFieldText, '*');
  90. GUI.color = Color.red;
  91. GUILayout.Label("PasswordField:" + m_passwordFieldText);
  92. GUI.color = Color.white;
  93. #region SelectionGrid
  94. //感觉就跟UGUI的GridLayoutGroup一样
  95. selGridInt = GUILayout.SelectionGrid(selGridInt, selStrings, 2);
  96. if (selectIndex!= selGridInt)
  97. {
  98. Debug.Log("选择了: " + selStrings[selGridInt]);
  99. selectIndex = selGridInt;
  100. }
  101. #endregion
  102. GUILayout.Space(20);
  103. //可以自适应宽,可以换行
  104. m_textAtea = GUILayout.TextArea(m_textAtea);
  105. //不能自适应,只有一行
  106. m_textField = GUILayout.TextField(m_textField);
  107. m_toggle = GUILayout.Toggle(m_toggle, "Toggle:");
  108. toolbarInt = GUILayout.Toolbar(toolbarInt, toolbarStrings);
  109. if (toolbarIndex!=toolbarInt)
  110. {
  111. Debug.Log("点击了:" + toolbarStrings[toolbarInt]);
  112. toolbarIndex = toolbarInt;
  113. }
  114. //滚动条
  115. vSbarValue = GUILayout.VerticalScrollbar(vSbarValue, 1.0F, 10.0F, 0.0F);
  116. hSbarValue = GUILayout.HorizontalScrollbar(hSbarValue, 1.0F, 0.0F, 10.0F);
  117. //滑动条
  118. vSliderValue = GUILayout.VerticalSlider(vSliderValue, 10.0F, 0.0F);
  119. hSliderValue = GUILayout.HorizontalSlider(hSliderValue, 0.0F, 10.0F);
  120. //创建一个GUI的区域,区域内的控件只受区域影响
  121. GUILayout.BeginArea(area);
  122. GUILayout.BeginHorizontal();
  123. #region Vertical A
  124. GUILayout.BeginVertical();
  125. GUILayout.Box("Text1");
  126. //控件与控件之间空隙一个距离
  127. GUILayout.Space(20f);
  128. GUILayout.Box("Text1");
  129. GUILayout.Space(20f);
  130. GUILayout.Button("Button");
  131. GUILayout.EndVertical();
  132. #endregion
  133. #region Vertical B
  134. GUILayout.BeginVertical();
  135. GUILayout.Box("Text3");
  136. //自动测算空隙距离
  137. GUILayout.FlexibleSpace();
  138. GUILayout.Box("Text4");
  139. GUILayout.FlexibleSpace();
  140. GUILayout.Button("Button");
  141. GUILayout.EndVertical();
  142. #endregion
  143. GUILayout.EndHorizontal();
  144. GUILayout.EndArea();
  145. //
  146. scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(300), GUILayout.Height(100));
  147. GUILayout.Label(longString);
  148. if (GUILayout.Button("Clear"))
  149. longString = "";
  150. //
  151. GUILayout.EndScrollView();
  152. if (GUILayout.Button("Add More Text"))
  153. longString += "\nHere is another line";
  154. //
  155. BeginWindows();
  156. windowRect = GUILayout.Window(0, windowRect, DoMyWindow, "GUILayout的Window");
  157. EndWindows();
  158. }
  159. void DoMyWindow(int windowID)
  160. {
  161. if (GUILayout.Button("Button"))
  162. Debug.Log("点击按钮!");
  163. GUI.DragWindow();
  164. }
  165. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/122420
推荐阅读
相关标签
  

闽ICP备14008679号