using UnityEngine;
using UnityEngine.UI;
using System.Collections;
///
/// User interface loading progress.
///
using UnityEngine.Events;
namespace WorldStreamer2
{
public class UILoadingStreamer : MonoBehaviour
{
[Tooltip("List of streamers objects that should affect loading screen. Drag and drop here all your streamer objects from scene hierarchy which should be used in loading screen.")]
///
/// The streamers.
///
public Streamer[] streamers;
///
/// The progress image.
///
public Image progressImg;
[Tooltip("Time in seconds that you give your loading screen to get data from whole streamers about scene that they must load before loading screen will be switched off.")]
///
/// The wait time after end of loading.
///
public float waitTime = 2;
public UnityEvent onDone;
///
/// Awake this instance, and set fill to 0.
///
void Awake()
{
foreach (var item in streamers)
{
item.loadingStreamer = this;
item.showLoadingScreen = true;
}
progressImg.fillAmount = 0;
}
///
/// Update this instance, and sets current progress of streaming.
///
void Update()
{
if (streamers.Length > 0)
{
bool initialized = true;
progressImg.fillAmount = 0;
foreach (var item in streamers)
{
progressImg.fillAmount += item.LoadingProgress / (float)streamers.Length;
initialized = initialized && item.initialized;
}
if (initialized)
{
if (progressImg.fillAmount >= 1)
{
if (onDone != null)
onDone.Invoke();
StartCoroutine(TurnOff());
}
}
}
else
Debug.Log("No streamer Attached");
}
public IEnumerator TurnOff()
{
yield return new WaitForSeconds(waitTime);
gameObject.SetActive(false);
}
///
/// Show progress bar and resets fill.
///
public void Show()
{
progressImg.fillAmount = 0;
gameObject.SetActive(true);
}
}
}