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

public class ScenesManager : BaseManager<ScenesManager>
{
    private ScenesManager() { }
    /// <summary>
    /// 同步加载场景
    /// </summary>
    /// <param name="sceneName">场景名称</param>
    /// <param name="action">场景加载后需要做的</param>
    public void LoadScene(string sceneName, UnityAction action = null, LoadSceneMode loadSceneMode = LoadSceneMode.Single)
    {
        SceneManager.LoadScene(sceneName,loadSceneMode);
        action?.Invoke();
    }

    /// <summary>
    /// 异步加载场景
    /// </summary>
    /// <param name="sceneName">场景名称</param>
    /// <param name="action">委托</param>
    /// <param name="loadSceneMode">加载场景方式</param>
    public void LoadSceneAsyn(string sceneName, UnityAction action = null,LoadSceneMode loadSceneMode = LoadSceneMode.Single)
    {
        MonoMgr.Instance.StartCoroutine(ReallyLoadScene(sceneName, action));
    }

    /// <summary>
    /// 协程加载场景
    /// </summary>
    /// <param name="sceneName">场景名称</param>
    /// <param name="action">委托</param>
    /// <param name="loadSceneMode">加载场景方式</param>
    /// <returns></returns>
    private IEnumerator ReallyLoadScene(string sceneName, UnityAction action = null, LoadSceneMode loadSceneMode = LoadSceneMode.Single)
    {
        AsyncOperation ao = SceneManager.LoadSceneAsync(sceneName, loadSceneMode);
        do
        {
            //分发事件
            yield return ao.progress;
        } 
        while (!ao.isDone);
        action?.Invoke();
    }
}
