Beispiel #1
0
        /// <summary>
        /// 通过一个路径加载ab
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="prority">优先级</param>
        /// <param name="handler">回调</param>
        /// <returns></returns>
        public AssetBundleLoader Load(string path, int prority, LoadAssetCompleteHandler handler = null)
        {
#if _AB_MODE_
            AssetBundleLoader loader = this.CreateLoader(HashUtil.Get(path.ToLower()) + ".ab", path);
#else
            AssetBundleLoader loader = this.CreateLoader(path);
#endif
            loader.prority     = prority;
            loader.onComplete += handler;

            _isCurrentLoading = true;
            _nonCompleteLoaderSet.Add(loader);
            _thisTimeLoaderSet.Add(loader);

            return(loader);
        }
Beispiel #2
0
        public AssetBundleInfo GetBundleInfo(string key)
        {
            key = key.ToLower();
#if _AB_MODE_
            key = HashUtil.Get(key) + ".ab";
#endif
            var e = _loadedAssetBundle.GetEnumerator();
            while (e.MoveNext())
            {
                AssetBundleInfo abi = e.Current.Value;
                if (abi.bundleName == key)
                {
                    return(abi);
                }
            }
            return(null);
        }
        IEnumerator UpdateAssetBundle(string assetBundleServerPath, string assetBundleLocalPath)
        {
            string localAssetBundleManifestPath  = $"{assetBundleLocalPath}/{AssetBundlePathResolver.Instance.BundleSaveDirName}";
            string serverAssetBundleManifestPath = $"{assetBundleServerPath}/{AssetBundlePathResolver.Instance.BundleSaveDirName}";

            string localDependFilePath  = $"{assetBundleLocalPath}/{AssetBundlePathResolver.Instance.DependFileName}";
            string serverDependFilePath = $"{assetBundleServerPath}/{AssetBundlePathResolver.Instance.DependFileName}";

            AssetBundleManifest localAssetBundleManifest  = null;
            AssetBundleManifest serverAssetBundleManifest = null;

            if (FileUtility.CheckFileIsExist(localAssetBundleManifestPath) == false)
            {
                yield return(MainThreadDispatcher.StartCoroutine(DownLoadAssetBundle(serverAssetBundleManifestPath, localAssetBundleManifestPath)));

                if (m_HasError)
                {
                    deleteFolder = true;
                    yield break;
                }

                yield return(MainThreadDispatcher.StartCoroutine(DownLoadAssetBundle(serverDependFilePath, localDependFilePath)));

                if (m_HasError)
                {
                    deleteFolder = true;
                    yield break;
                }

                localAssetBundleManifest           = LoadLocalAssetBundleManifest(localAssetBundleManifestPath);
                m_NeedDownloadAssetBundleNamesList = localAssetBundleManifest.GetAllAssetBundles().ToList();
                int currentCount = 0;
                for (int i = 0; i < m_NeedDownloadAssetBundleNamesList.Count; i++)
                {
                    string assetBundleName = m_NeedDownloadAssetBundleNamesList[i];

                    string serverAssetBundlesPath = string.Format("{0}/{1}", assetBundleServerPath, assetBundleName);

                    string localAssetBundlesPath = string.Format("{0}/{1}", assetBundleLocalPath, assetBundleName);

                    yield return(MainThreadDispatcher.StartCoroutine(DownLoadAssetBundle(serverAssetBundlesPath, localAssetBundlesPath)));

                    if (m_HasError)
                    {
                        deleteFolder = true;
                        yield break;
                    }

                    currentCount++;
                    AssetBundleUpdateProcessEvent?.Invoke((float)currentCount / m_NeedDownloadAssetBundleNamesList.Count);
                }
            }
            else
            {
                byte[] localAssetBundleManifestBytes = FileUtility.FileConvertToBytes(localAssetBundleManifestPath);
                //load AssetBundleManifest to memory from server
                WWW serverWWW = new WWW(serverAssetBundleManifestPath);

                yield return(serverWWW);

                if (serverWWW.error != null)
                {
                    m_HasError = true;
                    yield break;
                }

                byte[] serverAssetBundleManifestBytes = serverWWW.bytes;
                serverWWW.Dispose();

                if (HashUtil.Get(localAssetBundleManifestBytes) == HashUtil.Get(serverAssetBundleManifestBytes))
                {
                    AssetBundleUpdateProcessEvent?.Invoke(1);
                    yield break;
                }

                yield return(MainThreadDispatcher.StartCoroutine(DownLoadAssetBundle(serverDependFilePath, localDependFilePath)));

                if (m_HasError)
                {
                    deleteFolder = true;
                    yield break;
                }

                serverAssetBundleManifest = LoadAssetBundleManifest(serverAssetBundleManifestBytes);
                localAssetBundleManifest  = LoadLocalAssetBundleManifest(localAssetBundleManifestPath);

                string[] localAssetBundleNames  = localAssetBundleManifest.GetAllAssetBundles();
                string[] serverAssetBundleNames = serverAssetBundleManifest.GetAllAssetBundles();

                ///Check each AssetBundles from server
                for (int i = 0; i < serverAssetBundleNames.Length; i++)
                {
                    string assetBundleName = serverAssetBundleNames[i];

                    if (localAssetBundleNames.Contains(assetBundleName))
                    {
                        Hash128 localAssetBundleHash = localAssetBundleManifest.GetAssetBundleHash(assetBundleName);

                        Hash128 serverAssetBundleHash = serverAssetBundleManifest.GetAssetBundleHash(assetBundleName);

                        //if the hash value is different,delete the AssetBundle file in local machine,then download the AssetBundle from server
                        if (localAssetBundleHash != serverAssetBundleHash)
                        {
                            m_NeedDownloadAssetBundleNamesList.Add(assetBundleName);
                        }
                    }
                    else
                    {
                        m_NeedDownloadAssetBundleNamesList.Add(assetBundleName);
                    }
                }

                //delete redundant AssetBundles in local machine
                for (int i = 0; i < localAssetBundleNames.Length; i++)
                {
                    string assetBundleName = localAssetBundleNames[i];

                    if (!serverAssetBundleNames.Contains(assetBundleName))
                    {
                        string deleteAssetBundlePath = string.Format("{0}/{1}", assetBundleLocalPath, assetBundleName);

                        //delete redundant AssetBundles
                        FileUtility.DeleteFile(deleteAssetBundlePath);
                        FileUtility.IfDeletedFileCurrentDirectoryIsEmptyDeleteRecursively(deleteAssetBundlePath);
                    }
                }
                int currentCount = 0;
                for (int i = 0; i < m_NeedDownloadAssetBundleNamesList.Count; i++)
                {
                    string assetBundleName = m_NeedDownloadAssetBundleNamesList[i];

                    string serverAssetBundlesPath = string.Format("{0}/{1}", assetBundleServerPath, assetBundleName);

                    string localAssetBundlesPath = string.Format("{0}/{1}", assetBundleLocalPath, assetBundleName);

                    yield return(MainThreadDispatcher.StartCoroutine(DownLoadAssetBundle(serverAssetBundlesPath, localAssetBundlesPath)));

                    if (m_HasError)
                    {
                        deleteFolder = true;
                        yield break;
                    }

                    currentCount++;
                    AssetBundleUpdateProcessEvent?.Invoke((float)currentCount / m_NeedDownloadAssetBundleNamesList.Count);
                }

                FileUtility.BytesConvertToFile(serverAssetBundleManifestBytes, localAssetBundleManifestPath);
            }
            AssetBundleUpdateProcessEvent?.Invoke(1);
            serverAssetBundleManifest = null;
            localAssetBundleManifest  = null;
            Resources.UnloadUnusedAssets();
        }