예제 #1
0
        /// <summary>
        /// 从cache中加载包
        /// </summary>
        /// <param name="strABName"></param>
        /// <returns></returns>
        private IEnumerator _LoadAssetBundleAsycFromCache(string strABName)
        {
            //检查manifest
            if (m_assetBundleManifest == null ||
                AssetBundleInfoManager.IsExits() ||
                AssetBundleInfoManager.GetSingel().GetBundleInfo(itfDownloadPath) == null ||
                AssetBundleInfoManager.GetSingel().GetBundleInfo(itfDownloadPath).IsCached(strABName) == false)
            {
                Debug.LogError("LoadAssetBundleFromCache error!");
                yield break;
            }

            //加载包
            string path = itfDownloadPath + strABName;
            AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);

            yield return(request);

            if (request.isDone &&
                request.assetBundle != null)
            {
                //包加载成功
                tagLoadedAssetBundle loadedBundle = new tagLoadedAssetBundle();
                loadedBundle.Init(request.assetBundle);
                m_dictLoadedAssetBundles.Add(strABName, loadedBundle);

                Debug.Log("load " + strABName + " bundle from cache succeed!");
            }
            else
            {
                //包加载失败
                Debug.Log("load assetBundle:" + strABName + "from cache is invalid!");
            }
        }
예제 #2
0
        /// <summary>
        /// 从cache中加载包
        /// </summary>
        /// <param name="strABName"></param>
        /// <returns></returns>
        private void _LoadAssetBundleFromCache(string strABName)
        {
            //检查manifest
            if (m_assetBundleManifest == null
                || AssetBundleInfoManager.IsExits() == false
                || AssetBundleInfoManager.GetSingel().GetBundleInfo(AppConst.BundleInfoPath) == null
                || AssetBundleInfoManager.GetSingel().GetBundleInfo(AppConst.BundleInfoPath).IsCached(strABName) == false)
            {
                Debug.LogError("LoadAssetBundleFromCache error!");
                return;
            }

            //加载包
            string path = itfDownloadPath + strABName;
            AssetBundle assetBundle = AssetBundle.LoadFromFile(path);

            if (assetBundle != null)
            {
                //包加载成功
                tagLoadedAssetBundle loadedAB = null;
                if (m_dictLoadedAssetBundles.TryGetValue(strABName, out loadedAB))
                {
                    loadedAB.assetBundle = assetBundle;
                    loadedAB.nReferencedCount = 1;
                }
                else
                    Debug.LogError("LoadAssetBundleFromCache error, tagLoadedAssetBundle is null!");

                Debug.Log("load " + strABName + " bundle from cache succeed!");
            }
            else
                //包加载失败
                Debug.Log("load assetBundle:" + strABName + " from cache fail!");
        }
예제 #3
0
        /// <summary>
        /// 加载AssetBundle,先查找cache里面,没有就查找streamingAssetPath
        /// </summary>
        /// <param name="strABName"></param>
        /// <returns></returns>
        private void _LoadAssetBundle(string strABName)
        {
            // 检察是否已经加载
            tagLoadedAssetBundle loadedAB = null;
            m_dictLoadedAssetBundles.TryGetValue(strABName, out loadedAB);
            if (loadedAB != null)
            {
                // 所有资源都要先加载依赖包
                _LoadDependencies(strABName);

                //已经加载后又再次加载,引用+1
                loadedAB.nReferencedCount++;
                Debug.Log("load " + strABName + " success, and already downloaded just add reference count!");
            }
            else
            {
                // 提前生成,后面再填充数据
                loadedAB = new tagLoadedAssetBundle();
                m_dictLoadedAssetBundles.Add(strABName, loadedAB);

                // 所有资源都要先加载依赖包
                _LoadDependencies(strABName);

                if (AssetBundleInfoManager.IsExits() == true
                && AssetBundleInfoManager.GetSingel().GetBundleInfo(AppConst.BundleInfoPath) != null
                && AssetBundleInfoManager.GetSingel().GetBundleInfo(AppConst.BundleInfoPath).IsCached(strABName) == true)
                {
                    _LoadAssetBundleFromCache(strABName);
                }
                else
                {
                    _LoadAssetBundleFromStreaming(strABName);
                }
            }
        }
예제 #4
0
        /// <summary>
        /// 加载AssetBundle,先查找cache里面,没有就查找streamingAssetPath
        /// </summary>
        /// <param name="strABName"></param>
        /// <returns></returns>
        private IEnumerator _LoadAssetBundleAsyc(string strABName)
        {
            //if (m_gameobject == null)
            //{
            //    Debug.LogError("GameMainObj not exist!");
            //    yield break;
            //}

            // 检察是否已经加载
            tagLoadedAssetBundle bundle = null;
            m_dictLoadedAssetBundles.TryGetValue(strABName, out bundle);
            if (bundle != null)
            {
                //已经加载后又再次加载,引用+1
                bundle.nReferencedCount++;
                Debug.Log("download " + strABName + " success, and already downloaded just add reference count!");

                yield break;
            }

            if (AssetBundleInfoManager.IsExits()
                && AssetBundleInfoManager.GetSingel().GetBundleInfo(AppConst.BundleInfoPath) != null
                && AssetBundleInfoManager.GetSingel().GetBundleInfo(AppConst.BundleInfoPath).IsCached(strABName) == true)
            {
                Coroutine routine = StartCoroutine(_LoadAssetBundleAsycFromCache(strABName));
                m_listCoroutine.Add(routine);
                yield return routine;
            }
            else
            {
                Coroutine routine = StartCoroutine(_LoadAssetBundleAsycFromStreaming(strABName));
                m_listCoroutine.Add(routine);
                yield return routine;
            }
        }
예제 #5
0
        /// <summary>
        /// 加载资源逻辑
        /// </summary>
        /// <param name="strPath"></param>
        /// <param name="assetName"></param>
        /// <param name="specialAssetName"></param>
        /// <returns></returns>
        private UnityEngine.Object _LoadSingleAssetInternal(string strPath, string assetName, string specialAssetName = null)
        {
            // strPath不判断长度,允许为长度为0
            if (strPath != null &&
                assetName != null &&
                assetName.Length != 0)
            {
                // 转为小写,包名只能是小写
                string strABName = strPath.ToLower() + assetName.ToLower();

                // 某些包的assetname比较特殊,比如说Manifest文件
                string realAseetName = null;
                if (specialAssetName == null ||
                    specialAssetName.Length == 0)
                {
                    realAseetName = assetName;
                }
                else
                {
                    realAseetName = specialAssetName;
                }

                AssetBundle assetBundle = null;
                if (AssetBundleInfoManager.IsExits() == true &&
                    AssetBundleInfoManager.GetSingel().GetBundleInfo(itfDownloadPath) != null &&
                    AssetBundleInfoManager.GetSingel().GetBundleInfo(itfDownloadPath).IsCached(strABName) == true)
                {
                    // 优先从cache加载
                    string cachePath = itfDownloadPath + strABName;
                    assetBundle = AssetBundle.LoadFromFile(cachePath);
                }
                else
                {
                    // cache没找到就从streaming加载
                    Debug.Log("streamingpath: " + itfStreamingPath + " " + strABName);
                    string streamingPath = itfStreamingPath + strABName;
                    assetBundle = AssetBundle.LoadFromFile(streamingPath);
                }

                if (assetBundle != null)
                {
                    // 最后从包中加载出资源
                    Object ob = assetBundle.LoadAsset(realAseetName);

                    //string[] names = assetBundle.GetAllAssetNames();

                    // 立即卸载
                    assetBundle.Unload(false);

                    return(ob);
                }
            }

            return(null);
        }
예제 #6
0
        /// <summary>
        /// 加载资源逻辑
        /// </summary>
        /// <param name="strPath"></param>
        /// <param name="assetName"></param>
        /// <param name="specialAssetName"></param>
        /// <returns></returns>
        private UnityEngine.Object _LoadSingleAssetInternal(string strPath, string assetName = null)
        {
            // strPath不判断长度,允许为长度为0
            if (strPath != null
                )
            {
                // 转为小写,包名只能是小写
                string strABName = strPath.ToLower();

                // 某些包的assetname比较特殊,比如说Manifest文件
                string realAseetName = null;
                if (assetName != null)
                {
                    realAseetName = assetName;
                }
                else {
                    int index = strPath.LastIndexOf('/');
                    realAseetName = strPath.Substring(index + 1);
                }
                AssetBundle assetBundle = null;
                if (AssetBundleInfoManager.IsExits() == true
                && AssetBundleInfoManager.GetSingel().GetBundleInfo(AppConst.BundleInfoPath) != null
                && AssetBundleInfoManager.GetSingel().GetBundleInfo(AppConst.BundleInfoPath).IsCached(strABName) == true)
                {
                    // 优先从cache加载
                    string cachePath = itfDownloadPath + strABName;
                    assetBundle = AssetBundle.LoadFromFile(cachePath);
                }
                else
                {
                    // cache没找到就从streaming加载
                    Debug.Log("streamingpath: " + itfStreamingPath + " " + strABName);
                    string streamingPath = itfStreamingPath + strABName;
                    assetBundle = AssetBundle.LoadFromFile(streamingPath);
                }

                if (assetBundle != null)
                {
                    // 最后从包中加载出资源
                    Object ob = assetBundle.LoadAsset(realAseetName);

                    // 立即卸载
                    assetBundle.Unload(false);

                    return ob;
                }
            }

            return null;
        }
예제 #7
0
        /// <summary>
        /// 对比版本文件
        /// </summary>
        /// <returns></returns>
        private string[] _CompareBundleInfo()
        {
            string[] toUpdateAssetBundleNames = null;

            if (AssetBundleInfoManager.IsExits() == false)
            {
                return(null);
            }

            // 读取下载路径上的版本文件
            tagBundleInfo localBundleInfo = AssetBundleInfoManager.GetSingel().GetBundleInfo(AssetBundleManager.Instance.itfDownloadPath);

            if (localBundleInfo != null)
            {
                return(tagBundleInfo.Compare(localBundleInfo, m_serverBundleInfo));
            }

            // 读取包内路径上的版本文件
            localBundleInfo = AssetBundleInfoManager.GetSingel().GetBundleInfo(AssetBundleManager.Instance.itfStreamingPath);
            if (localBundleInfo != null)
            {
                return(tagBundleInfo.Compare(localBundleInfo, m_serverBundleInfo));
            }

            //if (toUpdateAssetBundleNames != null
            //    && toUpdateAssetBundleNames.Length != 0)
            //{
            //    // begin download!
            //    for (int i = 0; i < toUpdateAssetBundleNames.Length; i++)
            //    {
            //        Debug.Log("Compare: " + toUpdateAssetBundleNames[i]);
            //    }
            //}

            Debug.Log("local BundleInfo is null!");

            return(toUpdateAssetBundleNames);
        }
예제 #8
0
        /// <summary>
        /// 检查更新
        /// </summary>
        /// <returns></returns>
        private IEnumerator _CheckUpdate()
        {
            // 不检查更新
            if (CheckNewVersion == false)
            {
                // 调用更新完成回调
                if (m_finishUpdateCallback != null)
                {
                    m_finishUpdateCallback(false);
                }

                yield break;
            }

            // 开始检查更新
            //if (m_gameobject == null)
            //{
            //    Debug.LogError("GameMainObj not exist!");
            //    yield break;
            //}

            // 下载BundleInfo包,服务器上的bundleinfofile,用于对比本地文件下载新的包,包下载完毕以后才能将该bundleinfo文件保存到本地
            Coroutine routine = StartCoroutine(_DownloadAssetBundleInternal(m_BundleInfoFileName, true));

            m_listCoroutine.Add(routine);
            yield return(routine);

            //if (m_gameobject == null)
            //{
            //    Debug.LogError("GameMainObj not exist!");
            //    yield break;
            //}

            // 对比需要更新的bundle
            string[] toUpdateAssetBundleNames = _CompareBundleInfo();

            // 没有需要更新的资源,中断协程
            if (toUpdateAssetBundleNames == null ||
                toUpdateAssetBundleNames.Length == 0)
            {
                Debug.Log("nothing need to be updated!");

                // 调用更新完成回调
                if (m_finishUpdateCallback != null)
                {
                    m_finishUpdateCallback(false);
                }
                yield break;
            }

            //if (m_gameobject == null)
            //{
            //    Debug.LogError("GameMainObj not exist!");
            //    yield break;
            //}
            // 开始更新
            Coroutine routine2 = StartCoroutine(_DownloadAssetBundlesAsyc(toUpdateAssetBundleNames));

            m_listCoroutine.Add(routine2);
            yield return(routine2);

            // 保存版本信息
            _SaveFile(m_BundleInfoFileName, m_serverBundleInfo.itfBuffer);

            if (AssetBundleInfoManager.IsExits())
            {
                AssetBundleInfoManager.GetSingel().ReloadBundleInfo(AssetBundleManager.Instance.itfDownloadPath);
            }

            // 强制停止所有协程
            _StopAllCoroutine();

            // 调用更新完成回调
            if (m_finishUpdateCallback != null)
            {
                m_finishUpdateCallback(true);
            }
        }
예제 #9
0
        /// <summary>
        /// 实际使用www下载包的地方
        /// </summary>
        /// <param name="strABName"></param>
        /// <returns></returns>
        private IEnumerator _DownloadAssetBundleInternal(string strABName, bool isBundleInfoFile = false)
        {
            WWW    download = null;
            string url      = m_strBaseDownloadingURL + strABName;

            Debug.Log(url);
            download = new WWW(url);
            yield return(download);

            if (download.error != null)
            {
                Debug.LogError(download.error);
                yield break;
            }

            if (download.isDone &&
                download.assetBundle != null)
            {
                if (isBundleInfoFile == true &&
                    AssetBundleInfoManager.IsExits())
                {
                    //解决乱码问题将下载的bundleinfo 字节流按照assetbundle的方式加载 而不是直接使用字节流
                    AssetBundle.UnloadAllAssetBundles(false); //这里是因为在某个地方已经加载了本地的bundleinfo ,如果不释放掉就不能加载刚刚下下来的这个buffer(暂时还没找到)
                    AssetBundle bundleinfo = AssetBundle.LoadFromMemory(download.bytes);
                    // AssetBundle bundleinfo = bundleinfoRe.assetBundle;
                    if (bundleinfo != null)
                    {
                        TextAsset text = bundleinfo.LoadAsset <TextAsset>("bundleinfo");
                        if (text != null)
                        {
                            m_serverBundleInfo = AssetBundleInfoManager.GetSingel().LoadBundleInfo(text.bytes);
                        }
                    }
                    bundleinfo.Unload(true);
                    // _SaveFile(strABName, download.bytes);
                }

                //如果是bundleinfo的话不应该是直接用字节流去读取数据因为这个字节流是元始的喂解密的bundle字节流
                // _SaveServerInfoAndLoad(strABName, download.bytes);

                else
                {
                    _SaveFile(strABName, download.bytes);
                }

                //包加载成功立即卸载,只需要缓存到本地即可
                if (download.assetBundle != null)
                {
                    download.assetBundle.Unload(false);
                }
                Debug.Log("download " + strABName + " bundle succeed!");
            }
            else
            {
                //包加载失败
                Debug.LogError("assetBundle:" + strABName + " is invalid!");
            }

            //也可以不调用Dispose,unity会在适当的时候释放www资源,如果dispose正在下载的资源,有极大问题,可能崩溃
            //download.Dispose();
        }