动态资源加载工具类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ResoucesManager
{
private static Dictionary<string, GameObject> resDic = new Dictionary<string, GameObject>();
/// <summary>
/// 用于加载resources中文件夹中的资源
/// </summary>
/// <param name="path">预制体在resources文件夹中的路径</param>
/// <returns>加载出来的预制体</returns>
public static GameObject Load(string path)
{
//1.判断容器中是否存在该路径
if (resDic.ContainsKey(path))
{
return resDic[path];
}
//2.如果存在,直接从容器中获取
//3.如果没有,使用load方法读取,并且存入容器
GameObject go = Resources.Load(path) as GameObject;
resDic[path] = go;
return go;
}
//非泛型容器
//Direction<object,object>
private static Hashtable resTable = new Hashtable();
//泛型函数
public static T Load<T> (string path) where T : Object
{
//1.判断容器中是否存在该路径
if (resTable.ContainsKey(path))
{
return resTable[path] as T;
}
//2.如果存在,直接从容器中获取
//3.如果没有,使用load方法读取,并且存入容器
T t = Resources.Load<T>(path);
resTable[path] = t;
return t;
}
}