public class ResMgr : Singleton<ResMgr>
{
//存储预制体列表
private Hashtable m_Cache;
public ResMgr()
{
m_Cache = new Hashtable();
}
//加载游戏预制体资源
public GameObject Load(string path, ResType resType, bool isCache = false)
{
GameObject obj = null;
if (m_Cache.Contains(path))
{
obj = m_Cache[path] as GameObject;
}
else
{
StringBuilder str = new StringBuilder();
switch (resType)
{
case ResType.Effect:
str.Append("Prefab/Effect/");
break;
case ResType.Role:
str.Append("Prefab/Role/");
break;
case ResType.SceneUI:
str.Append("Prefab/SceneUI/");
break;
case ResType.WinUI:
str.Append("Prefab/WinUI/");
break;
case ResType.UIOther:
str.Append("Prefab/UIOther/");
break;
default:
break;
}
str.Append(path);
obj = Resources.Load(str.ToString())as GameObject;
if (isCache)
{
m_Cache.Add(path, obj);
}
}
return GameObject.Instantiate(obj);
}
public override void Dispose()
{
m_Cache.Clear();
Resources.UnloadUnusedAssets();
}
//加载xml文本
private string XMLPath = "Config";
public TextAsset LoadConfigXML(string name)
{
return LoadXMLAsset(name, XMLPath);
}
public TextAsset LoadXMLAsset(string name, string path)
{
TextAsset textAsset = null;
string loadPath = path + "/" + name;
textAsset = Resources.Load(loadPath, typeof(TextAsset)) as TextAsset;
if (textAsset == null)
{
Debug.LogError(loadPath + " is null");
return null;
}
return textAsset;
}
//加载图片
public Texture2D LoadImage(string name)
{
return LoadTexture(name, "UI/Icon/");
}
public Texture2D LoadTexture(string name, string path)
{
string loadPath = path + "/" + name;
Texture2D tex = Resources.Load(loadPath, typeof(Texture2D)) as Texture2D;
if (tex == null)
{
Debug.LogError("Texture2D " + name + " is not found in Resources");
}
return tex;
}
}
加载资源类
最新推荐文章于 2020-09-04 11:39:05 发布