Danro/Assets/Resources/Scripts/FullscreenToggle.cs
2025-09-18 17:05:15 +03:00

44 lines
1.3 KiB
C#

using UnityEngine;
using UnityEngine.InputSystem;
public class GlobalFullscreenHotkey : MonoBehaviour
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
static void Bootstrap()
{
var go = new GameObject("GlobalFullscreenHotkey");
go.AddComponent<GlobalFullscreenHotkey>();
Object.DontDestroyOnLoad(go);
}
void Update()
{
var kb = Keyboard.current;
if (kb == null) return;
bool f11 = kb.f11Key.wasPressedThisFrame;
bool altEnter = kb.enterKey.wasPressedThisFrame && (kb.leftAltKey.isPressed || kb.rightAltKey.isPressed);
if (f11 || altEnter) Toggle();
}
static void Toggle()
{
bool fs = !Screen.fullScreen;
Screen.fullScreenMode = fs ? FullScreenMode.FullScreenWindow : FullScreenMode.Windowed;
if (fs)
{
Screen.SetResolution(Display.main.systemWidth, Display.main.systemHeight, true);
Screen.fullScreen = true;
}
else
{
int w = Mathf.RoundToInt(Display.main.systemWidth * 0.5f);
int h = Mathf.RoundToInt(Display.main.systemHeight * 0.5f);
Screen.fullScreen = false;
Screen.SetResolution(w, h, false);
}
}
}