Unity3D自学笔记——GUI组件的应用

所有GUI组件都必须写在OnGUI函数。
GUI组件所用到的图片需将Texture Type改为 Editor GUI and Legacy GUI,否则图片会有拉伸

图片,文本,按钮

GUI.Label
public static void Label(Rect position, string text);
public static void Label(Rect position, Texture image);
public static void Label(Rect position, GUIContent content);
public static void Label(Rect position, string text, GUIStyle style);
public static void Label(Rect position, Texture image, GUIStyle style);
public static void Label(Rect position, GUIContent content, GUIStyle style);

 void OnGUI() {
        GUI.Label(new Rect(10, 10, 100, 20), "Hello World!");
    }

GUI.Box
public static void Box(Rect position, string text);
public static void Box(Rect position, Texture image);
public static void Box(Rect position, GUIContent content);
public static void Box(Rect position, string text, GUIStyle style);
public static void Box(Rect position, Texture image, GUIStyle style);
public static void Box(Rect position, GUIContent content, GUIStyle style);

GUI.Button
每次点击只响应一次点击事件
GUI.RepeatButton
只要点击后不松开则一直响应点击事件

文本输入

GUI.TextField
必须要有输入值和返回值,否则内容不会根据键盘输入而变化

public string text = "This is TextField";

void OnGUI()
{
   text = GUI.TextField(new Rect(0, 0, 100, 20), text);
}

GUI.PasswordField

public string text = "This is TextField";

void OnGUI()
{
   text = GUI.PasswordField(new Rect(0, 0, 100, 20), text, '*');
}

GUI.TextArea

public string text = "This is TextField";

void OnGUI()
{
   text = GUI.TextArea(new Rect(0, 0, 100, 100), text);
}

例:登陆窗口

private string strUserName = string.Empty;
    private string strPWD = string.Empty;
    private string strMSG = string.Empty;

    void OnGUI()
    {
        GUI.Label(new Rect(0, 0, 100, 20), "UserName :");
        GUI.Label(new Rect(0, 30, 100, 20), "Password : ");

        strUserName = GUI.TextField(new Rect(120, 0, 100, 20), strUserName);
        strPWD = GUI.PasswordField(new Rect(120, 30, 100, 20), strPWD, '*');

        if(GUI.Button(new Rect(30, 60, 100, 20), "Login"))
        {
            strMSG = "Hello";
        }

        GUI.Label(new Rect(30, 90, 100, 20), strMSG);        
    }

其他控件

GUI.Toggle

bool isCheck;
void OnGUI()
{
    isCheck = GUI.Toggle(new Rect(0, 0, 100, 20), isCheck, "Toggle");
}

GUI.Toolbar

int selectedIndex = 0;
void OnGUI()
{
    selectedIndex = GUI.Toolbar(new Rect(0, 0, 100, 20), selectedIndex, new string[] {"Tool1", "Tool2", "Tool3"});
}

GUI.SelectionGrid

 int selectedIndex = 0;
 void OnGUI()
 {
     selectedIndex = GUI.SelectionGrid(new Rect(0, 0, 100, 100), selectedIndex, new string[] {"Tool1", "Tool2", "Tool3"}, 1);
 }

GUI.HorizontalSlider

float selectedValue = 0;
void OnGUI()
{
    selectedValue = GUI.HorizontalSlider(new Rect(0, 0, 100, 20), selectedValue, -100f, 100f);
}

GUI.HorizontalScrollbar

float selectedValue = 0;
    void OnGUI()
    {
        selectedValue = GUI.HorizontalScrollbar(new Rect(0, 0, 100, 20), selectedValue, 20f, -100f, 100f);
    }

GUI.BeginScrollView
GUI.EndScrollView

public Rect area;
Vector2 position = new Vector2(0, 0);
void OnGUI()
{
    position = GUI.BeginScrollView(area, position, new Rect(0, 0, 100, 100));
    GUI.Button(new Rect(0, 0, 100, 20), "Button1");
    GUI.Button(new Rect(0, 30, 100, 20), "Button2");
    GUI.Button(new Rect(0, 60, 100, 20), "Button3");
    GUI.Button(new Rect(0, 90, 100, 20), "Button4");
    GUI.EndScrollView();
}

GUI.Window

 public Rect area;
    void OnGUI()
    {
        area = GUI.Window(0, area, windfuc, "Window");
    }

    void windfuc(int windowID)
    {
        GUI.Button(new Rect(20, 20, 100, 20), "Button");
        GUI.DragWindow();
    }

GUILayout 界面自动布局

GUILayout.BeginArea
GUILayout.EndArea

void OnGUI() 
{
    GUILayout.BeginArea(new Rect(10, 10, 100, 100));
    GUILayout.Button("Click me");
    GUILayout.Button("Or me");
    GUILayout.EndArea();
}

例:组合使用

public Rect area = new Rect(20, 20, 200, 200);

void OnGUI()
{
    GUILayout.BeginArea(area);
    GUILayout.BeginHorizontal();

    #region Vertical A
    GUILayout.BeginVertical();
    GUILayout.Box("Text1");
    //控件与控件之间空隙一个距离
    GUILayout.Space(20f);
    GUILayout.Box("Text1");
    GUILayout.EndVertical();
    #endregion

    #region Vertical B
    GUILayout.BeginVertical();
    GUILayout.Box("Text3");
    //自动测算空隙距离
    GUILayout.FlexibleSpace();
    GUILayout.Box("Text4");
    GUILayout.EndVertical();
    #endregion

    GUILayout.EndHorizontal();
    GUILayout.EndArea();
}

GUISkin 与 GUIStyle

public GUISkin skin;
    private string strUserName = string.Empty;
    private string strPWD = string.Empty;
    private string strMSG = string.Empty;
    private Rect area;
    void Start()
    {
        area = new Rect(20, 20, 400, 400);
    }
    void OnGUI()
    {
        GUI.skin = skin;
        area = GUI.Window(0, area, windfuc, "Login");        
    }

    void windfuc(int windowID)
    {
        GUI.Label(new Rect(40, 100, 320, 30), "UserName");
        GUI.Label(new Rect(40, 190, 320, 30), "Password");

        strUserName = GUI.TextField(new Rect(60, 150, 280, 20), strUserName);
        strPWD = GUI.PasswordField(new Rect(60, 240, 280, 20), strPWD, '*', skin.textField);

        GUI.Button(new Rect(150, 280, 100, 40), "Login");

        GUI.DragWindow();
    }

效果图
这里写图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值