Example #1
0
        /// <summary>
        /// 更新基础的和必须的AssetBundle
        /// </summary>
        /// <param name="assetUrl"></param>
        /// <param name="groupName"></param>
        /// <param name="progressInfo"></param>
        /// <param name="loadBaseBundle"></param>
        /// <returns></returns>
        public IEnumerator UpdateBaseAssetBundle(string assetUrl, ProgressInfo progressInfo, bool loadBaseBundle = false)
        {
            //下载服务器的Version 文件
            string        serverVersionFilePath = assetUrl + "/Version.txt";
            DataContainer dataContainer         = new DataContainer();

            yield return(LoadVersionFile(serverVersionFilePath, dataContainer));

            if (string.IsNullOrEmpty(dataContainer.strData))
            {
                progressInfo.sucess = false;
                progressInfo.fail   = true;
                progressInfo.start  = true;
                DebugUtil.LogError("load version file fail ,Url:", assetUrl);
                yield break;
            }
            AssetBundleVersionInfo _serverVersionInfo = JsonConvert.DeserializeObject <AssetBundleVersionInfo>(dataContainer.strData);

            //临时存储versionInfo文件
            FileUtil.CreateFile(FilePathTools.persistentDataPath_Platform, "Version.txt.temp", dataContainer.strData);


            List <string> mustGroups = GetMustGroups(_serverVersionInfo.preloadConditions);
            List <AssetBundleGroupInfo> baseGroupinfos = new List <AssetBundleGroupInfo>();

            foreach (var item in _serverVersionInfo.assetBundleGroups.Values)
            {
                if (item.BaseGroup || mustGroups.Contains(item.GroupName))
                {
                    baseGroupinfos.Add(item);
                }
            }


            List <string> needLoadBundleNames = new List <string>();

            foreach (var group in baseGroupinfos)
            {
                foreach (var bundleInfo in group.AssetBundles)
                {
                    if (FileUtil.IsFileExistsInPersistetntDataPath(bundleInfo.Value.AssetBundleName, bundleInfo.Value.HashString))
                    {
                        continue;
                    }
                    needLoadBundleNames.Add(bundleInfo.Value.AssetBundleName);
                }
            }
            yield return(LoadAssetBundle(assetUrl, needLoadBundleNames, progressInfo));


            //重命名为Version.txt
            FileUtil.Rename(FilePathTools.persistentDataPath_Platform + "/Version.txt.temp", FilePathTools.persistentDataPath_Platform + "/Version.txt");

            //更新_versionInfo
            //string persistentVersionFilePath = string.Format("{0}/{1}", FilePathTools.persistentDataPath_Platform_ForWWWLoad, versionFileName);
            //DataContainer dataContainer2 = new DataContainer();
            //yield return LoadVersionFile(persistentVersionFilePath, dataContainer2);
            //this._versionInfo = JsonConvert.DeserializeObject<AssetBundleVersionInfo>(dataContainer2.strData);
            this._versionInfo = _serverVersionInfo;
            this.assetUrl     = assetUrl;//记录当前基础素材的 服务器地址 ,用于后面下载其他group资源。
        }
Example #2
0
        /// <summary>
        /// 1.检测是否需要将streaming下文件移动到persistent下
        /// </summary>
        /// <returns></returns>
        public IEnumerator Init()
        {
            DebugUtil.Log("streamingAssetsPath_Platform:" + FilePathTools.streamingAssetsPath_Platform);
            DebugUtil.Log("streamingAssetsPath_Platform_ForWWWLoad:" + FilePathTools.streamingAssetsPath_Platform_ForWWWLoad);
            DebugUtil.Log("persistentDataPath_Platform:" + FilePathTools.persistentDataPath_Platform);
            DebugUtil.Log("persistentDataPath_Platform_ForWWWLoad:" + FilePathTools.persistentDataPath_Platform_ForWWWLoad);

            assetBundleGroupLoadHelpers = new Dictionary <string, AssetBundleGroupLoadHelperItem>();
#if !UNITY_EDITOR
            AssetBundleConfig.Inst.UseAssetBundle = true;// 非编辑器模式下,永远使用ab包
#endif
            if (!AssetBundleConfig.Inst.UseAssetBundle)
            {
                yield break;
            }
#if UNITY_EDITOR
            //Editor状态下 删除persistent 方便快速测试
            //if (Directory.Exists(FilePathTools.persistentDataPath_Platform))
            //    Directory.Delete(FilePathTools.persistentDataPath_Platform,true);
#endif
            //读取persistent下的VersionFile
            string oldPersistentVersionFilePath = string.Format("{0}/{1}", FilePathTools.persistentDataPath_Platform, versionFileName);
            if (File.Exists(oldPersistentVersionFilePath))
            {
                string oldPersistentVersionFilePathWWWLoad = string.Format("{0}/{1}", FilePathTools.persistentDataPath_Platform_ForWWWLoad, versionFileName);
                DebugUtil.Log("load oldPersistent VersionFile,path:" + oldPersistentVersionFilePathWWWLoad);
                DataContainer dataContainer = new DataContainer();
                yield return(LoadVersionFile(oldPersistentVersionFilePathWWWLoad, dataContainer));

                if (!string.IsNullOrEmpty(dataContainer.strData))
                {
                    this._oldPersistentVersionInfo = JsonConvert.DeserializeObject <AssetBundleVersionInfo>(dataContainer.strData);
                }
                else
                {
                    this._oldPersistentVersionInfo = null;
                }
            }
            else
            {
                this._oldPersistentVersionInfo = null;
            }


            //读取streaming下的VersionFile
            string        streamingVersionFilePath = string.Format("{0}/{1}", FilePathTools.streamingAssetsPath_Platform_ForWWWLoad, versionFileName);
            DataContainer dataContainerStreaming   = new DataContainer();
            yield return(LoadVersionFile(streamingVersionFilePath, dataContainerStreaming));

            this._streamingVersionInfo = JsonConvert.DeserializeObject <AssetBundleVersionInfo>(dataContainerStreaming.strData);

            //移动Streaming
            if (_oldPersistentVersionInfo == null || CommonUtil.CompareVersion(this._streamingVersionInfo.Version, this._oldPersistentVersionInfo.Version) > 0)
            {
                yield return(MoveStreamingToPersistent());

                this._versionInfo = this._streamingVersionInfo;
            }
            else
            {
                this._versionInfo = this._oldPersistentVersionInfo;
            }

            while (!_finishMovingStreaming)
            {
                yield return(null);
            }
        }
        /// <summary>
        /// Bundle打包,并生成Version配置文件
        /// 1.Bundle包导出至AssttBundleOut内
        /// 2.把基础包拷贝到StreamingAssets文件夹内
        /// 3.生成VersionInfo文件,拷贝到StreamingAssets内
        /// </summary>
        public static void BuildAllAssetBundle(BuildTarget buildTarget = BuildTarget.NoTarget)
        {
            if (AssetBundleConfig.Instance.CompleteAsset)
            {
                string _assetVersion = AssetBundleConfig.Instance.AssetVersion;
            }
            else
            {
                string _assetVersion = CommonUtil.SubAssetVersionIncrease(AssetBundleConfig.Instance.AssetVersion);
            }
            exportPathParent = FilePathTools.assetBundleOutPath + "/" + AssetBundleConfig.Instance.AssetVersion;
            //EditorUtility.DisplayCancelableProgressBar("打包中...", "0%", 0.01f);
            AssetDatabase.RemoveUnusedAssetBundleNames();

            assetBundleVersionInfo = new AssetBundleVersionInfo
            {
                Version           = AssetBundleConfig.Instance.AssetVersion,
                preloadConditions = AssetBundleConfig.Instance.preloadConditions
            };

            //EditorUtility.DisplayCancelableProgressBar("打包中...", "10%", 0.1f);
            //删除老的bundle,(如果不删除可能会引起打包崩溃,但基本没发生过)
            //(不删除老的bundle,重新打bundle会很快)
            //if (Directory.Exists(exportPath))
            //    Directory.Delete(exportPath, true);

            if (Directory.Exists(FilePathTools.streamingAssetsPath_Platform))
            {
                Directory.Delete(FilePathTools.streamingAssetsPath_Platform, true);
            }

            DirectoryInfo outFold = new DirectoryInfo(FilePathTools.assetBundleOutPath);

            DirectoryInfo[] outBundleFolds = outFold.GetDirectories();
            if (outBundleFolds.Length > 0)
            {
                DirectoryInfo exportFold = outBundleFolds[0];
                string        foldName   = exportFold.Name;
                if (foldName != AssetBundleConfig.Instance.AssetVersion)
                {
                    exportFold.MoveTo(exportPathParent);
                }
            }

            if (!Directory.Exists(exportPathParent))
            {
                Directory.CreateDirectory(exportPathParent);
            }

            //EditorUtility.ClearProgressBar();
            float deltaProgress = 0.8f / AssetBundleConfig.Instance.Groups.Length;

            //按Group打包
            for (int i = 0; i < AssetBundleConfig.Instance.Groups.Length; i++)
            {
                float currProgress = 0.1f + deltaProgress * i;
                //EditorUtility.DisplayCancelableProgressBar("打包中...", currProgress * 100 + "%", currProgress);
                BundleGroup bundleGroup = AssetBundleConfig.Instance.Groups[i];
                BuildGroup(bundleGroup, Path.Combine(exportPathParent, bundleGroup.GroupName), buildTarget);
            }

            //保存version信息,把version存到exportPathParent的上一级目录
            string assetBundleVersionInfoStr = assetBundleVersionInfo.ToString();
            string versionFileName           = "Version.txt";

            FileUtil.CreateFile(exportPathParent, versionFileName, assetBundleVersionInfoStr);
            //EditorUtility.DisplayCancelableProgressBar("打包中...", "90%", 0.90f);

            //拷贝 InInitialPacket==true 的包到streamingasset文件夹下,Version.txt文件也拷贝到streamingasset文件夹下
            if (!Directory.Exists(FilePathTools.streamingAssetsPath_Platform))
            {
                Directory.CreateDirectory(FilePathTools.streamingAssetsPath_Platform);
            }

            foreach (BundleGroup group in AssetBundleConfig.Instance.Groups)
            {
                List <string> paths = group.GetBundlePaths(GetBundleMethod.All);
                // 拷贝到AssetBundleOutRelease下
                foreach (string item in paths)
                {
                    string srcFile  = Path.Combine(Path.Combine(exportPathParent, group.GroupName), item.ToLower());
                    string destFile = Path.Combine(FilePathTools.assetBundleOutReleasePath + "/" + AssetBundleConfig.Instance.AssetVersion, item.ToLower());
                    FileUtil.CopyFile(srcFile, destFile);
                }
                if (!group.BaseGroup && !AssetBundleConfig.Instance.CompleteAsset)
                {
                    continue;
                }

                // 拷贝到streamingAsset下
                foreach (string item in paths)
                {
                    string srcFile  = Path.Combine(Path.Combine(exportPathParent, group.GroupName), item.ToLower());
                    string destFile = FilePathTools.streamingAssetsPath_Platform + "/" + item.ToLower();
                    FileUtil.CopyFile(srcFile, destFile);
                }
            }
            FileUtil.CopyFile(exportPathParent + "/" + versionFileName, FilePathTools.streamingAssetsPath_Platform + "/" + "Version.txt");
            //如果不是CompleteAsset,AssetbundleOut下Version的副素材版本号+1。
            //if (!AssetBundleConfig.Instance.CompleteAsset)
            //{
            //    FileUtil.CreateFile(exportPathParent, "Version.txt", assetBundleVersionInfo.SubAssetVersionIncrease().ToString());
            //}
            //AssetBundleOut文件夹下Version拷贝到All文件夹下
            FileUtil.CopyFile(exportPathParent + "/" + versionFileName, FilePathTools.assetBundleOutReleasePath + "/" + AssetBundleConfig.Instance.AssetVersion + "/" + "Version.txt");


            //EditorUtility.DisplayCancelableProgressBar("打包中...", "100%", 1f);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            DebugUtil.Log("AssetBundle 打包成功");
        }