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);
    }
}

你可能感兴趣的:(开始学习)