using System.Collections; using System.Collections.Generic; 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-15 08:27:13 浏览: 105
这个问题可能是因为在设置分辨率和全屏模式时,需要一些时间来完成,所以第一次点击可能会被忽略。你可以尝试在BtnFullScreenEvent和BtnExitFullScreenEvent方法中加入一个小延迟,让程序等待一段时间再执行屏幕分辨率和全屏模式的更改。你可以使用Coroutine来实现这个小延迟,具体做法如下:
public IEnumerator SetFullScreen(bool isFullScreen)
{
yield return new WaitForEndOfFrame(); //等待帧结束
if (isFullScreen == false)
{
Resolution[] resolutions = Screen.resolutions;
Screen.SetResolution(resolutions[resolutions.Length - 1].width, resolutions[resolutions.Length - 1].height, true);
Screen.fullScreen = true;
}
else
{
Screen.SetResolution(960, 540, false);
Screen.fullScreen = false;
}
}
public void BtnFullScreenEvent()
{
if (Screen.fullScreen == false)
{
StartCoroutine(SetFullScreen(true)); //使用Coroutine来实现小延迟
}
}
public void BtnExitFullScreenEvent()
{
if (Screen.fullScreen == true)
{
StartCoroutine(SetFullScreen(false)); //使用Coroutine来实现小延迟
}
}
这样做可能会稍微增加一些代码复杂性,但是可以确保屏幕分辨率和全屏模式的更改能够正常执行,从而避免了需要点击两次的问题。
阅读全文