HKI Core
SceneLoaderController.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using UnityEngine.UI;
3 using UnityEngine.SceneManagement;
4 using System.Collections;
5 using HKI.Core.UI;
6 using HKI.Core.Loc;
7 using HKI.Core.GameEvents;
8 
9 namespace HKI.Core.Scenes
10 {
14  public class SceneLoaderController : MonoBehaviour
15  {
16  // Public variables
20  [SerializeField] GameObject FaderGO = null;
21 
25  [SerializeField] CanvasGroup FaderCanvasGroup = null;
26 
30  [SerializeField] GameObject VisualsGO = null;
31 
35  [SerializeField] GameObject InfoTextGO = null;
36 
40  [SerializeField] GameEvent LoadingSceneUnloadedEvent = null;
41 
42  // Private variables
43  TMProAndUGUITextAccessor infoText = null;
44 
45  // Awake function
46  void Awake()
47  {
48  infoText = new TMProAndUGUITextAccessor(InfoTextGO);
49 
50  infoText.ChangeText("");
51 
52  FaderCanvasGroup.alpha = 0.0f;
53 
54  HideVisuals();
55  }
56 
57  // Interface functions
58  public void ShowVisuals()
59  {
60  if (VisualsGO != null)
61  VisualsGO.SetActive(true);
62  }
63 
64  public void HideVisuals()
65  {
66  if (VisualsGO != null)
67  VisualsGO.SetActive(false);
68  }
69 
70  public void ChangInfoText(TextLocalization textLocalization, Language textLanguage)
71  {
72  if(textLocalization == null)
73  {
74  Debug.LogError("(SceneLoaderController) you are trying to change the info text but the given textLocalization is null!)");
75  return;
76  }
77 
78  infoText.ChangeText(textLocalization.GetText(textLanguage));
79  }
80 
90  public void LoadScene(string loadingScene, string scene, bool directLoad = true, float fadeInTime = 1.0f, float fadeOutTime = 1.0f, bool doFading = true)
91  {
92  if(!DoesSceneExist(scene))
93  {
94  Debug.LogError("(SceneLoaderController) you are trying to load a scene that doesn't exist! (Maybe you need to add the scene to the build settings scenes!)");
95  return;
96  }
97 
98  if (directLoad)
99  StartCoroutine(SceneLoadingDirectly(scene, fadeInTime, fadeOutTime, doFading));
100  else
101  StartCoroutine(SceneLoadingWithLoadingScene(loadingScene, scene, fadeInTime, fadeOutTime, doFading));
102  }
103 
108  public void LoadSceneAdditively(string scene)
109  {
110  if (!DoesSceneExist(scene))
111  {
112  Debug.LogError("(SceneLoader) you are trying to load a scene that doesn't exist! (Maybe you need to add the scene to the build settings scenes!)");
113  return;
114  }
115 
116  StartCoroutine(SceneLoadingAdditively(scene));
117  }
118 
119  // Coroutine functions
128  IEnumerator SceneLoadingDirectly(string scene, float fadeInTime, float fadeOutTime, bool doFading)
129  {
130  if(doFading)
131  yield return StartCoroutine(Fade(fadeInTime, true));
132 
133  yield return SceneManager.LoadSceneAsync(scene, LoadSceneMode.Single);
134 
135  if(doFading)
136  yield return StartCoroutine(Fade(fadeOutTime, false));
137  }
138 
148  IEnumerator SceneLoadingWithLoadingScene(string loadingScene, string scene, float fadeInTime, float fadeOutTime, bool doFading)
149  {
150  if (doFading)
151  yield return StartCoroutine(Fade(fadeInTime, true));
152 
153  yield return SceneManager.LoadSceneAsync(loadingScene, LoadSceneMode.Single);
154 
155  ShowVisuals();
156 
157  infoText.ChangeText("");
158 
159 
160  if (doFading)
161  yield return StartCoroutine(Fade(fadeOutTime, false));
162 
163 
164  yield return SceneLoadingAdditively(scene);
165 
166  yield return new WaitForSecondsRealtime(1.0f);
167 
168  if (doFading)
169  yield return StartCoroutine(Fade(fadeInTime, true));
170 
171  if (LoadingSceneUnloadedEvent != null)
172  LoadingSceneUnloadedEvent.Raise();
173 
174  HideVisuals();
175 
176  yield return SceneManager.UnloadSceneAsync(loadingScene);
177 
178  if(doFading)
179  yield return StartCoroutine(Fade(fadeOutTime, false));
180  }
181 
187  IEnumerator SceneLoadingAdditively(string scene)
188  {
189  yield return SceneManager.LoadSceneAsync(scene, LoadSceneMode.Additive);
190  }
191 
192  // Helper functions
193  bool DoesSceneExist(string scene)
194  {
195  return !string.IsNullOrEmpty(scene);
196  }
197 
204  IEnumerator Fade(float fadingTime, bool fadeIn)
205  {
206  float timeSinceFadingStart = 0.0f;
207 
208  FaderCanvasGroup.alpha = fadeIn ? 0.0f : 1.0f;
209 
210  while (timeSinceFadingStart < fadingTime)
211  {
212  timeSinceFadingStart += Time.unscaledDeltaTime;
213 
214  if(fadeIn)
215  FaderCanvasGroup.alpha = Mathf.Lerp(0.0f, 1.0f, timeSinceFadingStart / fadingTime);
216  else
217  FaderCanvasGroup.alpha = Mathf.Lerp(1.0f, 0.0f, timeSinceFadingStart / fadingTime);
218 
219  yield return null;
220  }
221 
222  FaderCanvasGroup.alpha = fadeIn ? 1.0f : 0.0f;
223 
224  yield break;
225  }
226  }
227 }
IEnumerator Fade(float fadingTime, bool fadeIn)
Coroutine implementation of fading.
void LoadSceneAdditively(string scene)
This function will load a scene to the existing scene additively.
This ScriptableObject contains as a container for all text localization information.
void Raise()
Calling this function will inform all registered listener that this event is raised.
Definition: GameEvent.cs:24
string GetText(Language language)
This function returns the string in a specific language. If there is no localization element in the r...
This class allows the use of the standard Unity UI Text component and the TextMeshPro component witho...
IEnumerator SceneLoadingWithLoadingScene(string loadingScene, string scene, float fadeInTime, float fadeOutTime, bool doFading)
Coroutine implementation of loading a new scene via a loading scene.
Special Variable enables the use of the SystemLanguage data type as a variable of the settings system...
Definition: Language.cs:13
IEnumerator SceneLoadingAdditively(string scene)
Coroutine implementation of loading a scene additively to another.
This is a ScriptableObject game event. It can be listen too and raised via code or within Unity Edito...
Definition: GameEvent.cs:12
This MonoBehaviour controls scene changing and the blending of the fading overlay for scene transitio...
void LoadScene(string loadingScene, string scene, bool directLoad=true, float fadeInTime=1.0f, float fadeOutTime=1.0f, bool doFading=true)
This function is for switching from one scene to another.
void ChangInfoText(TextLocalization textLocalization, Language textLanguage)
IEnumerator SceneLoadingDirectly(string scene, float fadeInTime, float fadeOutTime, bool doFading)
Coroutine implementation of direct loading a new scene.