SanAndreasUnity/Assets/Scripts/Behaviours/GameManager.cs

134 lines
3.1 KiB
C#
Raw Normal View History

2020-05-31 17:07:22 +00:00
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace SanAndreasUnity.Behaviours {
public class SceneChangedMessage {
public Scene s1;
public Scene s2;
}
public class GameManager : MonoBehaviour {
public static GameManager Instance { get ; private set ; }
public static bool CursorLocked { get ; private set ; }
public Texture2D logoTexture = null;
public GameObject barPrefab;
2019-06-22 18:42:12 +00:00
[SerializeField] [Range(10, 100)] private int m_defaultMaxFps = 60;
[SerializeField] [Range(10, 100)] private int m_defaultMaxFpsOnMobile = 25;
2019-06-22 18:42:12 +00:00
2019-07-27 16:23:24 +00:00
[SerializeField] [Range(5, 100)] int m_defaultPhysicsUpdateRate = 30;
[SerializeField] [Range(5, 100)] int m_defaultPhysicsUpdateRateOnMobile = 20;
2021-01-04 00:28:58 +00:00
public static int DefaultLayerIndex => 0;
2019-06-23 00:13:55 +00:00
public Vector2 cursorSensitivity = new Vector2(2f, 2f);
2020-05-31 17:07:22 +00:00
/// <summary> Are we in a startup scene ? </summary>
public static bool IsInStartupScene { get { return UnityEngine.SceneManagement.SceneManager.GetActiveScene ().buildIndex == 0; } }
private void Awake() {
if (null == Instance)
Instance = this;
2019-07-27 16:23:24 +00:00
// set default max fps
if (Application.isMobilePlatform)
SetMaxFps(m_defaultMaxFpsOnMobile);
else
SetMaxFps(m_defaultMaxFps);
2019-06-22 18:42:12 +00:00
2019-07-27 16:23:24 +00:00
// set default physics update rate
if (Application.isMobilePlatform)
Time.fixedDeltaTime = 1.0f / m_defaultPhysicsUpdateRateOnMobile;
else
Time.fixedDeltaTime = 1.0f / m_defaultPhysicsUpdateRate;
2020-05-31 17:07:22 +00:00
}
void OnEnable ()
{
SceneManager.activeSceneChanged += this.OnSceneChangedInternal;
}
void OnDisable ()
{
SceneManager.activeSceneChanged -= this.OnSceneChangedInternal;
}
void OnSceneChangedInternal (Scene s1, Scene s2)
{
Utilities.F.SendMessageToObjectsOfType<MonoBehaviour>("OnSceneChanged", new SceneChangedMessage() {s1 = s1, s2 = s2});
}
void Start () {
}
void Update () {
// Fix cursor state if it has been 'broken', happens eg. with zoom gestures in the editor in macOS
if (CursorLocked && ((Cursor.lockState != CursorLockMode.Locked) || (Cursor.visible)))
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
public static bool CanPlayerReadInput() {
return Loader.HasLoaded && !UI.PauseMenu.IsOpened;
}
public static void ChangeCursorState(bool locked, bool updateVisibility = true)
2020-05-31 17:07:22 +00:00
{
CursorLocked = locked;
Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None;
if (updateVisibility)
Cursor.visible = !locked;
2020-05-31 17:07:22 +00:00
}
public static void ExitApplication() {
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit ();
#endif
}
public static void SetMaxFps (int maxFps)
{
QualitySettings.vSyncCount = 0;
Application.targetFrameRate = maxFps;
}
public static int GetMaxFps ()
{
if (!IsFpsLimited ())
return 0;
return Application.targetFrameRate;
}
public static bool IsFpsLimited ()
{
return QualitySettings.vSyncCount == 0;
}
}
}