HKI Core
GameSystemUpdateManager.cs
Go to the documentation of this file.
1 #pragma warning disable 0649
2 using UnityEngine;
3 using UnityEngine.SceneManagement;
4 using System.Collections;
5 using HKI.Core.Scenes;
6 using HKI.Core.GameEvents;
7 
8 namespace HKI.Core.GameSystems
9 {
15  public class GameSystemUpdateManager : MonoBehaviour
16  {
17  // Public variables
18  [SerializeField] SceneLoader SceneLoader = null;
19 
23  [SerializeField] [Scene] string FailedInitScene = "";
24 
28  [SerializeField] GameEvent InitFinishedGameEvent = null;
29 
33  [SerializeField] GameSystem[] GameSystemsForInit;
34 
35  [SerializeField] TimeSystem TimeSystem = new TimeSystem();
36 
40  [SerializeField] GameSystem[] GameSystemsForUpdate;
41 
46 
51 
55  [SerializeField] GameSystem[] GameSystemsForShutdown;
56 
57  // Private variables
58  int gameSystemsForUpdateLenght = 0;
59  int gameSystemsForFixedUpdateLenght = 0;
60  int gameSystemsForLateUpdateLenght = 0;
61 
62  // Start function
67  IEnumerator Start()
68  {
69  SceneManager.SetActiveScene(gameObject.scene);
70 
71  TimeSystem.Setup();
72 
73  if(GameSystemsForUpdate != null)
74  gameSystemsForUpdateLenght = GameSystemsForUpdate.Length;
75 
76  if(GameSystemsForFixedUpdate != null)
77  gameSystemsForFixedUpdateLenght = GameSystemsForFixedUpdate.Length;
78 
79  if(GameSystemsForLateUpdate != null)
80  gameSystemsForLateUpdateLenght = GameSystemsForLateUpdate.Length;
81 
82  SceneLoaderController slc = FindObjectOfType<SceneLoaderController>();
83 
84  if(GameSystemsForInit != null)
85  {
86  int gameSystemsForInitLenght = GameSystemsForInit.Length;
87  for(int i = 0; i < gameSystemsForInitLenght; i++)
88  {
89  SceneLoader.ChangeText(GameSystemsForInit[i].GetInitInfoTextLocalization);
90 
91  yield return StartCoroutine(GameSystemsForInit[i].Init());
92 
93  if (!GameSystemsForInit[i].InitSuccessful)
94  {
95  SceneLoader.ChangeText(GameSystemsForInit[i].GetInitErrorInfoTextLocalization);
96 
97  Debug.LogError("(GameSystemUpdateManager) failed to initialize (GameSystem)" + GameSystemsForInit[i].name);
98 
99  if(!string.IsNullOrEmpty(FailedInitScene))
100  {
101  if(SceneLoader != null)
102  SceneLoader.LoadSceneDirect(FailedInitScene);
103  else
104  SceneManager.LoadScene(FailedInitScene);
105  }
106 
107  yield break;
108  }
109  }
110  }
111 
112  if (InitFinishedGameEvent != null)
113  InitFinishedGameEvent.Raise();
114  }
115 
119  void OnDestroy()
120  {
121  if(GameSystemsForShutdown != null)
122  {
123  int gameSystemsForShutdownLenght = GameSystemsForShutdown.Length;
124  for (int i = 0; i < gameSystemsForShutdownLenght; i++)
125  GameSystemsForShutdown[i].Shutdown();
126  }
127 
128  TimeSystem.Shutdown();
129  }
130 
131  // Update functions
135  void Update()
136  {
137  TimeSystem.Update();
138 
139  if(GameSystemsForUpdate != null)
140  {
141  for(int i = 0; i < gameSystemsForUpdateLenght; i++)
142  GameSystemsForUpdate[i].Update();
143  }
144  }
145 
149  void FixedUpdate()
150  {
151  TimeSystem.FixedUpdate();
152 
153  if(GameSystemsForFixedUpdate != null)
154  {
155  for (int i = 0; i < gameSystemsForFixedUpdateLenght; i++)
156  GameSystemsForFixedUpdate[i].FixedUpdate();
157  }
158  }
159 
163  void LateUpdate()
164  {
165  if(GameSystemsForLateUpdate != null)
166  {
167  for (int i = 0; i < gameSystemsForLateUpdateLenght; i++)
168  GameSystemsForLateUpdate[i].LateUpdate();
169  }
170  }
171  }
172 }
This MonoBehaviour controlls all GameSystems. It&#39;s responsble to initialize, update and shutdown this...
IEnumerator Start()
The Start function initialized all GameSystems. For a smoother framerate it&#39;s using coroutines...
GameSystem [] GameSystemsForShutdown
The Shutdown function of this GameSystems will be called on destroy of this MonoBehaviour.
void Raise()
Calling this function will inform all registered listener that this event is raised.
Definition: GameEvent.cs:24
GameSystem [] GameSystemsForUpdate
This GameSystems will be updated on Update.
void OnDestroy()
OnDestroy is responsible for shutting all GameSystems down.
void Update()
The Update function updates the TimeSystem and then updates all GameSystems.
GameSystem [] GameSystemsForLateUpdate
This GameSystems will be updated on LateUpdate.
void ChangeText(TextLocalization textLocalization)
Definition: SceneLoader.cs:55
This ScriptableObject spawns the SceneLoaderController prefab and gives access to the SceneLoaderCont...
Definition: SceneLoader.cs:10
This system stores all engine time values so that can be used by other systems without having to call...
Definition: TimeSystem.cs:10
void LoadSceneDirect(string scene)
Definition: SceneLoader.cs:69
GameSystem [] GameSystemsForInit
This GameSystems will be initialized.
GameSystem [] GameSystemsForFixedUpdate
This GameSystems will be updated on FixedUpdate.
void LateUpdate()
Like the Update function, the LateUpdate function updates the TimeSystem and then updates all GameSys...
This is a ScriptableObject game event. It can be listen too and raised via code or within Unity Edito...
Definition: GameEvent.cs:12
void FixedUpdate()
Like the Update function, the FixedUpdate function updates the TimeSystem and then updates all GameSy...
This MonoBehaviour controls scene changing and the blending of the fading overlay for scene transitio...
This ScriptableObject is the abstract base class for all GameSystems. It provides "interface" functio...
Definition: GameSystem.cs:10