GameManager.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using HQFPSWeapons.UserInterface;
  6. namespace HQFPSWeapons
  7. {
  8. public class GameManager : Singleton<GameManager>
  9. {
  10. public Material[] PreloadedMaterials { get { return m_PreloadedMaterials; } set { m_PreloadedMaterials = value; } }
  11. public Player CurrentPlayer { get; private set; }
  12. public UIManager CurrentInterface { get; private set; }
  13. [BHeader("General", true)]
  14. [SerializeField]
  15. private SceneField[] m_GameScenes = null;
  16. [Space]
  17. [SerializeField]
  18. private Texture2D m_CustomCursorTex = null;
  19. [Space]
  20. [SerializeField]
  21. [Tooltip("This will help with stuttering and lag when loading new objects for the first time, but will increase the memory usage right away.")]
  22. private bool m_PreloadMaterialsInEditor = false;
  23. [SerializeField]
  24. private Material[] m_PreloadedMaterials = null;
  25. public void Quit()
  26. {
  27. Application.Quit();
  28. }
  29. /// <summary> Starting the game with an index of -1 is going to reload the current scene</summary>
  30. public void StartGame(int index = -1)
  31. {
  32. Cursor.visible = false;
  33. Cursor.lockState = CursorLockMode.Locked;
  34. // Load the game scene
  35. if (index == -1)
  36. SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().name, LoadSceneMode.Single);
  37. else
  38. SceneManager.LoadSceneAsync(m_GameScenes[index].SceneName, LoadSceneMode.Single);
  39. }
  40. public void SetPlayerPosition()
  41. {
  42. //Set the position and rotation with the random spawn point transform
  43. CurrentPlayer.transform.position = GetSpawnPoint();
  44. CurrentPlayer.transform.rotation = GetSpawnRotation();
  45. }
  46. private Vector3 GetSpawnPoint()
  47. {
  48. //get a random spawn point
  49. var spawnPoints = FindObjectOfType<PlayerSpawnPoints>();
  50. Vector3 spawnPoint = CurrentPlayer.transform.position;
  51. if (spawnPoints != null)
  52. {
  53. var newSpawnPoint = spawnPoints.GetRandomSpawnPoint();
  54. if (newSpawnPoint != Vector3.zero)
  55. spawnPoint = newSpawnPoint;
  56. }
  57. return spawnPoint;
  58. }
  59. private Quaternion GetSpawnRotation()
  60. {
  61. var spawnPoints = FindObjectOfType<PlayerSpawnPoints>();
  62. Quaternion spawnRotation = CurrentPlayer.transform.rotation;
  63. if (spawnPoints != null)
  64. spawnRotation = spawnPoints.GetRandomRotation();
  65. return spawnRotation;
  66. }
  67. private void OnEnable()
  68. {
  69. SceneManager.sceneLoaded += OnSceneLoaded;
  70. }
  71. private void OnDestroy()
  72. {
  73. SceneManager.sceneLoaded -= OnSceneLoaded;
  74. }
  75. private void Awake()
  76. {
  77. if (Instance != null && Instance != this)
  78. {
  79. Destroy(gameObject);
  80. return;
  81. }
  82. if(Application.isEditor && m_PreloadMaterialsInEditor)
  83. {
  84. List<GameObject> preloadObjects = new List<GameObject>();
  85. Camera camera = new GameObject("Material Preload Camera", typeof(Camera)).GetComponent<Camera>();
  86. camera.orthographic = true;
  87. camera.orthographicSize = 100f;
  88. camera.farClipPlane = 100f;
  89. camera.depth = 999;
  90. camera.renderingPath = RenderingPath.Forward;
  91. camera.useOcclusionCulling = camera.allowHDR = camera.allowMSAA = camera.allowDynamicResolution = false;
  92. preloadObjects.Add(camera.gameObject);
  93. foreach(var mat in m_PreloadedMaterials)
  94. {
  95. if(mat == null)
  96. continue;
  97. var quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
  98. quad.transform.position = camera.transform.position + camera.transform.forward * 50f + camera.transform.right * UnityEngine.Random.Range(-100f, 100f) + camera.transform.up * UnityEngine.Random.Range(-100f, 100f);
  99. quad.transform.localScale = Vector3.one * 0.01f;
  100. quad.GetComponent<Renderer>().sharedMaterial = mat;
  101. preloadObjects.Add(quad);
  102. }
  103. camera.Render();
  104. foreach(var obj in preloadObjects)
  105. Destroy(obj);
  106. preloadObjects.Clear();
  107. }
  108. if(m_CustomCursorTex != null)
  109. Cursor.SetCursor(m_CustomCursorTex, Vector2.zero, CursorMode.Auto);
  110. DontDestroyOnLoad(gameObject);
  111. }
  112. private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
  113. {
  114. CurrentPlayer = FindObjectOfType<Player>();
  115. CurrentInterface = FindObjectOfType<UIManager>();
  116. CurrentInterface.AttachToPlayer(CurrentPlayer);
  117. }
  118. private void Start()
  119. {
  120. Shader.WarmupAllShaders();
  121. GC.Collect();
  122. }
  123. }
  124. }