打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
AssetBundle系列资源的加载、简易的资源管理器
userphoto

2014.11.06

关注

每个需要进行资源管理的类都继承自IAssetManager,该类维护它所使用到的所有资源的一个资源列表。并且每个资源管理类可以重写其资源引用接口和解引用接口。

每个管理器有自己的管理策略,比如SceneManager对场景背景图可以保留最近使用的几张,使用LRU算法维护当前内存中的贴图张数等...

using UnityEngine;using System.Collections;using System.Collections.Generic;// 和资源有关的管理器都将继承自此类public class IAssetManager {    // 管理器所管理的资源列表,实际上是引用列表    protected List<string> lstRefAsset = new List<string>();    // 增加引用的资源    public virtual void RefAsset(string name)    {}    // 以一定的策略卸载资源    public virtual bool UnloadAsset()    { return true; }}

资源管理器类,UnloadUnusedAsset函数保证只有在真正有资源需要卸载的时候才调用Resources.UnloadUnusedAssets();

有一点需要注意的地方就是,在资源解压完成后,一定要记得要释放压缩包内存,即:

www.assetBundle.LoadAsync(GetAssetName(name), type);

www.assetBundle.Unload(false);

因为,www压缩包数据只能手动卸载,跳转场景都不会删除,所以加载完立即unload是一个好习惯。

还有一点需要注意的www.assetBundle.mainAsset是一个同步加载的操作,也就是说调用此函数时会卡。using UnityEngine;using System.Collections;

using UnityEngine;using System.Collections;using System.Collections.Generic;public class ResourceManager{    // 已解压的Asset列表 [prefabPath, asset]    private Dictionary<string, Object> dicAsset = new Dictionary<string, Object>();    // "正在"加载的资源列表 [prefabPath, www]    private Dictionary<string, WWW> dicLoadingReq = new Dictionary<string, WWW>();    public Object GetResource(string name)    {        Object obj = null;        if (dicAsset.TryGetValue(name, out obj) == false)        {            Debug.LogWarning("<GetResource Failed> Res not exist, res.Name = " + name);            if (dicLoadingReq.ContainsKey(name))            {                Debug.LogWarning("<GetResource Failed> The res is still loading");            }        }        return obj;    }    // name表示prefabPath,eg:Prefab/Pet/ABC    public void LoadAsync(string name)    {        LoadAsync(name, typeof(Object));    }    // name表示prefabPath,eg:Prefab/Pet/ABC    public void LoadAsync(string name, System.Type type)    {        // 如果已经下载,则返回        if (dicAsset.ContainsKey(name))            return;        // 如果正在下载,则返回        if (dicLoadingReq.ContainsKey(name))            return;        // 添加引用        RefAsset(name);        // 如果没下载,则开始下载        CoroutineProvider.Instance().StartCoroutine(AsyncLoadCoroutine(name, type));    }    private IEnumerator AsyncLoadCoroutine(string name, System.Type type)    {        string assetBundleName = GlobalSetting.ConvertToAssetBundleName(name);        string url = GlobalSetting.ConverToFtpPath(assetBundleName);        int verNum = GameApp.GetVersionManager().GetVersionNum(assetBundleName);        Debug.Log("WWW AsyncLoad name =" + assetBundleName + " versionNum = " + verNum);        if (Caching.IsVersionCached(url, verNum) == false)            Debug.Log("Version Is not Cached, which will download from net!");        WWW www = WWW.LoadFromCacheOrDownload(url,verNum);        dicLoadingReq.Add(name, www);        while (www.isDone == false)            yield return null;        AssetBundleRequest req = www.assetBundle.LoadAsync(GetAssetName(name), type);        while (req.isDone == false)            yield return null;        dicAsset.Add(name, req.asset);        dicLoadingReq.Remove(name);        www.assetBundle.Unload(false);        www = null;        // Debug.Log("WWW AsyncLoad Finished " + assetBundleName + " versionNum = " + verNum);    }    public bool IsResLoading(string name)    {        return dicLoadingReq.ContainsKey(name);    }    public bool IsResLoaded(string name)    {        return dicAsset.ContainsKey(name);    }    public WWW GetLoadingWWW(string name)    {        WWW www = null;        dicLoadingReq.TryGetValue(name, out www);        return www;    }    // 移除Asset资源的引用,name表示prefabPath    public void UnrefAsset(string name)    {        dicAsset.Remove(name);    }    private string GetAssetName(string ResName)    {        int index = ResName.LastIndexOf('/');        return ResName.Substring(index + 1, ResName.Length - index - 1);    }    public void UnloadUnusedAsset()    {        bool effectNeedUnload = GameApp.GetEffectManager().UnloadAsset();        bool worldNeedUnload = GameApp.GetWorldManager().UnloadAsset();        bool sceneNeedUnload = GameApp.GetSceneManager().UnloadAsset();        if (effectNeedUnload || worldNeedUnload || sceneNeedUnload)        {            Resources.UnloadUnusedAssets();        }    }    // 根据资源路径添加资源引用,每个管理器管理自己的引用    private void RefAsset(string name)    {        // 模型之类的        if (name.Contains(GlobalSetting.CharacterPath))            GameApp.GetWorldManager().RefAsset(name);        // 图片之类的        else if (name.Contains(GlobalSetting.TexturePath))            GameApp.GetUIManager().RefPTexture(name);// 特效之类的        else if (name.Contains(GlobalSetting.EffectPath))            GameApp.GetEffectManager().RefAsset(name);     ......     else            Debug.LogWarning("<Res not ref> name = " + name);    }}

 

资源管理的关键在于以下几点:

(1)资源所对应的几块内存的管理,Unity的内存一直是一个相对比较棘手的方面,所以一定要多做尝试找到规律和方法;

(2)资源加载、卸载策略,什么时候加载什么时候卸载需要根据游戏类型来进行定制;

(3)资源打包策略,也就是以什么单位进行什么类型的资源打包,原则上是让同一资源尽量只需要打包一次,比如多个场景都用到了同一棵树,那么最好是对这棵树单独打包等等。

    比如Prefab1和Prefab2同时引用了Fbx1,将2个prefab单独打包时都会分别包含Fbx1,并且解压到内存时,也会有2份独立的Fbx1,这样会造成内存变大,这点一定要注意。

......

Unity内存和资源这一块虽然显得比较拖泥带水,不过只要使用的够规范,一般还是能够保证内存的干净的。

......

还有一个尚未解决的问题,Unity使用www方式加载资源的时候,不能进行同步加载操作,只能异步,我见到过其他人也遇到过这个问题,很是蛋疼菊紧。

------------------------

 我也遇到了同样的问题,我使用下面这个接口读取本地的bundle资源:

public UnityEngine.Object LoadLocalRes(string szName)
{
UnityEngine.Object obj = null;
obj = Resources.Load(szName);
if (obj == null)
{
// 本地资源没有,去bundle下载资源查找
string szBundlePath = "Assets.Resources." + szName.Replace("/", ".");
if (DLCManager.DownloadList.ContainsKey(szBundlePath) == true)
{
string szMD5File = DLCManager.GetInstance().GetMD5FileName(szBundlePath);
int nVersionNum = DLCManager.DownloadList[szBundlePath];
WWW res = WWW.LoadFromCacheOrDownload(DLCManager.GetInstance().LocalVersionPathRead + szMD5File, nVersionNum);

if (string.IsNullOrEmpty(res.error))
{
obj = res.assetBundle.mainAsset;
res.assetBundle.Unload(false);
}
}
}

return obj;
}
这里没用协程处理WWW.LoadFromCacheOrDownload导致大量调用此接口阻塞,但是改成协程处理又没办法立即返回obj给调用者。楼主最近有没有找到解决办法?
  
#2楼 2014-05-31 16:12 刺客刺  
CoroutineProvider 是什么啊,U3D没这个东西吧
  
#3楼[楼主] 2014-06-03 10:16 斯芬克斯  
@Code Knight
obj = res.assetBundle.mainAsset;这行代码是阻塞式的,会卡主。
对于同步www加载,还没有太好的办法。
  
#4楼[楼主] 2014-06-03 10:19 斯芬克斯  
@刺客刺
这个是我自己写的一个全局提供Coroutine服务的类型,就是用来调用Monobehaviour的Coroutine操作的。
代码如下所示:
// 专门用来开启Coroutine的对象
public class CoroutineProvider : MonoBehaviour
{
private static CoroutineProvider instance = null;
public static CoroutineProvider Instance()
{
if(instance == null)
{
instance = GameObject.Find("GameStarterprefabs").AddComponent("CoroutineProvider") as CoroutineProvider;
}
return instance;
}

}
  
#5楼 2014-10-13 16:25 suntabu  
@Code Knight
unity3d 4.5.2开始可以同步加载AssetBundle
AssetBundle.CreatFromMemoryImmidete
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
【新提醒】Unity AssetBundle值得注意的问题
Unity资源管理汇总
【厚积薄发】揭开AssetBundle庐山真面目(一)
unity 加载资源
简单总结AssetBundle的打包/解包
玩转Unity资源,对象和序列化
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服