AssetBundle系列——资源的加载、简易的资源管理器

每个需要进行资源管理的类都继承自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方式加载资源的时候,不能进行同步加载操作,只能异步,我见到过其他人也遇到过这个问题,很是蛋疼菊紧。

 

posted @ 2014-02-21 17:41  斯芬克斯  阅读(23909)  评论(19编辑  收藏  举报