using System.Collections; using System.Collections.Generic; using UnityEngine; using GatherAndDefend.Events; #if UNITY_EDITOR using UnityEditor; [CustomEditor(typeof(LoadingScreen))] public class LoadingScreenEditor : Editor { public override void OnInspectorGUI() { DrawDefaultInspector(); if (!Application.isPlaying) return; if (GUILayout.Button("start")) { (target as LoadingScreen).ShowLoadingScreen(); } else if (GUILayout.Button("stop")) { (target as LoadingScreen).HideLoadingScreen(); } } } #endif /// /// the view-model associated with the loading screen view. /// public class LoadingScreen : SingletonBehaviour { [Header("Screen")] public CanvasGroup blueScreen; public float fadeInDuration = 1; public float fadeOutDuration = 1; public GameObject cloudPrefab; public int maxClouds = 10; public CloudAnimation.Properties cloudProperties; private List clouds = new List(); private Coroutine screenCoroutine, cloudCoroutine; private IEnumerator SpawnClouds() { while (true) { var randPos = Extensions.RandomInRectangle(Screen.width + 100, Screen.height - 100); var cloudInstance = Instantiate(cloudPrefab, randPos, Quaternion.identity); cloudInstance.transform.SetParent(transform); var cloudComponent = cloudInstance.GetComponent(); cloudComponent.properties = cloudProperties; clouds.Add(cloudComponent); yield return new WaitForSeconds(fadeInDuration); } } private IEnumerator EnableLoadingScreen() { yield return blueScreen.FadeTo(1, fadeInDuration); EventAggregator.Instance.GetEvent().Invoke(); } private IEnumerator DisableLoadingScreen() { yield return blueScreen.FadeTo(0, fadeOutDuration); EventAggregator.Instance.GetEvent().Invoke(); } public void ShowLoadingScreen() { screenCoroutine = StartCoroutine(EnableLoadingScreen()); cloudCoroutine = StartCoroutine(SpawnClouds()); } public void HideLoadingScreen() { StopCoroutine(screenCoroutine); StopCoroutine(cloudCoroutine); clouds.FindAll(x => x).ForEach(x => x.Kill()); StartCoroutine(DisableLoadingScreen()); } }