项目中的资源管理器代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 资源管理器
namespace C_Framework
{
public class C_ResMgr : C_MonoSingleton<C_ResMgr>
{
// 正在加载的资源映射
private List<C_IResLoader> m_LoadingResLoaderList = new List<C_IResLoader>();
// 已经加载的资源映射
private List<C_IResLoader> m_LoadedResLoaderList = new List<C_IResLoader>();
// AssetBundle
private List<C_IResLoader> m_AssetBundleLoaderList = new List<C_IResLoader>();
void Update()
{
// 不能用foreach remove后会报错
// 遍历正在加载的资源,检测是否已经加载完成
for (int i = m_LoadingResLoaderList.Count - 1; i >= 0; i--)
{
C_IResLoader loader = m_LoadingResLoaderList[i];
if (loader.IsDone())
{
m_LoadedResLoaderList.Add(loader);
loader.LoadDoneCallback();
m_LoadingResLoaderList.RemoveAt(i);
}
}
}
/// <summary>
/// 判断是否是已经加载完成的资源
/// </summary>
/// <param name="resName">资源名</param>
/// <returns></returns>
public bool IsLoadedRes(string resName)
{
if (string.IsNullOrEmpty(resName))
return false;
resName = StandardResName(resName);
foreach (C_IResLoader loader in m_LoadedResLoaderList)
{
if (loader.ResName == resName)
return true;
}
return false;
}
/// <summary>
/// 判断是否是已经加载完成的资源
/// </summary>
/// <param name="resName">资源名</param>
/// <returns></returns>
public bool IsLoadedAssetBundle(string resName)
{
if (string.IsNullOrEmpty(resName))
return false;
resName = StandardAssetBundleName(resName);
foreach (C_IResLoader loader in m_AssetBundleLoaderList)
{
if (loader.ResName == resName && loader.IsDone())
return true;
}
return false;
}
/// <summary>
/// 加载资源
/// </summary>
/// <param name="resName"></param>
/// <param name="loadResDone"></param>
public void LoadRes(string resName, C_VoidDelegate.WithObject loadResDone = null)
{
if (string.IsNullOrEmpty(resName))
return;
resName = StandardResName(resName);
foreach (C_IResLoader loader in m_LoadedResLoaderList)
{
if (loader.ResName == resName)
{
if (loadResDone != null)
loadResDone(loader.GetResObj());
return;
}
}
foreach (C_IResLoader loader in m_LoadingResLoaderList)
{
if (loader.ResName == resName)
{
if (loadResDone != null)
loader.AddLoadDoneCallback(loadResDone);
return;
}
}
C_ResourceLoader resLoader = new C_ResourceLoader(resName, loadResDone);
m_LoadingResLoaderList.Add(resLoader);
}
/// <summary>
/// 加载资源
/// </summary>
/// <param name="resName"></param>
/// <param name="loadResDone"></param>
public void LoadAssetBundle(string resName, C_VoidDelegate.WithObject loadResDone = null)
{
if (string.IsNullOrEmpty(resName))
return;
resName = StandardAssetBundleName(resName);
foreach (C_IResLoader loader in m_AssetBundleLoaderList)
{
if (loader.ResName == resName)
{
if (loadResDone != null)
{
if (loader.IsDone())
{
loadResDone(loader.GetResObj());
}
else
{
loader.AddLoadDoneCallback(loadResDone);
}
}
return;
}
}
C_AssetBundleLoader resLoader = new C_AssetBundleLoader(resName, loadResDone);
m_AssetBundleLoaderList.Add(resLoader);
}
/// <summary>
/// 释放资源
/// </summary>
public void Unload(string resName)
{
if (string.IsNullOrEmpty(resName))
return;
UnloadRes(resName);
UnloadAssetBundle(resName);
}
public void UnloadRes(string resName)
{
if (string.IsNullOrEmpty(resName))
return;
resName = StandardResName(resName);
foreach (C_IResLoader loader in m_LoadingResLoaderList)
{
if (loader.ResName == resName)
{
loader.Unload();
m_LoadingResLoaderList.Remove(loader);
}
}
foreach (C_IResLoader loader in m_LoadedResLoaderList)
{
if (loader.ResName == resName)
{
loader.Unload();
m_LoadedResLoaderList.Remove(loader);
}
}
}
public void UnloadAssetBundle(string resName)
{
if (string.IsNullOrEmpty(resName))
return;
resName = StandardAssetBundleName(resName);
foreach (C_IResLoader loader in m_AssetBundleLoaderList)
{
if (loader.ResName == resName)
{
loader.Unload();
m_AssetBundleLoaderList.Remove(loader);
}
}
}
public string StandardResName(string resName)
{
if (string.IsNullOrEmpty(resName))
return "";
int end = resName.LastIndexOf('.');
return resName.Substring(0, (end == -1 ? resName.Length : end));
}
public string StandardAssetBundleName(string resName)
{
if (string.IsNullOrEmpty(resName))
return "";
return resName.Substring(resName.LastIndexOf("/") + 1).ToLower();
}
}
}