summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Core/Loader.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Assets/Scripts/Core/Loader.cs')
-rw-r--r--Assets/Scripts/Core/Loader.cs73
1 files changed, 73 insertions, 0 deletions
diff --git a/Assets/Scripts/Core/Loader.cs b/Assets/Scripts/Core/Loader.cs
new file mode 100644
index 0000000..23cf216
--- /dev/null
+++ b/Assets/Scripts/Core/Loader.cs
@@ -0,0 +1,73 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using UnityEngine.Events;
+
+namespace Core
+{
+ public class Loader : MonoBehaviour
+ {
+ public static Loader Current;
+
+ public List<LoadGroup> loadGroups;
+ public UnityEvent onLoaded;
+
+ private bool _started;
+
+
+#if UNITY_EDITOR
+ [HideInInspector] public int objectCount;
+ [HideInInspector] public int loadedObjectCount;
+
+ private void OnGUI()
+ {
+ GUILayout.Label($"Loaded: {loadedObjectCount / objectCount * 100}% ({loadedObjectCount}/{objectCount})");
+ }
+#endif
+
+ private void Update()
+ {
+ if (!_started)
+ _started = true;
+ }
+
+ private void Awake()
+ {
+#if UNITY_EDITOR
+ foreach (var loadGroup in loadGroups)
+ objectCount += loadGroup.GameObjects.Count;
+#endif
+ if (Current == null)
+ Current = this;
+
+ StartCoroutine(Load());
+ }
+
+ private IEnumerator Load()
+ {
+ yield return new WaitUntil(() => _started);
+ foreach (var loadGroup in loadGroups)
+ {
+ if (loadGroups == null)
+ continue;
+ foreach (var loadGroupGameObject in loadGroup.GameObjects)
+ {
+ Instantiate(loadGroupGameObject);
+#if UNITY_EDITOR
+ loadedObjectCount++;
+#endif
+ yield return null;
+ }
+ }
+ onLoaded.Invoke();
+ Destroy(gameObject);
+ }
+
+ private void OnDestroy()
+ {
+ if (Current == this)
+ Current = null;
+ }
+ }
+}