2025-04-23 21:09:19 +08:00

53 lines
1.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
/// <summary>
/// 资源加载模块
/// 1异步加载2委托和lambda表达式3协程4泛型
/// </summary>
public class ResMgr : BaseManager<ResMgr>
{
//同步加载资源
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
{
//开启异步加载的协程
MonoMgr.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);
}
}
}