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方式加載資源的時候,不能進行同步加載操作,只能異步,我見到過其他人也遇到過這個問題,很是蛋疼菊緊。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM