Danro/Assets/Resources/Scripts/UI/FullscreenToggle.cs

60 lines
1.7 KiB
C#

using UnityEngine;
using UnityEngine.InputSystem;
public class FullscreenToggle : MonoBehaviour
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
static void Bootstrap()
{
var go = new GameObject("FullscreenToggle");
go.AddComponent<FullscreenToggle>();
DontDestroyOnLoad(go);
}
public void OnFullscreen(InputValue value)
{
if (Application.platform != RuntimePlatform.WebGLPlayer)
{
if (value.isPressed) // Проверяем нажатие кнопки
{
Toggle();
}
}
}
//void Update()
//{
// if (Application.platform != RuntimePlatform.WebGLPlayer)
// {
// 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();
// //if (Input.GetButtonDown("Fullscreen")) 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);
}
}
}