HKI Core
SceneLoader.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using HKI.Core.Loc;
3 using HKI.Core.Init;
4 
5 namespace HKI.Core.Scenes
6 {
10  public class SceneLoader : ScriptableObject, IInit
11  {
12  // Public variables
13  [SerializeField] [Scene] string LoadingScene = "";
14 
15  [SerializeField] GameObject SceneLoaderControllerPrefab = null;
16 
17  [SerializeField] Language TextLanguage = null;
18 
19  [SerializeField] bool DoFading = true;
20  [SerializeField] float FadeInTime = 1.0f;
21  [SerializeField] float FadeOutTime = 1.0f;
22 
23  // Private variables
24  GameObject sceneLoaderControllerGO = null;
25  SceneLoaderController sceneLoaderController = null;
26 
27  // Init function
28  public void Init()
29  {
30  if(string.IsNullOrEmpty(LoadingScene))
31  Debug.LogError("(SceneLoader) the LoadingScene is null!");
32 
33  if(SceneLoaderControllerPrefab != null)
34  {
35  sceneLoaderControllerGO = GameObject.Instantiate<GameObject>(SceneLoaderControllerPrefab);
36  sceneLoaderController = sceneLoaderControllerGO.GetComponent<SceneLoaderController>();
37 
38  if(sceneLoaderController == null)
39  Debug.LogError("(SceneLoader) the SceneLoaderControllerPrefab has no SceneLoaderController attached to it!");
40  else
41  DontDestroyOnLoad(sceneLoaderControllerGO);
42  }
43  else
44  Debug.LogError("(SceneLoader) the SceneLoaderControllerPrefab is null!");
45  }
46 
47  // OnDisable function
48  void OnDisable()
49  {
50  if(sceneLoaderControllerGO != null)
51  Destroy(sceneLoaderControllerGO);
52  }
53 
54  // Interface function
55  public void ChangeText(TextLocalization textLocalization)
56  {
57  if(sceneLoaderController != null)
58  sceneLoaderController.ChangInfoText(textLocalization, TextLanguage);
59  else
60  Debug.LogError("(SceneLoader) you are trying to change the info text but there is no SceneLoaderController available. (Maybe the loading scene doesn't have one or you are using direct load)");
61  }
62 
64  {
65  return sceneLoaderController;
66  }
67 
68 
69  public void LoadSceneDirect(string scene)
70  {
71  LoadScene(scene, true);
72  }
73 
74  public void LoadSceneIndirect(string scene)
75  {
76  LoadScene(scene, false);
77  }
78 
79  public void LoadSceneAdditively(string scene)
80  {
81  if(sceneLoaderController == null)
82  {
83  Debug.LogError("(SceneLoader) you are trying to load a scene but there is no SceneLoaderController available. (Maybe the loading scene doesn't have one or you are using direct load)");
84  return;
85  }
86 
87  sceneLoaderController.LoadSceneAdditively(scene);
88  }
89 
90  public void QuitApplication()
91  {
92  Application.Quit();
93  }
94 
95  // Helper functions
96  void LoadScene(string scene, bool directLoad)
97  {
98  if (sceneLoaderController == null)
99  {
100  Debug.LogError("(SceneLoader) you are trying to load a scene but there is no SceneLoaderController available. (Maybe the loading scene doesn't have one or you are using direct load)");
101  return;
102  }
103 
104  sceneLoaderController.LoadScene(LoadingScene, scene, directLoad, FadeInTime, FadeOutTime, DoFading);
105  }
106  }
107 }
This interface allows the initialization of anything by the Initialzer.
Definition: IInit.cs:8
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 LoadScene(string scene, bool directLoad)
Definition: SceneLoader.cs:96
void ChangeText(TextLocalization textLocalization)
Definition: SceneLoader.cs:55
void LoadSceneIndirect(string scene)
Definition: SceneLoader.cs:74
This ScriptableObject spawns the SceneLoaderController prefab and gives access to the SceneLoaderCont...
Definition: SceneLoader.cs:10
void LoadSceneDirect(string scene)
Definition: SceneLoader.cs:69
Special Variable enables the use of the SystemLanguage data type as a variable of the settings system...
Definition: Language.cs:13
void LoadSceneAdditively(string scene)
Definition: SceneLoader.cs:79
This MonoBehaviour controls scene changing and the blending of the fading overlay for scene transitio...
SceneLoaderController GetSceneLoaderController()
Definition: SceneLoader.cs:63
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)