【Unity】簡易的資源管理器

http://www.cnblogs.com/sifenkesi/p/3559924.htmlhtml

每一個須要進行資源管理的類都繼承自IAssetManager,該類維護它所使用到的全部資源的一個資源列表。而且每一個資源管理類能夠重寫其資源引用接口和解引用接口。算法

每一個管理器有本身的管理策略,好比SceneManager對場景背景圖能夠保留最近使用的幾張,使用LRU算法維護當前內存中的貼圖張數等...c#

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();異步

有一點須要注意的地方就是,在資源解壓完成後,必定要記得要釋放壓縮包內存,即:ide

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

www.assetBundle.Unload(false);url

由於,www壓縮包數據只能手動卸載,跳轉場景都不會刪除,因此加載完當即unload是一個好習慣。spa

還有一點須要注意的www.assetBundle.mainAsset是一個同步加載的操做,也就是說調用此函數時會卡。htm

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);
    }
}

 

資源管理的關鍵在於如下幾點:blog

(1)資源所對應的幾塊內存的管理,Unity的內存一直是一個相對比較棘手的方面,因此必定要多作嘗試找到規律和方法;

(2)資源加載、卸載策略,何時加載何時卸載須要根據遊戲類型來進行定製;

(3)資源打包策略,也就是以什麼單位進行什麼類型的資源打包,原則上是讓同一資源儘可能只須要打包一次,好比多個場景都用到了同一棵樹,那麼最好是對這棵樹單獨打包等等。

    好比Prefab1和Prefab2同時引用了Fbx1,將2個prefab單獨打包時都會分別包含Fbx1,而且解壓到內存時,也會有2份獨立的Fbx1,這樣會形成內存變大,這點必定要注意。

......

Unity內存和資源這一塊雖然顯得比較拖泥帶水,不過只要使用的夠規範,通常仍是可以保證內存的乾淨的。

......

還有一個還沒有解決的問題,Unity使用www方式加載資源的時候,不能進行同步加載操做,只能異步,我見到過其餘人也遇到過這個問題,非常蛋疼菊緊。

相關文章
相關標籤/搜索