Skip to content

Instantly share code, notes, and snippets.

@spdp-dev
Last active May 8, 2023 17:32
Show Gist options
  • Save spdp-dev/ee09564557f3b80536e808ebfc5c3c55 to your computer and use it in GitHub Desktop.
Save spdp-dev/ee09564557f3b80536e808ebfc5c3c55 to your computer and use it in GitHub Desktop.
A fullscreen manager for Unity projects. With Windows/Linux/macOS exports, I typically have them start in a 720p window so as to be streamer friendly. This script then allows the game to switch from 720p to the user's native resolution and back again. See the ToggleFullScreen method for valid key combos.
#if PLATFORM_STANDALONE
using UnityEngine;
public class FullscreenManager : MonoBehaviour
{
static FullscreenManager instance;
public static FullscreenManager Instance
{
get
{
if (instance == null)
instance = GameObject.FindObjectOfType<FullscreenManager>();
if (instance == null)
instance = Instantiate(new GameObject("FullscreenManager")).AddComponent<FullscreenManager>();
return instance;
}
}
const int DEFAULT_WIDTH = 1280;
const int DEFAULT_HEIGHT = 720;
void Awake()
{
EnforceSingleInstance();
}
void Update()
{
ToggleFullScreen();
}
void DisableFullScreen() => Screen.SetResolution(DEFAULT_WIDTH, DEFAULT_HEIGHT, false);
void EnableFullScreen() => Screen.SetResolution(Screen.currentResolution.width, Screen.currentResolution.height, true);
void EnforceSingleInstance()
{
if (instance == null)
instance = this;
else if (instance != this)
Destroy(gameObject);
DontDestroyOnLoad(gameObject);
}
void ToggleFullScreen()
{
if (Input.GetKeyDown(KeyCode.Escape))
DisableFullScreen();
if ((Input.GetKey(KeyCode.LeftAlt) && Input.GetKeyDown(KeyCode.Return)) ||
(Input.GetKey(KeyCode.LeftAlt) && Input.GetKeyDown(KeyCode.F)) ||
(Input.GetKey(KeyCode.LeftControl) && Input.GetKeyDown(KeyCode.Return)) ||
(Input.GetKey(KeyCode.LeftControl) && Input.GetKeyDown(KeyCode.F)) ||
(Input.GetKey(KeyCode.LeftCommand) && Input.GetKeyDown(KeyCode.Return)) ||
(Input.GetKey(KeyCode.LeftCommand) && Input.GetKeyDown(KeyCode.F)))
{
if (Screen.fullScreen)
DisableFullScreen();
else
EnableFullScreen();
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment