using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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();
}
}
}
///
/// the view-model associated with the loading screen view.
///
public class LoadingScreen : SingletonBehaviour
{
public event System.Action ScreenActivated;
public event System.Action ScreenDeactivated;
[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);
ScreenActivated?.Invoke();
}
private IEnumerator DisableLoadingScreen()
{
yield return blueScreen.FadeTo(0, fadeOutDuration);
ScreenDeactivated?.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());
}
}