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.
 
 
 
 

97 lines
2.3 KiB

using System.Collections;
using System.Collections.Generic;
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 fps = 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 = -1;
public float currentFps = 30.0f;
[Header( "Event" )]
public UnityEvent onQuit = new UnityEvent();
private void Awake()
{
Application.targetFrameRate = fps;
#if UNITY_STANDALONE || UNITY_EDITOR
if ( mode == FullScreenMode.ExclusiveFullScreen )
{
var displays = Display.displays;
int num = Mathf.Min( Mathf.Max( displayCount, 1 ), displays.Length );
for ( int 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.isPressed )
#else
if ( Input.GetKey( KeyCode.Escape ) )
#endif
{
onQuit.Invoke();
Application.Quit();
}
}
}
}
}