Unity2d 全屏與窗口的切換

核心代碼

    public void ResolutionSet()
    {
        if (ResoType % 2 == 0)
        {
            Screen.SetResolution(1155, 763, false, 60);
            ResoType++;
            ResoText.text = "窗口";
            return;
        }
        if (ResoType % 2 == 1)
        {
            Screen.SetResolution(2160, 1440, true, 60);
            ResoType++;
            ResoText.text = "全屏";
            return;
        }
    }

完整代碼

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;

    private int ResoType=1;
    public Text ResoText;

    // Start is called before the first frame update
    void Start()
    {
        flag = false;
       
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Alpha1))
        {
            ResolutionSet();
        }
    }

    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 ResolutionSet()
    {
        if (ResoType % 2 == 0)
        {
            Screen.SetResolution(1155, 763, false, 60);
            ResoType++;
            ResoText.text = "窗口";
            return;
        }
        if (ResoType % 2 == 1)
        {
            Screen.SetResolution(2160, 1440, true, 60);
            ResoType++;
            ResoText.text = "全屏";
            return;
        }
    }
}

附完整教程:

Unity2d Rubys Adventure 課程設計報告

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