默认计划
2325人加入学习
(4人评价)
【唐老狮】Unity中的UI系统之GUI
价格 ¥ 109.00
承诺服务
该课程属于 Unity3d实战就业路线课程套餐
请加入后再学习

GUI.Label(Rect变量,字符串);

GUI.Label(Rect变量,Texture变量);

GUI.Label(Rect变量,GUIContent变量);

GUI.tooltip,鼠标触碰到控件时GUIContent变量

中tooltip的值,没触到就是0

以上函数均可在最后加GUIStyle变量

if(GUI.Button(Rect变量,GUIContent变量,GUIStyle变量))

{

        //此控件被点击并在其范围内抬起时执行的逻辑

}

if(GUI.RepeatButton(Rect变量,GUIContent变量,GUIStyle变量))

{

        //此控件被长按时执行的逻辑

}

 

 

[展开全文]

GUI代码驱动

作用:

1.创建游戏内调试工具

2.为脚本组建创建自定义检视面板

3.创建编辑器窗口和工具

4(不要用它为玩家制作UI功能)

工作原理

private void OnGUI()

{

//每帧执行,在OnDIsable之前,LateUpdate之后执行,使用条件该脚本继承MonoBehavior

}

[展开全文]
using System.Collections.Generic;
using UnityEngine;

public class 单选控件 : MonoBehaviour
{
    /*单选“控件”本质上仍然是通过协同管理多个Toggle控件来实现的*/

    #region 内部类型
    [System.Serializable]
    public sealed class ToggleInfo
    {
        public string content;
        public Vector2 size;
    }

    public enum Direction
    {
        Horizontal,
        Vertical
    }
    #endregion

    public List<ToggleInfo> toggles;
    public Vector2 startPosition;
    public int spacing;
    public Direction m_direction;

    private int selectedIndex = 0;

    private void OnGUI()
    {
        float posX = startPosition.x;
        float posY = startPosition.y;

        for (int i = 0; i < toggles.Count; i++)
        {
            Rect rect = new Rect(posX, posY, toggles[i].size.x, toggles[i].size.y);

            if (GUI.Toggle(rect, selectedIndex == i, toggles[i].content))
            {
                selectedIndex = i;
            }

            switch (m_direction)
            {
                case Direction.Horizontal:
                    posX += (toggles[i].size.x + spacing);
                    break;
                case Direction.Vertical:
                    posY += (toggles[i].size.y + spacing);
                    break;
            }
        }
    }
}

试着自己实现了一个简易好用的单选控件

[展开全文]

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


public class PositionInfomation
{
    float screenWidth;
    float screenHeight;
    Vector2 screenOriginPos;
    Vector2 selfOriginPos;
    public Vector2 offsetPos;
    public Vector2 widthAndHight;
    public Vector2 finalPos;


    //基于屏幕原点
    public void FindScreenOrigin(E_AlignmentType screenAlignment)
    {
        screenWidth = Screen.width;
        screenHeight = Screen.height;
        switch (screenAlignment)
        {
            case E_AlignmentType.Left:
                screenOriginPos.x = 0;
                screenOriginPos.y = screenHeight/2;
                break;
            case E_AlignmentType.Right:
                screenOriginPos.x = screenWidth;
                screenOriginPos.y = screenHeight / 2;
                break;
            case E_AlignmentType.Top:
                screenOriginPos.x = screenWidth/2;
                screenOriginPos.y = 0;
                break;
            case E_AlignmentType.Bottom:
                screenOriginPos.x = screenWidth / 2;
                screenOriginPos.y = screenHeight;
                break;
            case E_AlignmentType.TopLeft:
                screenOriginPos.x = 0;
                screenOriginPos.y = 0;
                break;
            case E_AlignmentType.TopRight:
                screenOriginPos.x = screenWidth;
                screenOriginPos.y = 0;
                break;
            case E_AlignmentType.BottomRight:
                screenOriginPos.x = screenWidth;
                screenOriginPos.y = screenHeight;
                break;
            case E_AlignmentType.BottomLeft:
                screenOriginPos.x = 0;
                screenOriginPos.y = screenHeight;
                break;
            case E_AlignmentType.Center:
                screenOriginPos.x = screenWidth/2;
                screenOriginPos.y = screenHeight/2;
                break;
        }
    }

    //自身原点绘制偏移
    public void FindSelfOrigin(E_AlignmentType selfAlignment)
    {
        switch (selfAlignment)
        {
            case E_AlignmentType.Left:
                selfOriginPos.x = 0;
                selfOriginPos.y = - widthAndHight.y / 2;
                break;
            case E_AlignmentType.Right:
                selfOriginPos.x = -widthAndHight.x;
                selfOriginPos.y = -widthAndHight.y / 2;
                break;
            case E_AlignmentType.Top:
                selfOriginPos.x = widthAndHight.x / 2;
                selfOriginPos.y = 0;
                break;
            case E_AlignmentType.Bottom:
                selfOriginPos.x = -widthAndHight.x / 2;
                selfOriginPos.y = -widthAndHight.y;
                break;
            case E_AlignmentType.TopLeft:
                selfOriginPos.x = 0;
                selfOriginPos.y = 0;
                break;
            case E_AlignmentType.TopRight:
                selfOriginPos.x = -widthAndHight.x;
                selfOriginPos.y = 0;
                break;
            case E_AlignmentType.BottomRight:
                selfOriginPos.x = -widthAndHight.x;
                selfOriginPos.y = -widthAndHight.y;
                break;
            case E_AlignmentType.BottomLeft:
                selfOriginPos.x = 0;
                selfOriginPos.y = -widthAndHight.y;
                break;
            case E_AlignmentType.Center:
                selfOriginPos.x = -widthAndHight.x / 2;
                selfOriginPos.y = -widthAndHight.y / 2;
                break;
        }
    }

    //最终位置
    public void FinalPos()
    {
        finalPos = selfOriginPos + screenOriginPos + offsetPos;
    }
}

----------------------

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


public class ContentControlBase : MonoBehaviour
{
    protected PositionInfomation positionInfomation = new PositionInfomation();
    public Rect position;
    public GUIContent content;
    public E_AlignmentType screenAlignment = E_AlignmentType.TopLeft;
    public E_AlignmentType selfAlignment = E_AlignmentType.TopLeft;
    public Vector2 offsetPos;
    public bool dIYStyle;
    public GUIStyle style;

    protected virtual void OnGUI()
    {
        positionInfomation.widthAndHight.x = position.width;
        positionInfomation.widthAndHight.y = position.height;
        positionInfomation.offsetPos = this.offsetPos;
        positionInfomation.FindScreenOrigin(screenAlignment);
        positionInfomation.FindSelfOrigin(selfAlignment);
        positionInfomation.FinalPos();
        position.x = positionInfomation.finalPos.x;
        position.y = positionInfomation.finalPos.y;
    }
}

--------------------------

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

[ExecuteAlways]
public class ButtonClass : ContentControlBase
{
    
    protected override void OnGUI()
    {
        base.OnGUI();
        if (dIYStyle)
        {
            GUI.Button(position, content, style);
        }
        else
        {
            GUI.Button(position, content);
        }
    }  
}

[展开全文]

using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

public class MainMenu : MonoBehaviour
{
    private static MainMenu instance;

    public GUIStyle gUIStyle;
    public Rect rectStartGame;
    public Rect rectExitGame;
    public Rect rectOpinions;
    public Rect backgroundPicRect;
    public bool opinionsIsSelect;
    public Texture backgroundTex;

    public Rect musicToggle;
    public Rect soundToggle;
    public bool musicToggleIsSelect;
    public bool soundToggleIsSelect;

    public Rect volumeRect;
    public GUIStyle volumeGUIStyle;
    float volume = 50f;

    AudioSource musicAudioSource;
    public AudioClip musicClip;

    private void OnGUI()
    {
        GUI.DrawTexture(backgroundPicRect, backgroundTex);

        if (GUI.Button(rectStartGame, "开始游戏", gUIStyle))
        {
            HidePannel();
            SignIn.ShowPannel();
        }
        opinionsIsSelect = GUI.Toggle(rectOpinions, opinionsIsSelect, "设置", gUIStyle);
        if (opinionsIsSelect) 
        {
            musicToggleIsSelect = GUI.Toggle(musicToggle, musicToggleIsSelect, "音乐开关", gUIStyle);
            soundToggleIsSelect = GUI.Toggle(soundToggle, soundToggleIsSelect, "音效开关", gUIStyle);
            if (musicToggleIsSelect)
            {
                musicAudioSource = this.gameObject.GetComponent<AudioSource>();
                if (musicAudioSource == null)
                {
                    musicAudioSource= this.gameObject.AddComponent<AudioSource>();
                }
                else
                {
                    musicAudioSource.clip = musicClip;
                    volume = GUI.HorizontalSlider(volumeRect, volume, 0, 1);
                    musicAudioSource.volume = volume;
                    musicAudioSource.Play();
                    musicAudioSource.loop = true;
                }
            }
            else if (musicAudioSource != null)
            {
                musicAudioSource.Stop();
            }
        }
        if (GUI.Button(rectExitGame, "结束游戏", gUIStyle))
        {
            QuitMenu.ShowPannel();
        }
    }

    private void Awake()
    {
        instance = this;
    }

    public static void ShowPannel()
    {
        instance.gameObject.SetActive(true);
    }


    public static void HidePannel()
    {
        instance.gameObject.SetActive(false);
    }
}

---------------------

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class QuitMenu : MonoBehaviour
{
    Rect quitMenu = new Rect(200, 150, 200, 100);
    Rect yesQuit = new Rect(20, 40, 50, 20);
    Rect cancelQuit = new Rect(125, 40, 50, 20);

    bool inThisMenu = false;

    private static QuitMenu instance;

    private void Awake()
    {
        instance = this;
        HidePannel();
    }
    private void OnGUI()
    {
        inThisMenu = true;
        quitMenu = GUI.ModalWindow(1, quitMenu, DrawMenu, "是否退出");
    }

    void DrawMenu(int id)
    {
        switch (id)
        {
            case 1:
                if (GUI.Button(yesQuit, "退出"))
                {
                    Application.Quit();
                }
                if (GUI.Button(cancelQuit, "取消"))
                {
                    HidePannel();
                }
                GUI.DragWindow();
                break; 
        }
    }

    public static void ShowPannel()
    {
        instance.gameObject.SetActive(true);
    }

    private static void HidePannel()
    {
        instance.gameObject.SetActive(false);
    }
}
 

[展开全文]

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

public class Scrollbar : MonoBehaviour
{
    public string[] display = new string[] { };
    public Rect scrollView;
    public Vector2 vector2ScrollPosition;
    public Rect scrollContent;


    private void OnGUI()
    {
        scrollContent.height = display.Length * 15;
        vector2ScrollPosition = GUI.BeginScrollView(scrollView , vector2ScrollPosition , scrollContent);
        for (int i = 0; i < display.Length; i++)
        {
            GUI.Label(new Rect(0, i * 15, 100, 30), display[i]);
        }

        GUI.EndScrollView();
    }
}

[展开全文]

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

public class Toolbar : MonoBehaviour
{
    public Rect toolbarRect;
    public Rect displayRect;
    public string[] titleSelected = new string[] {"选项一","选项二","选项三","选项四","选项五" };
    public string display = "";
    int toolbarIndex = 0;

    private void OnGUI()
    {
        toolbarIndex = GUI.Toolbar(toolbarRect, toolbarIndex, titleSelected);
        GUI.Label(displayRect, display);
        switch (toolbarIndex)
        {
            case 0:
                display = "第一个信息";
                break;
            case 1:
                display = "第二个信息";
                break;
            case 2:
                display = "第三个信息";
                break;
            case 3:
                display = "第四个信息";
                break;
            case 4:
                display = "第五个信息";
                break;

            default:
                display = "";
                break;
        }
    }
}

[展开全文]

using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

public class MainMenu : MonoBehaviour
{
    private static MainMenu instance;

    public GUIStyle gUIStyle;
    public Rect rectStartGame;
    public Rect rectExitGame;
    public Rect rectOpinions;
    public Rect backgroundPicRect;
    public bool opinionsIsSelect;
    public Texture backgroundTex;

    public Rect musicToggle;
    public Rect soundToggle;
    public bool musicToggleIsSelect;
    public bool soundToggleIsSelect;

    public Rect volumeRect;
    public GUIStyle volumeGUIStyle;
    float volume = 50f;

    AudioSource musicAudioSource;
    public AudioClip musicClip;

    private void OnGUI()
    {
        GUI.DrawTexture(backgroundPicRect, backgroundTex);

        if (GUI.Button(rectStartGame, "开始游戏", gUIStyle))
        {
            HidePannel();
            SignIn.ShowPannel();
        }
        opinionsIsSelect = GUI.Toggle(rectOpinions, opinionsIsSelect, "设置", gUIStyle);
        if (opinionsIsSelect) 
        {
            musicToggleIsSelect = GUI.Toggle(musicToggle, musicToggleIsSelect, "音乐开关", gUIStyle);
            soundToggleIsSelect = GUI.Toggle(soundToggle, soundToggleIsSelect, "音效开关", gUIStyle);
            if (musicToggleIsSelect)
            {
                if (musicAudioSource == null)
                {
                    musicAudioSource= this.gameObject.AddComponent<AudioSource>();
                }
                else
                {
                    musicAudioSource.clip = musicClip;
                    volume = GUI.HorizontalSlider(volumeRect, volume, 0, 1);
                    musicAudioSource.volume = volume;
                    musicAudioSource.loop = true;
                    musicAudioSource.Play();
                }
            }
            else if (musicAudioSource != null)
            {
                musicAudioSource.Stop();
            }
        }
        if (GUI.Button(rectExitGame, "结束游戏", gUIStyle))
        {
            Application.Quit();
        }
    }

    private void Awake()
    {
        instance = this;
    }

    public static void ShowPannel()
    {
        instance.gameObject.SetActive(true);
    }


    public static void HidePannel()
    {
        instance.gameObject.SetActive(false);
    }
}

-------------------

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

public class SignIn : MonoBehaviour
{
    private static SignIn instance;

    public Rect backgroundPicRect;
    public Texture backgroundTex;

    public GUIStyle headLine;
    public GUIStyle button;
    public Rect userAddressRect;
    public Rect passwordRect;
    public Rect headLineRect;
    public Rect confirmbuttonRect;
    public Rect backToMainMenuRect;
    public Rect informationRect;

    string userAddress = "请输入用户名";
    string password = "";
    string infomation = "请输入密码";

    private void OnGUI()
    {
        GUI.DrawTexture(backgroundPicRect, backgroundTex);
        GUI.Label(headLineRect, "登陆界面", headLine);
        GUI.Label(informationRect, infomation, headLine);
        userAddress = GUI.TextField(userAddressRect, userAddress);
        password = GUI.PasswordField(passwordRect, password, '*');
        if(GUI.Button(confirmbuttonRect, "确定"))
        {
            if (password == "8888" && userAddress == "Admin")
            {
                SceneManager.LoadScene("GameScene");
            }
            else
            {
                infomation = "用户名或密码错误";
            }
        }
        if (GUI.Button(backToMainMenuRect, "返回"))
        {
            HidePannel();
            MainMenu.ShowPannel();
        }
    }
    private void Awake()
    {
        instance = this;
        HidePannel();
    }
    public static void ShowPannel()
    {
        instance.gameObject.SetActive(true);
    }


    public static void HidePannel()
    {
        instance.gameObject.SetActive(false);
    }
}
 

[展开全文]

using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

public class MainMenu : MonoBehaviour
{
    private static MainMenu instance;

    public GUIStyle gUIStyle;
    public Rect rectStartGame;
    public Rect rectExitGame;
    public Rect rectOpinions;
    public bool opinionsIsSelect;

    public Rect musicToggle;
    public Rect soundToggle;
    public bool musicToggleIsSelect;
    public bool soundToggleIsSelect;

    public Rect volumeRect;
    public GUIStyle volumeGUIStyle;
    float volume = 50f;

    AudioSource musicAudioSource;
    public AudioClip musicClip;

    private void OnGUI()
    {
        if(GUI.Button(rectStartGame, "开始游戏", gUIStyle))
        {
            HidePannel();
            SignIn.ShowPannel();
        };
        opinionsIsSelect = GUI.Toggle(rectOpinions, opinionsIsSelect, "设置", gUIStyle);
        if (opinionsIsSelect) 
        {
            musicToggleIsSelect = GUI.Toggle(musicToggle, musicToggleIsSelect, "音乐开关", gUIStyle);
            soundToggleIsSelect = GUI.Toggle(soundToggle, soundToggleIsSelect, "音效开关", gUIStyle);
            if (musicToggleIsSelect)
            {
                if (musicAudioSource == null)
                {
                    musicAudioSource= this.gameObject.AddComponent<AudioSource>();
                    musicAudioSource.clip = musicClip;
                    volume = GUI.HorizontalSlider(volumeRect, volume, 0, 1);
                    musicAudioSource.volume = volume;
                    musicAudioSource.Play();
                    musicAudioSource.loop = true;
                }
                else
                {
                    musicAudioSource.clip = musicClip;
                    volume = GUI.HorizontalSlider(volumeRect, volume, 0, 1);
                    musicAudioSource.volume = volume;
                    musicAudioSource.loop = true;
                    musicAudioSource.Play();
                }
            }
            else if (musicAudioSource != null)
            {
                musicAudioSource.Stop();
            }
        }
        if (GUI.Button(rectExitGame, "结束游戏", gUIStyle))
        {
            Application.Quit();
        }
    }

    private void Awake()
    {
        instance = this;
    }

    public static void ShowPannel()
    {
        instance.gameObject.SetActive(true);
    }


    public static void HidePannel()
    {
        instance.gameObject.SetActive(false);
    }
}
-----------------------------

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

public class SignIn : MonoBehaviour
{
    private static SignIn instance;

    public GUIStyle headLine;
    public GUIStyle button;
    public Rect userAddressRect;
    public Rect passwordRect;
    public Rect headLineRect;
    public Rect confirmbuttonRect;
    public Rect backToMainMenuRect;
    public Rect informationRect;

    string userAddress = "请输入用户名";
    string password = "*";
    string infomation = "请输入密码";

    private void OnGUI()
    {
        GUI.Label(headLineRect, "登陆界面", headLine);
        GUI.Label(informationRect, infomation, headLine);
        userAddress = GUI.TextField(userAddressRect, userAddress);
        password = GUI.PasswordField(passwordRect, password, '*');
        if(GUI.Button(confirmbuttonRect, "确定"))
        {
            if (password == "8888" && userAddress == "Admin")
            {
                SceneManager.LoadScene("GameScene");
            }
            else
            {
                infomation = "用户名或密码错误";
            }
        }
        if (GUI.Button(backToMainMenuRect, "返回"))
        {
            HidePannel();
            MainMenu.ShowPannel();
        }
    }
    private void Awake()
    {
        instance = this;
        HidePannel();
    }
    public static void ShowPannel()
    {
        instance.gameObject.SetActive(true);
    }


    public static void HidePannel()
    {
        instance.gameObject.SetActive(false);
    }
}

[展开全文]

public class MainMenu : MonoBehaviour
{
    public GUIStyle gUIStyle;
    public Rect rectStartGame;
    public Rect rectExitGame;
    public Rect rectOpinions;
    public bool opinionsIsSelect;

    public Rect musicToggle;
    public Rect soundToggle;
    public bool musicToggleIsSelect;
    public bool soundToggleIsSelect;

    AudioSource musicAudioSource;
    public AudioClip musicClip;

    private void OnGUI()
    {
        if(GUI.Button(rectStartGame, "开始游戏", gUIStyle))
        {
            SceneManager.LoadScene("GameScene");
        };
        opinionsIsSelect = GUI.Toggle(rectOpinions, opinionsIsSelect, "设置", gUIStyle);
        if (opinionsIsSelect) 
        {
            musicToggleIsSelect = GUI.Toggle(musicToggle, musicToggleIsSelect, "音乐开关", gUIStyle);
            soundToggleIsSelect = GUI.Toggle(soundToggle, soundToggleIsSelect, "音效开关", gUIStyle);
            if (musicToggleIsSelect)
            {
                if (musicAudioSource == null)
                {
                    musicAudioSource= this.gameObject.AddComponent<AudioSource>();
                    musicAudioSource.clip = musicClip;
                    musicAudioSource.Play();
                    musicAudioSource.loop = true;
                }
                else
                {
                    musicAudioSource.clip = musicClip;
                    musicAudioSource.loop = true;
                    musicAudioSource.Play();
                }
            }
            else if (musicAudioSource != null)
            {
                musicAudioSource.Stop();
            }
        }
        if (GUI.Button(rectExitGame, "结束游戏", gUIStyle))
        {
            Application.Quit();
        }
    }
}

[展开全文]

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

public class MainMenu : MonoBehaviour
{
    public GUIContent exitGameGUIContent;
    public GUIContent startGameGUIContent;
    public GUIContent opinionGUIContent;
    public GUIStyle gUIStyle;
    public Rect rectStartGame;
    public Rect rectExitGame;
    public Rect rectOpinions;

    private void OnGUI()
    {
        if(GUI.Button(rectStartGame, startGameGUIContent, gUIStyle))
        {
            SceneManager.LoadScene("GameScene");
        }
        GUI.Button(rectOpinions, opinionGUIContent, gUIStyle);
        if(GUI.Button(rectExitGame, exitGameGUIContent, gUIStyle))
        {
            Application.Quit();
        }
    }
}

[展开全文]

可以用一个脚本专门做这两个方法,省的每个页面都做一份:

 

public class PanelSelect : MonoBehaviour
{

    //方便在其他脚本里 . 出来
    public static PanelSelect instance;

    private void Awake()
    {
        instance = this;
    }

//传入的参数是MonoBehaviour,因为需要失活的脚本都继承了这个类

    public void ShowPanel(MonoBehaviour instance)
    {
        instance.gameObject.SetActive(true);
    }

    public void HidePanel(MonoBehaviour instance)
    {
        instance.gameObject.SetActive(false);
    }

}

 

 

另外注意因为Awake阶段可能先后顺序不一致,如果其他脚本要在这个阶段调用这个PanelSelect.instace的话可能失败,所以可以放在Start阶段,以下是示例:

 

private void Awake()
{
    instance=this;
}

private void Start()
{
    PanelSelect.instance.HidePanel(this);
}

[展开全文]

//总结
//1.要完成面板之间相互控制显示有3中方法
//第一种:都写在一个onGuI中通过bool标识去控制显影
//第二种:挂载在同一个对象上通过控制脚本的失活激活enable去控制代码是否执行达到显影//第三种:挂载在不同对象上通过控制对象的失活激活来达到面板的显影
//2.关键的如何在多个面板之间相互调用显影我们是通过静态变量和静态方法的形式
 

[展开全文]

//注意:
//1.它每帧执行相当于是用于专门绘制GuI界面的函数

//2.一般只在其中执行GUI相关界面绘制和操作逻辑

//3.该函数在onDisable之前 LateUpdate之后执行

//4.只要是继承Mono的脚本都可以在0nGuI中绘制GUI
 

[展开全文]

只要是继承Mono的脚本都可以在OnGUI中绘制GUI

[展开全文]

输入框:

一.普通输入

GUI.TextField(Rect,输入的值,字符串最大长度);

返回输入的值,所以用法和复选框差不多

inputStr = GUI.TextField(Rect,inputStr,MaxLength)

二.密码输入

inputStr = GUI.PasswordField(Rect,inputStr,'一个表示密码的单字符');

拖动条:

三.水平拖动条

float value = GUI.HorizontalSlier(Rect,value(返回值),最小值,最大值);

可以再加三个GUIstyle,分别是按钮,按钮更精细的设置,背景条。

四.竖直拖动条

API:VerticalSlider();使用方法和水平一致

 

 

 

[展开全文]

授课教师

游戏开发前端主程

课程特色

视频(31)
下载资料(30)

学员动态