using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class LoadLevel : MonoBehaviour
{
    public GameObject loadingScrenn;
    public Slider slider;
    public void LoadingLevel(int sceneIndex)
    {
        //Inicia a rotina onde pegará o progresso de loading.
        StartCoroutine(LoadAsynchronosly(sceneIndex));
    }

    IEnumerator LoadAsynchronosly (int sceneIndex){
        //Cria uma variável que receberá o processo de loading.
        AsyncOperation operation = SceneManager.LoadSceneAsync(sceneIndex);

        loadingScrenn.SetActive(true);



        //Enquanto operação não encerradar, escrever etapa de loading.
        while (!operation.isDone)
        {
            float progress = Mathf.Clamp01(operation.progress / .9f);
            
            slider.value = progress;

            //Debug.Log(operation.progress);

            yield return null;
        }
    }

}
