小结:
1、TextArea、TextField等文本输入框不能复制,TextArea可以换行,可以自适应宽。
2、参数可以传GUIContent的都可以显示图片或文字。
3、 GUILayout.Window需要调用BeginWindows、 EndWindows才能显示窗口。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
/// <summary>
/// Unity 5.6
/// GUILayoutOption参数:
/// GUILayout.Width, GUILayout.Height, GUILayout.MinWidth, GUILayout.MaxWidth, GUILayout.MinHeight, GUILayout.MaxHeight, GUILayout.ExpandWidth, GUILayout.ExpandHeight
/// </summary>
public class GUILayoutControlExample : EditorWindow
{
[MenuItem("Examples/GUILayoutControlExample")]
static void Init()
{
GUILayoutControlExample window = (GUILayoutControlExample)EditorWindow.GetWindow(typeof(GUILayoutControlExample));
window.Show();
}
void OnEnable()
{
#region Box
m_boxTexture = Resources.Load("Textures/EquipIcon/12001") as Texture;
m_boxLable = "GUILayout的Box控件!改变窗口大小后文本可以自适应";
#endregion
}
// Box
Texture m_boxTexture;
string m_boxLable ;
//Label
string m_lableText = "GUILayout的Label控件!改变窗口大小后文本不能自适应";
//PasswordField
string m_passwordFieldText = "";
// SelectionGrid
int selGridInt = 0;
string[] selStrings = new string[] { "radio1", "radio2", "radio3", "radio4" };
int selectIndex = -1;
//BeginScrollView
public Vector2 scrollPosition;
public string longString = "This is a long-ish string";
//BeginArea
Rect area = new Rect(500, 20, 200, 200);
//GUILayout.Window
Rect windowRect = new Rect(200, 50, 200, 50);
//Scrollbar
public float vSbarValue;
public float hSbarValue;
//Slider
public float vSliderValue;
public float hSliderValue;
//TextArea
string m_textAtea = "TextArea 文本框";
//TextField
string m_textField = "TextField 输入框";
//Toggle
bool m_toggle = true;
//ToolBar
int toolbarInt = 0;
int toolbarIndex = -1;
string[] toolbarStrings = new string[] { "Toolbar1", "Toolbar2", "Toolbar3" };
void OnGUI()
{
//参数可以传GUIContent的都可以显示图片或文字
#region Box
//显示的图片大小就是资源大小,文本可以自适应
if (m_boxTexture != null)
{
GUILayout.Box(m_boxTexture);
//GUILayout.Box(new GUIContent(m_boxLable, m_boxTexture));
}
GUILayout.Box(m_boxLable);
#endregion
#region Button 自动布局按钮
//Button:鼠标按下抬起是一次有效点击,按下抬起才返回True。
//RepeatButton:鼠标按下后会一直返回True。(如果没有调用Repaint函数,按下、抬起返回True)
if (GUILayout.Button("Button"))
{
Debug.Log("点击按钮");
Repaint();
}
if (GUILayout.RepeatButton("RepeatButton"))
{
Debug.Log("点击RepeatButton按钮");
Repaint();
}
#endregion
//FlexibleSpace 自适应间距
GUILayout.FlexibleSpace();
GUILayout.Label("Lable:"+ m_lableText);
//PasswordField 传入的string需要先初始化一个默认值,不然会报空异常
m_passwordFieldText = GUILayout.PasswordField(m_passwordFieldText, '*');
GUI.color = Color.red;
GUILayout.Label("PasswordField:" + m_passwordFieldText);
GUI.color = Color.white;
#region SelectionGrid
//感觉就跟UGUI的GridLayoutGroup一样
selGridInt = GUILayout.SelectionGrid(selGridInt, selStrings, 2);
if (selectIndex!= selGridInt)
{
Debug.Log("选择了: " + selStrings[selGridInt]);
selectIndex = selGridInt;
}
#endregion
GUILayout.Space(20);
//可以自适应宽,可以换行
m_textAtea = GUILayout.TextArea(m_textAtea);
//不能自适应,只有一行
m_textField = GUILayout.TextField(m_textField);
m_toggle = GUILayout.Toggle(m_toggle, "Toggle:");
toolbarInt = GUILayout.Toolbar(toolbarInt, toolbarStrings);
if (toolbarIndex!=toolbarInt)
{
Debug.Log("点击了:" + toolbarStrings[toolbarInt]);
toolbarIndex = toolbarInt;
}
//滚动条
vSbarValue = GUILayout.VerticalScrollbar(vSbarValue, 1.0F, 10.0F, 0.0F);
hSbarValue = GUILayout.HorizontalScrollbar(hSbarValue, 1.0F, 0.0F, 10.0F);
//滑动条
vSliderValue = GUILayout.VerticalSlider(vSliderValue, 10.0F, 0.0F);
hSliderValue = GUILayout.HorizontalSlider(hSliderValue, 0.0F, 10.0F);
//创建一个GUI的区域,区域内的控件只受区域影响
GUILayout.BeginArea(area);
GUILayout.BeginHorizontal();
#region Vertical A
GUILayout.BeginVertical();
GUILayout.Box("Text1");
//控件与控件之间空隙一个距离
GUILayout.Space(20f);
GUILayout.Box("Text1");
GUILayout.Space(20f);
GUILayout.Button("Button");
GUILayout.EndVertical();
#endregion
#region Vertical B
GUILayout.BeginVertical();
GUILayout.Box("Text3");
//自动测算空隙距离
GUILayout.FlexibleSpace();
GUILayout.Box("Text4");
GUILayout.FlexibleSpace();
GUILayout.Button("Button");
GUILayout.EndVertical();
#endregion
GUILayout.EndHorizontal();
GUILayout.EndArea();
//
scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(300), GUILayout.Height(100));
GUILayout.Label(longString);
if (GUILayout.Button("Clear"))
longString = "";
//
GUILayout.EndScrollView();
if (GUILayout.Button("Add More Text"))
longString += "\nHere is another line";
//
BeginWindows();
windowRect = GUILayout.Window(0, windowRect, DoMyWindow, "GUILayout的Window");
EndWindows();
}
void DoMyWindow(int windowID)
{
if (GUILayout.Button("Button"))
Debug.Log("点击按钮!");
GUI.DragWindow();
}
}