Example #1
0
        /// <summary>
        /// 写入下载缓存信息用于断点续传
        /// </summary>
        void SaveDownloadCacheData()
        {
            if (currentState < State.UpdateAssetBundle)
            {
                return;
            }
            if (!Directory.Exists(PathResolver.CACHE_PATH))
            {
                return;
            }
            var manifestPath = PathResolver.GetCacheFileFullName(PathResolver.MAIN_MANIFEST_FILE_NAME);
            var manifestFile = PathResolver.LoadMainManifestByPath(manifestPath);

            if (null == manifestFile)
            {
                return;
            }
            //加载缓存信息文件
            var cache = new DownloadCache();

            cache.Load(PathResolver.DOWNLOADCACHE_FILE_PATH);
            //获取下载器完成任务列表
            if (m_Downloader != null && m_Downloader.completeDownloads != null && m_Downloader.completeDownloads.Count > 0)
            {
                foreach (var name in m_Downloader.completeDownloads)
                {
                    var hashCode = manifestFile.GetAssetBundleHash(name);
                    if (hashCode.isValid && !cache.data.assetBundles.ContainsKey(name))
                    {
                        var elem = new DownloadCacheData.AssetBundle()
                        {
                            assetbundleName = name, hash = hashCode.ToString()
                        };
                        Debug.Log(cache.data.assetBundles.Count + " - Cache Add:" + name);
                        cache.data.assetBundles.Add(name, elem);
                    }
                }
            }

            if (!cache.IsEmpty())
            {
                cache.Save(PathResolver.DOWNLOADCACHE_FILE_PATH);
            }
        }
        static void CoypAssetBundles()
        {
            //Clear Streaming assets foldes
            var platform = PathResolver.GetPlatformName();
            var filePath = Path.Combine(Application.streamingAssetsPath, PathResolver.BundleSaveDirName);

            if (Directory.Exists(filePath))
            {
                Directory.Delete(filePath, true);
            }
            Directory.CreateDirectory(filePath);
            //coyp asset bundle
            try
            {
                var dest = Path.Combine(Path.Combine(Application.streamingAssetsPath, PathResolver.BundleSaveDirName), platform).Replace("\\", "/") + "/";
                var res  = Path.Combine(Path.Combine(Environment.CurrentDirectory, PathResolver.BundleSaveDirName), platform).Replace("\\", "/") + "/";
                FileUtil.CopyFileOrDirectory(res, dest);
            }
            catch (Exception e)
            {
                Debug.LogError("[Assets]No AssetBundle output folder,try to build first");
            }
            AssetDatabase.Refresh();

            //注意包内的资源会自动把isNative标示自动设为true
            var resPath = PathResolver.INITIAL_PATH + "/" + PathResolver.RESOURCES_MANIFEST_FILE_NAME;
            var resCfg  = PathResolver.LoadResourceManifestByPath(resPath);

            if (resCfg != null)
            {
                foreach (var item in resCfg.data.assetbundles.Values)
                {
                    item.isNative = true;
                }
                resCfg.Save(resPath);
            }

            Debug.Log("[Assets]AssetBundle Copy Finish");
        }
Example #3
0
        void CompareAsset(
            ref List <string> downFiles,
            ref List <string> deleteFiles,
            AssetBundleManifest oldManifest,
            AssetBundleManifest newManifest,
            ResourcesManifest oldResManifest,
            ResourcesManifest newResManifest)
        {
            if (downFiles != null)
            {
                downFiles.Clear();
            }
            if (deleteFiles != null)
            {
                deleteFiles.Clear();
            }
            if (oldManifest == null)
            {
                Error(ErrorCode.LoadMainManifestFailed, "Local Manifest no Find");
                return;
            }
            if (newManifest == null)
            {
                Error(ErrorCode.LoadNewMainManifestFailed, "Load New MainManifest no Find");
                return;
            }
            if (newResManifest == null)
            {
                Error(ErrorCode.loadNewResourcesManiFestFailed, "Load New ResourceManifest no Find");
                return;
            }
            //标记位
            var old_ver_bit        = 1 << 0; //存在旧资源
            var new_ver_bit        = 1 << 1; //存在新资源
            var old_ver_native_bit = 1 << 2; //存在旧的本地资源
            var new_ver_native_bit = 1 << 3; //存在新的本地资源

            var tempDic = new Dictionary <string, int>();
            //标记旧资源
            var allAssetBundle = oldManifest.GetAllAssetBundles();

            foreach (var name in allAssetBundle)
            {
                SetDictionaryBit(ref tempDic, name, old_ver_bit);
            }
            //标记新资源
            var allNewAssetBundle = newManifest.GetAllAssetBundles();

            foreach (var name in allNewAssetBundle)
            {
                SetDictionaryBit(ref tempDic, name, new_ver_bit);
            }
            //标记旧本地资源
            if (oldResManifest.data != null && oldResManifest.data.assetbundles != null)
            {
                var itor = oldResManifest.data.assetbundles.GetEnumerator();
                while (itor.MoveNext())
                {
                    if (itor.Current.Value.isNative)
                    {
                        var name = itor.Current.Value.assetBundleName;
                        SetDictionaryBit(ref tempDic, name, old_ver_native_bit);
                    }
                }
            }
            //标记新的本地资源
            if (newResManifest.data != null && newResManifest.data.assetbundles != null)
            {
                var itor = newResManifest.data.assetbundles.GetEnumerator();
                while (itor.MoveNext())
                {
                    if (itor.Current.Value.isNative)
                    {
                        var name = itor.Current.Value.assetBundleName;
                        SetDictionaryBit(ref tempDic, name, new_ver_native_bit);
                    }
                }
            }

            //优先级:both>add>delete
            //both: 第0位和第1位标记的
            //delete : 第0位标记
            //add:第2位未标记,第3位标记的
            int both      = old_ver_bit | new_ver_bit;//2个版本都存在的资源
            var addFiles  = new List <string>();
            var bothFiles = new List <string>();

            using (var itor = tempDic.GetEnumerator())
            {
                while (itor.MoveNext())
                {
                    var name = itor.Current.Key;
                    var mask = itor.Current.Value;
                    if ((mask & new_ver_native_bit) == new_ver_native_bit &&
                        (mask & old_ver_native_bit) == 0)
                    {
                        addFiles.Add(name);
                    }
                    else if ((mask & both) == both)
                    {
                        bothFiles.Add(name);
                    }
                    else if ((mask & old_ver_bit) == old_ver_bit)
                    {
                        deleteFiles.Add(name);
                    }
                }
                itor.Dispose();
            }

            //加载下载缓存数据
            var download = new DownloadCache();

            download.Load(PathResolver.DOWNLOADCACHE_FILE_PATH);
            if (download.IsEmpty())
            {
                download = null;
            }

            //记录需要下载的文件
            {
                //加入新增文件
                downFiles.AddRange(addFiles);
                //判断同时存在文件的哈希
                foreach (var name in bothFiles)
                {
                    var fullName = PathResolver.GetFileFullName(name);
                    if (File.Exists(fullName))
                    {
                        //判断哈希是否相同
                        var oldHash = oldManifest.GetAssetBundleHash(name).ToString();
                        var newHash = newManifest.GetAssetBundleHash(name).ToString();
                        if (oldHash.CompareTo(newHash) == 0)
                        {
                            continue;
                        }
                        downFiles.Add(name);
                    }
                }

                //过滤已下载的文件
                if (null != download)
                {
                    var itor = download.data.assetBundles.GetEnumerator();
                    while (itor.MoveNext())
                    {
                        var elem     = itor.Current.Value;
                        var name     = elem.assetbundleName;
                        var fullName = PathResolver.GetFileFullName(name);
                        if (File.Exists(fullName))
                        {
                            var cacheHash = elem.hash;
                            var newHash   = newManifest.GetAssetBundleHash(name).ToString();
                            if (!string.IsNullOrEmpty(cacheHash) && cacheHash.CompareTo(newHash) == 0)
                            {
                                downFiles.Remove(name);
                            }
                        }
                    }
                }
            }
        }
Example #4
0
 ResourcesManifest OnLoadResourceFile()
 {
     return(PathResolver.LoadResourceManifest());
 }
Example #5
0
        /// <summary>
        /// 更新AB资源
        /// </summary>
        /// <returns></returns>
        IEnumerator StartUpdateAssetBundle()
        {
            if (errorCode != ErrorCode.None)
            {
                yield break;
            }

            UpdateCompleteValue(0f, 0f);
            //载入ResourceManifest
            var oldResManifestFile = OnLoadResourceFile();
            var path = PathResolver.GetCacheFileFullName(PathResolver.RESOURCES_MANIFEST_FILE_NAME);
            var newResManifestFile = PathResolver.LoadResourceManifestByPath(path);

            if (null == newResManifestFile)
            {
                Error(ErrorCode.loadNewResourcesManiFestFailed, "Can't Load new version ResourceManifest");
                yield break;
            }

            //载入MainManifest
            var oldManifest = PathResolver.LoadMainManifest();
            var newManifest = PathResolver.LoadCacheMainManifest();

            if (null == newManifest)
            {
                Error(ErrorCode.LoadNewMainManifestFailed, "can't load new version MainManifest");
                yield break;
            }

            var downFiles   = new List <string>();
            var deleteFiles = new List <string>();

            CompareAsset(ref downFiles, ref deleteFiles, oldManifest, newManifest, oldResManifestFile, newResManifestFile);

            //删除废气文件
            if (deleteFiles.Count > 0)
            {
                for (int i = 0; i < deleteFiles.Count; ++i)
                {
                    var fullName = PathResolver.GetFileFullName(deleteFiles[i]);
                    if (File.Exists(fullName))
                    {
                        File.Delete(fullName);
                        yield return(0);
                    }
                }
            }

            //更新下载资源
            m_Downloader = new AssetBundleDownloader(m_CurUrl);
            m_Downloader.Start(PathResolver.PATH, downFiles, newResManifestFile);
            while (!m_Downloader.isDone)
            {
                UpdateCompleteValue(m_Downloader.completedSize, m_Downloader.totalSize);
                yield return(0);
            }
            if (m_Downloader.isFailed)
            {
                Error(ErrorCode.DonwloadAssetBundleFailed);
                yield break;
            }
        }
Example #6
0
 public AssetBundleBuilder4x(PathResolver pathResolver) : base(pathResolver)
 {
 }
Example #7
0
        public override void Export()
        {
            #region 源代码
            base.Export();

            var platform = PathResolver.GetPlatformName();
            var filePath = Path.Combine(Path.Combine(Environment.CurrentDirectory, PathResolver.BundleSaveDirName), platform).Replace("\\", "/") + "/";
            if (Directory.Exists(filePath))
            {
                Directory.Delete(filePath, true);
            }
            Directory.CreateDirectory(filePath);

            List <AssetBundleBuild> list = new List <AssetBundleBuild>();
            //标记所有 asset bundle name
            var all = AssetBundleUtils.GetAll();
            for (int i = 0; i < all.Count; i++)
            {
                AssetTarget target = all[i];
                if (target.needSelfExport)
                {
                    AssetBundleBuild build = new AssetBundleBuild();
                    build.assetBundleName = target.bundleName;
                    build.assetNames      = new string[] { target.assetPath };
                    list.Add(build);
                }
            }

            //开始打包
            BuildPipeline.BuildAssetBundles(
                PathResolver.BundleSavePath,
                list.ToArray(),
                BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.DeterministicAssetBundle,
                EditorUserBuildSettings.activeBuildTarget);

#if UNITY_5_1 || UNITY_5_2
            AssetBundle ab = AssetBundle.CreateFromFile(PathResolver.BundleSavePath + PathResolver.GetPlatformForAssetBundles(Application.platform));
#else
            var ab = AssetBundle.LoadFromFile(PathResolver.BundleSavePath + "/" + PathResolver.GetPlatformName());
#endif
            var manifest = ab.LoadAsset("AssetBundleManifest") as AssetBundleManifest;
            //hash
            for (int i = 0; i < all.Count; i++)
            {
                AssetTarget target = all[i];
                if (target.needSelfExport)
                {
                    Hash128 hash = manifest.GetAssetBundleHash(target.bundleName);
                    target.bundleCrc = hash.ToString();
                }
            }

            this.SaveDepAll(all);
            this.SaveSpriteAll(all);
            this.SaveAssetAll(all);
            this.ExportResourcesManifestFile(manifest);
            ab.Unload(true);
            this.RemoveUnused(all);


            AssetDatabase.RemoveUnusedAssetBundleNames();
            AssetDatabase.Refresh();

            Debug.Log("[Assets]Build Finish!");
            #endregion
        }
 protected override IEnumerator LoadFromPackage()
 {
     _assetBundleSourceFile = PathResolver.GetBundleSourceFile(bundleName, false);
     return(base.LoadFromPackage());
 }