Unity UI界面的設計(完整版)

目錄

         一、初始界面

                  開始

關於

切換操作方式

菜單界面完整代碼

二、遊戲界面

幫助

重新開始遊戲

音樂的開關

退出遊戲

返回菜單

調節音量

遊戲界面完整代碼


一、初始界面

 

點擊"關於"時,按鈕的flag值改變,按鈕的文字也改變

前兩個按鈕根據"關於"鍵的flag值,執行不同的功能

關於

    public void About()
    {
        if (flag == false)
        {
            about.SetActive(true);
            flag = true;
            return;
        }
 
        if (flag == true)
        {
            about.SetActive(false);
            flag = false;
            return;
        }
    }

切換操作方式

    public void ctrl()
    {
        Debug.Log(ctrlflag);
        if(ctrlflag==1)
        {
            ctrlflag=2;
            ctrltext.text = "鼠標";
            ctrltext.fontSize = 49;
        }
        else if (ctrlflag == 2)
        {
            ctrlflag=3;
            ctrltext.text = "鍵盤";
            ctrltext.fontSize = 49;
        }
        else if (ctrlflag == 3)
        {
            ctrlflag=1;
            ctrltext.text = "鼠標+鍵盤";
            ctrltext.fontSize = 28;
        }
    }

打開網頁

Application.OpenURL("https://blog.csdn.net/weixin_43673589");

菜單界面完整代碼

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

public class UIEntry : MonoBehaviour
{
    public GameObject about;
    private bool flag;
    public static int ctrlflag=1;
    public Text ctrltext;
    public Text Button1;
    public Text Button2;
    
    // Start is called before the first frame update
    void Start()
    {
        flag = false;
       
    }

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

    public void StartGame()
    {
        if (flag == false)
        {
            SceneManager.LoadScene(1);
        }
        else
        {
            Application.OpenURL("https://blog.csdn.net/weixin_43673589");
        }
       
    }

    public void About()
    {
        //顯示遊戲介紹
        if (flag == false)
        {
            //介紹文本顯示
            about.SetActive(true);
            flag = true;

            //按鈕1從開始遊戲變更爲博客
            Button1.text = "作者博客";
            Button1.fontSize = 32;

            //按鈕1從開始遊戲變更爲博客
            Button2.text = "課設回顧";
            Button2.fontSize = 32;
            return;
        }

        //隱藏遊戲介紹
        if (flag == true)
        {
            about.SetActive(false);
            flag = false;

            //按鈕1從開始遊戲變更爲博客
            Button1.text = "開始";
            Button1.fontSize = 49;

            //按鈕1從開始遊戲變更爲博客
            Button2.text = "鼠標+鍵盤";
            Button2.fontSize = 28;
            return;
        }
    }

    public void Blog()
    {
        Application.OpenURL("https://blog.csdn.net/weixin_43673589");
    }

    public void ctrl()
    {
        if (flag == false)
        {
            Debug.Log(ctrlflag);
            if (ctrlflag == 1)
            {
                ctrlflag = 2;
                ctrltext.text = "鼠標";
                ctrltext.fontSize = 49;
            }
            else if (ctrlflag == 2)
            {
                ctrlflag = 3;
                ctrltext.text = "鍵盤";
                ctrltext.fontSize = 49;
            }
            else if (ctrlflag == 3)
            {
                ctrlflag = 1;
                ctrltext.text = "鼠標+鍵盤";
                ctrltext.fontSize = 28;
            }
        }
        else
        {
            Application.OpenURL("https://blog.csdn.net/weixin_43673589/article/details/106577768");
        }
    }
}

二、遊戲界面

幫助

    //彈出幫助
    public void helpButton()
    {
        if (helpflag == false)
        {
            help.SetActive(true);
            helpflag = true;
            return;
        }
 
        if (helpflag == true)
        {
            help.SetActive(false);
            helpflag = false;
            return;
        }
    }

重新開始遊戲

    //重新開始遊戲
    public void ReStart()
    {
        int index = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(index);
    }

音樂的開關

    //靜音
    public void MusicButton()
    {
        if (musicflag == false)
        {
            volume.volume = 1;
            //music.SetActive(true);
            musicflag = true;
            musicstatus.text = "音樂:開";
            return;
        }
 
        if (musicflag == true)
        {
            volume.volume = 0;
            //music.SetActive(false);
            musicflag = false;
            musicstatus.text = "音樂:關";
            return;
        }
    }

退出遊戲

    //退出遊戲
    public void ExitGame()
    {
        Application.Quit();
    }

返回菜單

    //返回菜單
    public void Menu()
    {
        SceneManager.LoadScene(0);
    }

調節音量

    //調節音量
    public void AdjustVolume(Scrollbar volumebar)
    {
        volume.volume = volumebar.value;
    }

遊戲界面完整代碼

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
 
public class UIManager : MonoBehaviour
{
    public static UIManager instance { get; private set; }
    
    //靜音設置
    public GameObject music;
    public Text musicstatus;
    private bool musicflag=true;
 
    //音量控制
    public AudioSource volume;
    public Scrollbar volumebar;
 
    //幫助按鈕
    public GameObject help;
    private bool helpflag;
 
    //顯示Bar:血量、子彈、時間、敵人
    public Image healthBar;
    public Text bulletCountText;
    public Text TimeBar;
    public Text EnemyLeftBar;
 
    private void Awake()
    {
        instance = this;
    }
 
    //更新血條
    public void UpdateHealthBar(int curAmount,int maxAmount)
    {
        healthBar.fillAmount = (float)curAmount / (float)maxAmount;
    }
    //更新子彈
    public void UpdateBulletCount(int curAmount,int maxAmount)
    {
        bulletCountText.text = curAmount.ToString()+"/"+maxAmount.ToString();
    }
 
    //刷新時間
    public void UpdateTimeBar(int curtime)
    {
        int min = curtime / 60;
        int sec = curtime % 60;
        if (sec < 10 && min<10)
        {
            TimeBar.text = "0"+min.ToString() + ":0" + sec.ToString();
        }
        if (sec > 10 && min < 10)
        {
            TimeBar.text = "0" + min.ToString() + ":" + sec.ToString();
        }
    }
 
    //更新敵人數
    public void UpdateEnemyLeft(int enemyleft)
    {
        EnemyLeftBar.text = enemyleft.ToString();
    }
 
    //退出遊戲
    public void ExitGame()
    {
        Application.Quit();
    }
 
    //靜音
    public void MusicButton()
    {
        if (musicflag == false)
        {
            volume.volume = 1;
            //music.SetActive(true);
            musicflag = true;
            musicstatus.text = "音樂:開";
            return;
        }
 
        if (musicflag == true)
        {
            volume.volume = 0;
            //music.SetActive(false);
            musicflag = false;
            musicstatus.text = "音樂:關";
            return;
        }
    }
    
    //重新開始遊戲
    public void ReStart()
    {
        int index = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(index);
    }
 
    //返回菜單
    public void Menu()
    {
        SceneManager.LoadScene(0);
    }
 
    //調節音量
    public void AdjustVolume(Scrollbar volumebar)
    {
        volume.volume = volumebar.value;
    }
 
    //彈出幫助
    public void helpButton()
    {
        if (helpflag == false)
        {
            help.SetActive(true);
            helpflag = true;
            return;
        }
 
        if (helpflag == true)
        {
            help.SetActive(false);
            helpflag = false;
            return;
        }
    }
}

附完整教程:

Unity2d Rubys Adventure 課程設計報告

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章