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

public class ResourcesManager : BaseManager<ResourcesManager>
{
    private ResourcesManager() { }

    /// <summary>
    /// 同步加载
    /// </summary>
    /// <param name="resName"></param>
    public T Load<T>(string resName) where T : Object
    {
        T res = Resources.Load<T>(resName);
        if (res is GameObject)
        {
            return GameObject.Instantiate(res);
        }
        else
            return res;
    }

    /// <summary>
    /// 异步加载
    /// </summary>
    /// <param name="resName"></param>
    public void LoadAsync<T>(string resName, UnityAction<T> action) where T : Object
    {
        MonoMgr.Instance.StartCoroutine(RealLoadAsync<T>(resName, action));
    }

    /// <summary>
    /// 真正的异步加载过程
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="resName"></param>
    /// <param name="action"></param>
    /// <returns></returns>
    private IEnumerator RealLoadAsync<T>(string resName,UnityAction<T> action) where T : Object
    {
        ResourceRequest request = Resources.LoadAsync<T>(resName);
        yield return request;
        if (request.asset is GameObject)
        {
            action(GameObject.Instantiate(request.asset) as T);
        }
        else
        {
            action(request.asset as T);
        }


    }
}
