TestCodeStructure/Assets/Scripts/Project/ProjectBase/ResourcesManager.cs

56 lines
1.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public static class ResourcesManager
{
/// <summary>
/// 同步加载
/// </summary>
/// <param name="resName"></param>
public static 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 static void LoadAsync<T>( MonoBehaviour target,string resName, UnityAction<T> action) where T : Object
{
target.StartCoroutine(RealLoadAsync<T>(resName, action));
}
/// <summary>
/// 真正的异步加载过程
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="resName"></param>
/// <param name="action"></param>
/// <returns></returns>
private static 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);
}
}
}