You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

95 lines
2.3 KiB

using UnityEngine;
using UnityEngine.Events;
namespace UltraCombos
{
public class DisplayConfig : MonoBehaviour
{
[Header("Display")]
[SerializeField] private FullScreenMode mode = FullScreenMode.Windowed;
[SerializeField] private int displayCount = 1;
[SerializeField] private Vector2Int resolution = new Vector2Int(1600, 900);
[SerializeField] private int targetFps = 60;
[Header("Misc")]
[Range(0, 10)]
[SerializeField] private float autoHideCursor = 0;
private float cursorStamp;
private Vector2 cursorLastPosition;
[SerializeField] private bool exitOnEsc = true;
private float timestamp = 0;
[field: SerializeField, ReadOnly] public float CurrentFps { get; private set; } = 30.0f;
[Header("Event")]
public UnityEvent onQuit = new UnityEvent();
private void Awake()
{
Application.targetFrameRate = targetFps;
#if UNITY_STANDALONE || UNITY_EDITOR
if (mode == FullScreenMode.ExclusiveFullScreen)
{
var displays = Display.displays;
var num = Mathf.Min(Mathf.Max(displayCount, 1), displays.Length);
for (var i = 0; i < num; ++i)
{
displays[i].Activate();
}
Screen.SetResolution(Screen.width, Screen.height, mode);
}
else if (mode == FullScreenMode.FullScreenWindow)
{
Screen.SetResolution(Screen.width, Screen.height, mode);
}
else
{
Screen.SetResolution(resolution.x, resolution.y, mode);
}
#else
Screen.sleepTimeout = SleepTimeout.NeverSleep;
#endif
}
private void Update()
{
if (autoHideCursor > 0)
{
#if ENABLE_INPUT_SYSTEM
var pos = UnityEngine.InputSystem.Mouse.current.position.ReadValue();
#else
var pos = (Vector2)Input.mousePosition;
#endif
var bias = pos - cursorLastPosition;
if (bias.magnitude > 1)
{
cursorLastPosition = pos;
cursorStamp = Time.time;
}
Cursor.visible = Time.time - cursorStamp < autoHideCursor;
}
if (Time.time - timestamp > 0)
{
CurrentFps = Mathf.Lerp(CurrentFps, 1.0f / (Time.time - timestamp), Time.deltaTime);
timestamp = Time.time;
}
if (exitOnEsc)
{
#if ENABLE_INPUT_SYSTEM
if (UnityEngine.InputSystem.Keyboard.current.escapeKey.wasPressedThisFrame)
#else
if (Input.GetKeyDown(KeyCode.Escape))
#endif
{
onQuit.Invoke();
Application.Quit();
}
}
}
}
}