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 课程设计报告

你可能感兴趣的:(Unity,游戏设计)