赞
踩
资源加载和引用计数
public class ResMgr : MonoSingleton<ResMgr>
{
//全局资源池
public List<Res> SharedLoadedReses = new List<Res>();
}
public class ResLoader
{
public T LoadSync<T>(string assetName) where T : Object
{
var res = GetOrCreateRes(assetName);
if (res != null)
{
return res.Asset as T;
}
// 真正加载资源
res = CreateRes(assetName);
res.LoadSync();
return res.Asset as T;
}
public void LoadAsync<T>(string assetName, Action<T> onLoaded) where T : Object
{
// 查询当前的 资源记录
var res = GetOrCreateRes(assetName);
if (res != null)
{
onLoaded(res.Asset as T);
return;
}
// 真正加载资源
res = CreateRes(assetName);
res.LoadAsync(loadedRes =>
{
onLoaded(loadedRes.Asset as T);
});
}
public void ReleaseAll()
{
mResRecord.ForEach(loadedAsset => loadedAsset.Release());
mResRecord.Clear();
}
#region private
private List<Res> mResRecord = new List<Res>();
private Res GetOrCreateRes(string assetName)
{
// 查询当前的 资源记录
var res = GetResFromRecord(assetName);
if (res != null)
{
return res;
}
// 查询全局资源池
res = GetFromResMgr(assetName);
if (res != null)
{
AddRes2Record(res);
return res;
}
return res;
}
private Res CreateRes(string assetName)
{
Res res = null;
if (assetName.StartsWith("resources://"))
{
res = new ResourcesRes(assetName);
}
else
{
res = new AssetBundleRes(assetName);
}
ResMgr.Instance.SharedLoadedReses.Add(res);
AddRes2Record(res);
return res;
}
private Res GetResFromRecord(string assetName)
{
return mResRecord.Find(loadedAsset => loadedAsset.Name == assetName);
}
private Res GetFromResMgr(string assetName)
{
return ResMgr.Instance.SharedLoadedReses.Find(loadedAsset => loadedAsset.Name == assetName);
}
private void AddRes2Record(Res resFromResMgr)
{
mResRecord.Add(resFromResMgr);
resFromResMgr.Retain();
}
#endregion
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。