using UnityEngine; using UnityEngine.UI; public class FullChangeControl : MonoBehaviour { public Button BtnFullScreen; public Button BtnExitFullScreen; void Update() { // 根据全屏状态切换按钮 if (Screen.fullScreen) { BtnFullScreen.gameObject.SetActive(false); BtnExitFullScreen.gameObject.SetActive(true); } else { BtnFullScreen.gameObject.SetActive(true); BtnExitFullScreen.gameObject.SetActive(false); } } // 全屏 public void BtnFullScreenEvent() { if (Screen.fullScreen == false) { Resolution[] resolutions = Screen.resolutions; Screen.SetResolution(resolutions[resolutions.Length - 1].width, resolutions[resolutions.Length - 1].height, true); Screen.fullScreen = true; } } // 退出全屏 public void BtnExitFullScreenEvent() { if (Screen.fullScreen == true) { Screen.SetResolution(960, 540, false); Screen.fullScreen = false; } } }这个代码需要点击两次才能进入全屏状态,我想要点击一次就进入全屏状态
时间: 2024-02-14 16:11:25 浏览: 163
unity 设置屏幕分辨率,并且使用 全屏
5星 · 资源好评率100%
这个问题可能是因为在进入全屏状态之后,屏幕分辨率发生了变化,导致需要点击两次才能成功进入全屏状态。解决这个问题的方法是在进入全屏状态之前,先将屏幕分辨率设置为当前屏幕的分辨率,然后再将屏幕设置为全屏状态。请尝试修改代码如下:
```
// 全屏
public void BtnFullScreenEvent()
{
if (Screen.fullScreen == false)
{
Resolution curResolution = Screen.currentResolution; //获取当前屏幕分辨率
Screen.SetResolution(curResolution.width, curResolution.height, false); //设置为当前分辨率
Screen.fullScreen = true; //设置为全屏状态
}
}
// 退出全屏
public void BtnExitFullScreenEvent()
{
if (Screen.fullScreen == true)
{
Screen.SetResolution(960, 540, false);
Screen.fullScreen = false;
}
}
```
阅读全文