您的位置:首页 > 移动开发 > Unity3D

unity的assetbundle加载

2017-12-04 17:42 726 查看
最近在写assetbundle的编辑器,顺便写了一下unity的资源管理器。

研究了一下王者农药的热更新机制,发现它的首页动画界面也可以热更新,但是这个时候才在启动进度条界面。

猜想了1下,他的资源加载路径是变化的。

优先加载缓存,发现缓存没得就加载包。

下次重新进入游戏,因为新数据已经down下来了,这个时候是直接加载缓存,就会发现初始动画改编了。

这里就需要动态设置加载路径。

说跑题了, 现先上传代码

/************************************************************************************* 

**文 件 名:ResourceManager 

**创建时间:2017/9/22 星期五 下午 1:55:46 

**作    者:worthgod(qq: 243515320)

**工    号:

**说    明: 

**版    本:V1.0.0  

**修改时间: 

**修 改 人: 

 *************************************************************************************/

using System.Collections;

using System.Collections.Generic;

using System.IO;

using UnityEngine;

using WorthGodFramework;

using WorthGodFramework.Config;

using WorthGodFramework.Core;

using WorthGodFramework.Log;

namespace WorthGod

{

    public class ResourceManager : SingletonMono<ResourceManager>

    {

        public Dictionary<string, AssetBundle> ObjectPool = new Dictionary<string, AssetBundle>();

        void Awake()

        {

            DontDestroyOnLoad(this);

        }

        private string _baseDownloadingURL = "";

        public string BaseDownloadingURL

        {

            get

            {

                if (string.IsNullOrEmpty(_baseDownloadingURL))

                {

#if !UNITY_EDITOR

                    _baseDownloadingURL = Util.GetRelativePath()+ "WorthGodFramework/";

#else

                    _baseDownloadingURL = Util.DataPath + "WorthGodFramework/";

#endif

                }

                return _baseDownloadingURL;

            }

            set

            {

                _baseDownloadingURL = value;

            }

        }

        // The base downloading url which is used to generate the full downloading url with the assetBundle names.

        public void LoadAssetAsync<T>(string abname, string assetname, WorthGodFrameworkAction<T> func) where T:UnityEngine.Object

        {

            

            if (ObjectPool.ContainsKey(abname))

            {

                var a = ObjectPool[abname].AllAssetNames();

                var m_Request = ObjectPool[abname].LoadAssetAsync(assetname, typeof(T));

                if (func != null)

                {

                    func(m_Request.asset as T);

                }

            }

            else {

                StartCoroutine(LoadingAb(abname, assetname, func));

            }

        }

        IEnumerator LoadingAb<T>(string abname, string assetname, WorthGodFrameworkAction<T> func) where T : UnityEngine.Object

        {

            var filePath = BaseDownloadingURL + abname;

            if (!File.Exists(filePath))

            {

                filePath = Util.DataPath + "WorthGodFramework/" + abname;

            }

            var download = new WWW(filePath);

    

            yield return download;

            if (download.error != null)

            {

                //todo 弹出下载失败的提示 -->  输出

                _Log.Error("下载初始动画界面,路径--》  " + BaseDownloadingURL + abname);

                yield return null;

            }

            else

            {

                if (download.isDone)

                {

                    var EncryptByte = download.bytes;

                    if (EncryptByte != null)

                    {

                        ////反解密

                        var BundleBytes = EncryptionHelper.getInstance().Decrypt(EncryptByte, WorthGodFrameworkConfig.EncryptionKey);

                        AssetBundle bundle = AssetBundle.LoadFromMemory(BundleBytes);

                        ObjectPool.Add(abname, bundle);

                        var m_Request = ObjectPool[abname].LoadAssetAsync(assetname, typeof(T));

                        if (func != null)

                        {

                            func(m_Request.asset as T);

                        }

                    }

                }

            }

            download.Dispose();

            download = null;

            yield break;

        }

        public T LoadAsset<T>(string abname, string assetname) where T : UnityEngine.Object

        {

            if (ObjectPool.ContainsKey(abname))

            {

                var m_Request = ObjectPool[abname].LoadAssetAsync(assetname, typeof(T));

                return m_Request.asset as T;

            }

            else

            {

                var filePath = BaseDownloadingURL + abname;

                if (!File.Exists(filePath))

                {

                    filePath = Util.DataPath + "WorthGodFramework/" + abname;

                }

                Debug.LogWarning("$#@$#@$#@      " + filePath);

                if (!File.Exists(filePath)) return null as T;

                var EncryptByte = File.ReadAllBytes(filePath);

                ////反解密

                var BundleBytes = EncryptionHelper.getInstance().Decrypt(EncryptByte, WorthGodFrameworkConfig.EncryptionKey);

                var bundle = AssetBundle.LoadFromMemory(BundleBytes);

                ObjectPool.Add(abname, bundle);

                var m_Request = bundle.LoadAsset(assetname, typeof(T));

                return m_Request as T;

            }

        }

        public void UnloadAb(string abname) {

            if (ObjectPool.ContainsKey(abname))

            {

                ObjectPool[abname].Unload(false);

                ObjectPool.Remove(abname);

            }

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息