unity-GUI常用控件及使用方法(初学:18)

写在前面,下面只是简单的控件的使用,其中的参数远远不止下面举例的那些,具体在使用vs编辑的时候,可以转到定义看看具体的参数类型即个数,本人只是个小白,有些注释也是自己瞎想的,有不对的请多多见谅!

Label文本或者纹理标签控件
Toggle开关控件
DrawTexture纹理图片控件
RepeatButton重复点击按钮控件
Window窗口控件
BeginScrollView滚动视图控件
PasswordField密码文本框控件
SetNextControlName设置下一个控件的名字控件
Focus Window焦点窗口控件
HorizontalSlider水平的滑块控件,可以自己设置阙值
HorizontalScrollbar水平的滚动条控件,并且可以自己设置阙值
Box图形盒子控件
FocusControl焦点控件
Toolbar工具栏控件
BringWindowToFont使窗口到前面
Bring WindowToFont使窗口到后面
ScrollTo将内容滚动到指定位置
Button按钮事件
TextField单行文本编辑控件
TextArea多行文本编辑控件
GetNameOfFocusedControl获取有焦点被命名控件的名称
UnfocusWindow失焦窗口
VerticalSlider垂直滑块控件,可以自己设置阙值
VerticalScrollbar垂直滚动条控件,可以自己设置阙值

部分控件的简单应用
Toggle:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Toggle : MonoBehaviour
{
    public bool text1 = false;
    public bool text2 = false;
    public Texture texture;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    private void OnGUI()
    {
        if(!texture)
        {
            Debug.Log("Please enter a texture");

            return;
        }
        text1 = GUI.Toggle(new Rect(0, 0, 100, 100), text1, "This is a toggle");
        text2 = GUI.Toggle(new Rect(150, 150, 100, 100),text2,texture);
    }
}

Button:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Button : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    private void OnGUI()
    {
        GUI.Button(new Rect(0, 0, 10, 10), "My Button");
    }
}

UnfocusWindow:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UnfocusWindow : MonoBehaviour
{
    private Rect windowrect = new Rect(20, 20, 120, 50);//声明一个窗口的巨型区域
    private Rect windowrect2 = new Rect(20, 80, 120, 50);//如上
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
    private void OnGUI()
    {
        windowrect = GUI.Window(0, windowrect, DoMyWindow, "windowrect");
        windowrect2 = GUI.Window(1, windowrect2, DoingWindow, "windowrect2");
    }

    void DoMyWindow(int windowID)
    {
        if (GUI.Button(new Rect(10, 20, 100, 20), "Focus other"))
        {
            GUI.UnfocusWindow();//与FocusWindow的区别是不用通过ID控制,直接移除,移除的窗口是当前的窗口焦点,所以没有参数
        }
    }
    void DoingWindow(int windowID)
    {
        if (GUI.Button(new Rect(10, 20, 100, 20), "Focus others"))
        {
            GUI.UnfocusWindow();
        }
    }
}

BeginScrollView:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BeginScrollView : MonoBehaviour
{
    public Vector2 ScrillPosition = Vector2.zero;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void OnGUI()
    {
        ScrillPosition = GUI.BeginScrollView(new Rect(Screen.width/10,Screen.height/10,Screen.width/4,Screen.height/3), ScrillPosition, new Rect(0, 0,400,400));//,Screen.width/2,Screen.height/2
        GUI.Button(new Rect(0, 0, 100, 20), "Top-left");
        GUI.Button(new Rect(120, 0, 100, 20), "Top-right");
        GUI.Button(new Rect(0, 120, 100, 20), "Bottom-left");
        GUI.Button(new Rect(120, 120, 100, 20), "Bottom-right");
        GUI.EndScrollView();
    }
}

FocusWindow:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FocusWindow : MonoBehaviour
{
    private Rect windowrect = new Rect(20, 20, 120, 50);//声明一个窗口的巨型区域
    private Rect windowrect2 = new Rect(20, 80, 120, 50);//如上
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    private void OnGUI()
    {
        windowrect = GUI.Window(0, windowrect, DoMyWindow,"windowrect");
        windowrect2 = GUI.Window(1, windowrect2, DoingWindow,"windowrect2");
    }

    void DoMyWindow(int windowID)
    {
        if(GUI.Button(new Rect(10,20,100,20),"Focus other"))
        {
            GUI.FocusWindow(windowID);
        }
    }
    void DoingWindow(int windowID)
    {
        if(GUI.Button(new Rect(10,20,100,20),"Focus others"))
        {
            GUI.FocusWindow(windowID);
        }
    }
}

ScrollTo:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScrollTo : MonoBehaviour
{
    public Vector2 scroll = Vector2.zero;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    private void OnGUI()
    {
        scroll = GUI.BeginScrollView(new Rect(Screen.width / 10, Screen.height / 10, Screen.width / 4, Screen.height / 3), scroll, new Rect(0, 0, 400, 400));//创建一个自定义的滚动区域
        if(GUI.Button(new Rect(0,0,Screen.width/5,Screen.height/10),"Go Right"))
        {
            GUI.ScrollTo(new Rect(Screen.width / 4, 0, Screen.width / 4, Screen.height / 10));
        }
        if (GUI.Button(new Rect(0, Screen.height/5, Screen.width / 5, Screen.height /10), "Go left"))
        {
            GUI.ScrollTo(new Rect(0, 0, Screen.width / 4, Screen.height / 10));
        }
        GUI.EndScrollView();
    }
}

Window:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Window : MonoBehaviour
{
    public Rect windowRect0 = new Rect(20, 20, 120, 50);
    public Rect windowRect1 = new Rect(20, 100, 120, 50);


    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void OnGUI()
    {
        windowRect0 = GUI.Window(0, windowRect0, DoMyWindow, new GUIContent("MyWindow"));
        windowRect1 = GUI.Window(1, windowRect1, DoMyWindow, new GUIContent("My Window"));
    }
    void DoMyWindow(int windowID)
    {
        if (GUI.Button(new Rect(10, 20, 100, 20), "Hello World"))
        {
            GUI.BringWindowToFront(windowID);
           }
         GUI.DragWindow(new Rect(0, 0, 120, 50));//最开始放在了创建按钮的语句里面,不管怎样都不能拖动,是因为拖动与点击不能同时发生,如果扩大拖动的范围呢也是不行的,可以考虑用一下携程
     
    }
}

HorizontalSlider:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HorizontalSlider : MonoBehaviour
{
    public float value = 0;
    public float leftvalue = 0;
    public float rightvalue = 100;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    private void OnGUI()
    {
        value = GUI.HorizontalScrollbar(new Rect(300, 300, 100, 10), value, 10f,leftvalue, rightvalue);//size是滑块的大小
    }
}

SelectionGrid:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SlectionGrid : MonoBehaviour
{
    public string[] seleststrings = new string[] { "Gid1", "Gid2"};
    public int election = 0;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    private void OnGUI()
    {
        election =  GUI.SelectionGrid(new Rect(0, 0, 100, 100), election, seleststrings, 2);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值