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 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; } } }