HKI Core
Initializer.cs
Go to the documentation of this file.
1 #pragma warning disable 0649
2 using UnityEngine;
3 using HKI.Core.Debuging;
4 
5 namespace HKI.Core.Init
6 {
10  [CreateAssetMenu(fileName = "Initializer", menuName = "HKI/General/Initializer", order = 0)]
11  public class Initializer : ScriptableObject, IInit
12  {
13  // Public variables
17  [Header("Debug system")]
18  [SerializeField] bool UseDebugSystem = true;
19 
23  [SerializeField] GameObject DebugSystemPrefab = null;
24 
28  [Header("ScriptableObjects that are required at start")]
29  [SerializeField] ScriptableObject[] ScriptableObjects;
30 
34  [Header("Prefabs that are required to be instantiated")]
35  [SerializeField] GameObject[] Prefabs;
36 
40  [Header("Objects that are global")]
41  [SerializeField] Object[] Objects;
42 
43  // Init function
48  public void Init()
49  {
50  // This ScriptableObject should be destroy/unloaded by a scene change
51  hideFlags = HideFlags.DontUnloadUnusedAsset;
52 
53  if(DebugSystemPrefab != null && UseDebugSystem)
54  {
55  GameObject debugSystemGO = GameObject.Instantiate<GameObject>(DebugSystemPrefab);
56  DontDestroyOnLoad(debugSystemGO);
57  DebugSystem debugSystem = debugSystemGO.GetComponent<DebugSystem>();
58  if (debugSystem == null)
59  Debug.LogError("Initalizer: the DebugSystemPrefab doesn't have a DebugSystem component on it!");
60  }
61  else
62  Debug.LogError("Initalizer: DebugSystemPrefab is null!");
63 
64  if (ScriptableObjects != null)
65  {
66  for (int i = 0; i < ScriptableObjects.Length; i++)
67  {
68  if(ScriptableObjects[i] != null)
69  {
70  IInit initSO = ScriptableObjects[i] as IInit;
71  if (initSO != null)
72  initSO.Init();
73  else
74  Debug.LogError("Initalizer: Element " + ScriptableObjects[i].name + " doesn't has a IInit implementation!");
75  }
76  else
77  Debug.LogWarning("Initalizer: Element " + i.ToString() + " is null!");
78  }
79  }
80 
81  // Instantiate all required prefabs
82  if(Prefabs != null)
83  {
84  for (int i = 0; i < Prefabs.Length; i++)
85  Instantiate(Prefabs[i]);
86  }
87  }
88  }
89 }
This interface allows the initialization of anything by the Initialzer.
Definition: IInit.cs:8
void Init()
This function will be called at the start of the game. It will initialize special system like setting...
Definition: Initializer.cs:48
ScriptableObject [] ScriptableObjects
All ScriptableObjects in this array that have an IInit interface implemented will be initialized...
Definition: Initializer.cs:29
This Initializer controlls the initialization of anything that implements the IInit interface...
Definition: Initializer.cs:11
Object [] Objects
This gloable objects will allways have a refernce during runtime and won&#39;t be destroyd by the garbage...
Definition: Initializer.cs:41
The DebugSystem controlls the pannel which includes the DebuConsole, SystemInfo, MemoryInfo and FPSIn...
Definition: DebugSystem.cs:12
GameObject [] Prefabs
All Prefabs in this array will be spawned at the start of the game.
Definition: Initializer.cs:35