using System.Collections;
using AibisDream.Framework;
using AibisDream.Kit;
using UnityEngine;
namespace AibisDream
{
/// 应用窗口模式并在实际分辨率稳定后发送显示变化通知。
public sealed class DisplaySettingsController : Singleton
{
private Coroutine _applyRoutine;
public void ApplyWindowMode(string windowMode)
{
if (!TryResolveMode(windowMode, out var width, out var height, out var mode))
{
Debug.LogWarning($"[DisplaySettingsController] Unknown window mode: {windowMode}");
return;
}
Screen.SetResolution(width, height, mode);
if (_applyRoutine != null)
{
StopCoroutine(_applyRoutine);
}
_applyRoutine = StartCoroutine(WaitForResolution(width, height));
}
private IEnumerator WaitForResolution(int targetWidth, int targetHeight)
{
var deadline = Time.unscaledTime + 1f;
while ((Screen.width != targetWidth || Screen.height != targetHeight)
&& Time.unscaledTime < deadline)
{
yield return null;
}
_applyRoutine = null;
EnumEventSystem.Global.Send(SettingChangeEvent.DisplayChanged);
}
private static bool TryResolveMode(
string windowMode,
out int width,
out int height,
out FullScreenMode mode)
{
switch (windowMode)
{
case "FullScreen":
case "MaximizedWindow":
width = Screen.currentResolution.width;
height = Screen.currentResolution.height;
mode = FullScreenMode.FullScreenWindow;
return true;
case "Windowed":
width = 1920;
height = 1080;
mode = FullScreenMode.Windowed;
return true;
default:
width = 0;
height = 0;
mode = FullScreenMode.Windowed;
return false;
}
}
}
}