using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
/// <summary>
/// 资源加载模块
/// 1.异步加载
/// 2.委托和 lambda表达式
/// 3.协程
/// 4.泛型
/// </summary>
public class ResManager : BaseManager<ResManager>
{
//同步加载资源
public T Load<T>(string name) where T:Object
{
T res = Resources.Load<T>(name);
//如果对象是一个GameObject类型的 我把他实例化后 再返回出去 外部 直接使用即可
if (res is GameObject)
return GameObject.Instantiate(res);
else//TextAsset AudioClip
return res;
}
//异步加载资源
public void LoadAsync<T>(string name, UnityAction<T> callback) where T:Object
{
//开启异步加载的协程
//使用公共Mono模块来开启协程
MonoManager.GetInstance().StartCoroutine(ReallyLoadAsync(name, callback));
}
//真正的协同程序函数 用于 开启异步加载对应的资源
private IEnumerator ReallyLoadAsync<T>(string name, UnityAction<T> callback) where T : Object
{
ResourceRequest r = Resources.LoadAsync<T>(name);
yield return r;
if (r.asset is GameObject)
callback(GameObject.Instantiate(r.asset) as T);
else
callback(r.asset as T);
}
}
Resources资源加载模块(提供异步和同步两种方式)
最新推荐文章于 2024-02-20 16:58:18 发布