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

public class Loading : MonoBehaviour {

    [SerializeField]
    private string sceneToLoad;

    [SerializeField]
    private Image progressImage;

    [SerializeField]
    private float timeToLoad = 3f; // Cambiado a 3 segundos

    void Start () {
        StartCoroutine(LoadScene());
    }

    IEnumerator LoadScene()
    {
        AsyncOperation loading;
        loading = SceneManager.LoadSceneAsync(sceneToLoad, LoadSceneMode.Single);
        loading.allowSceneActivation = false;

        float timer = 0f;

        while (timer < timeToLoad) {
            timer += Time.deltaTime;
            float progress = Mathf.Clamp01(timer / timeToLoad);
            progressImage.fillAmount = progress;
            yield return null;
        }

        progressImage.fillAmount = 1;
        loading.allowSceneActivation = true;
    }
}