Created
May 6, 2015 20:46
-
-
Save LordNed/e7c6a0e7a82c82de23c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using Newtonsoft.Json; | |
public class GlobalState : MonoBehaviour | |
{ | |
public static GameSettings Settings; | |
/// <summary> | |
/// This is as close to as we can come in Unity to a entry point to the program. The GlobalState | |
/// object should be placed into every scene, any second copy that is loaded will destroy itself | |
/// on load. The ExecutionOrder has been modified to set this Awake() function to be called as | |
/// the very first thing. | |
/// </summary> | |
private void Awake() | |
{ | |
// Prevent duplicates of this object. Duplicates are expected (one per scene) so there is no | |
// warning associated with this. | |
if(FindObjectsOfType<GlobalState>().Length > 1) | |
{ | |
Destroy(gameObject); | |
return; | |
} | |
Debug.Log("[Felbis.Global] GlobalState initialized. Loading and setting default values now."); | |
SetupGameSettings(); | |
} | |
private void OnApplicationQuit() | |
{ | |
// Save our settings to disk. | |
ExternalConfig.SetKey("gameSettings", JsonConvert.SerializeObject(Settings, Formatting.Indented)); | |
Debug.Log("[Felbis.Glboal] GlobalState quitting. Saving settings."); | |
} | |
private void SetupGameSettings() | |
{ | |
// Load and set Graphics Settings. | |
Settings = JsonConvert.DeserializeObject<GameSettings>(ExternalConfig.GetKey("gameSettings", "{}")); | |
// Don't bother setting any of the Rendering/Resolution related settings in the editor - they'll fail. | |
if(!Application.isEditor) | |
{ | |
int unitySavedResolutionWidth = PlayerPrefs.GetInt("Screenmanager Resolution Width"); | |
int unitySavedResolutionHeight = PlayerPrefs.GetInt("Screenmanager Resolution Height"); | |
bool unitySavedIsFullscreen = PlayerPrefs.GetInt("Screenmanager Is Fullscreen mode") == 1; | |
// If they have no external config, we'll just pick up the settings off of Unity. | |
if (Settings.ScreenWidth == 0 || Settings.ScreenHeight == 0) | |
{ | |
Settings.ScreenWidth = unitySavedResolutionWidth; | |
Settings.ScreenHeight = unitySavedResolutionHeight; | |
Settings.IsFullscreen = unitySavedIsFullscreen; | |
} | |
// If Unity's saved version doesn't match our saved version, try using our version. If setting our version fails, | |
// we'll load Unity's instead and set those. This should handle the user switching monitors between application runs. | |
if (unitySavedResolutionWidth != Settings.ScreenWidth || unitySavedResolutionHeight != Settings.ScreenHeight || unitySavedIsFullscreen != Settings.IsFullscreen) | |
{ | |
Debug.Log("[Felbis.Graphics] Local settings cache has mis-matched resolution or fullscreen setting compared to Unity's! Attempting to use ours now: " + Settings.ScreenWidth + "x" + Settings.ScreenHeight); | |
Screen.SetResolution(Settings.ScreenWidth, Settings.ScreenHeight, Settings.IsFullscreen); | |
// Wait until the end of the frame to check if the resolution change was succesful. | |
StartCoroutine(DoEndOfFrameResolutionCheck(unitySavedResolutionWidth, unitySavedResolutionHeight, unitySavedIsFullscreen)); | |
} | |
else | |
{ | |
Settings.ScreenWidth = Screen.width; | |
Settings.ScreenHeight = Screen.height; | |
Settings.IsFullscreen = Screen.fullScreen; | |
Debug.Log(string.Format("[Felbis.Graphics] Initialized game window. Width: {0} Height: {1} Fullscreen: {2} VSync: {3}", Screen.width, Screen.height, Screen.fullScreen, Settings.VSyncEnabled)); | |
} | |
QualitySettings.vSyncCount = Settings.VSyncEnabled ? 1 : 0; | |
} | |
} | |
private IEnumerator DoEndOfFrameResolutionCheck(int fallbackWidth, int fallbackHeight, bool fallbackIsFullScreen) | |
{ | |
// Wait for Unity to finish resizing the window. This seems to take two frames and not one. | |
yield return null; | |
yield return null; | |
// See if we managed to set our version. If we didn't, we're going to fall back to Unity's version. | |
if (Screen.width != Settings.ScreenWidth || Screen.height != Settings.ScreenHeight || Screen.fullScreen != Settings.IsFullscreen) | |
{ | |
Debug.LogWarning(string.Format("[Felbis.Graphics] Failed to set user specified width/height/fullscreen ({0}x{1}, fullscreen: {2}), using Unity's settings now.", Settings.ScreenWidth, Settings.ScreenHeight, Settings.IsFullscreen)); | |
Screen.SetResolution(fallbackWidth, fallbackHeight, fallbackIsFullScreen); | |
yield return null; | |
yield return null; | |
} | |
Settings.ScreenWidth = Screen.width; | |
Settings.ScreenHeight = Screen.height; | |
Settings.IsFullscreen = Screen.fullScreen; | |
Debug.Log(string.Format("[Felbis.Graphics] Initialized game window. Width: {0} Height: {1} Fullscreen: {2} VSync: {3}", Screen.width, Screen.height, Screen.fullScreen, Settings.VSyncEnabled)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment