Unity C# 分辨率及窗口全屏设置

发布于:2025-05-01 ⋅ 阅读:(15) ⋅ 点赞:(0)
using UnityEngine;

public class FullScreen : MonoBehaviour
{
    private static FullScreen _instance;
    public static FullScreen Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = FindAnyObjectByType(typeof(FullScreen)) as FullScreen;
                if (_instance == null)
                {
                    var go = new GameObject("_FullScreen");
                    DontDestroyOnLoad(go);
                    _instance = go.AddComponent<FullScreen>();
                }
            }
            return _instance;
        }
    }
    
    private int _width;
    private int _height;
    private bool _fullscreen;

    private void Awake()
    {
        SetFullScreen(1920, 1080, true);
    }

    private void SetFullScreen(int inWidth, int inHeight, bool inFullScreen)
    {
        _width = inWidth;
        _height = inHeight;
        _fullscreen = inFullScreen;
        
        Screen.SetResolution(inWidth, inHeight, _fullscreen);
        Screen.fullScreen = _fullscreen;
        Debug.Log(inWidth + " -- " + inHeight + " -- " + _fullscreen);
    }
    
    public void SetFullScreen(int inWidth, int inHeight)
    {
        _width = inWidth;
        _height = inHeight;
        
        Screen.SetResolution(inWidth, inHeight, _fullscreen);
        Screen.fullScreen = _fullscreen;
        Debug.Log(inWidth + " -- " + inHeight + " -- " + _fullscreen);
    }
    
    public void SetFullScreen(bool inFullScreen)
    {
        _fullscreen = inFullScreen;
        
        Screen.SetResolution(_width, _height, inFullScreen);
        Screen.fullScreen = inFullScreen;
        Debug.Log(_width + " -- " + _height + " -- " + inFullScreen);
    }
}
using UnityEngine;

public class SetResolutionButton : MonoBehaviour
{
    public int width;
    public int height;

    public void OnResolutionButtonClick()
    {
        FullScreen.Instance.SetFullScreen(width, height);
    }
}


网站公告

今日签到

点亮在社区的每一天
去签到