public static void Build(string externalResourcesPath)
        {
            var exportPath = GetExportPath();

            if (string.IsNullOrEmpty(exportPath))
            {
                return;
            }

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

            EditorApplication.LockReloadAssemblies();

            #if ENABLE_CRIWARE
            CriAssetGenerator.Generate(exportPath, externalResourcesPath);
            #endif

            // CriAssetGeneratorでCriのManifestファイルを生成後に実行.
            var assetBundleManifest = BuildAssetBundle.BuildAllAssetBundles(exportPath);

            // ビルド成果物の情報をAssetInfoManifestに書き込み.
            AssetInfoManifestGenerator.SetAssetFileInfo(exportPath, externalResourcesPath, assetBundleManifest);

            // 再度AssetInfoManifestだけビルドを実行.
            BuildAssetBundle.BuildAssetInfoManifest(exportPath, externalResourcesPath);

            // 不要ファイル削除.
            BuildAssetBundle.DeleteUnUseFiles(exportPath);

            // AssetBundleファイルをパッケージ化.
            BuildAssetBundle.BuildPackage(exportPath);

            EditorApplication.UnlockReloadAssemblies();

            UnityConsole.Event(ExternalResources.ConsoleEventName, ExternalResources.ConsoleEventColor, "Build ExternalResource Complete.");
        }
        public static void Build(string externalResourcesPath, AssetManageConfig assetManageConfig)
        {
            var exportPath = GetExportPath();

            if (string.IsNullOrEmpty(exportPath))
            {
                return;
            }

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

            EditorApplication.LockReloadAssemblies();

            try
            {
                // アセット情報ファイルを生成.
                var assetInfoManifest = AssetInfoManifestGenerator.Generate(externalResourcesPath, assetManageConfig);

                // キャッシュ済みアセットバンドルのハッシュ値取得.
                var cachedAssetBundleHashs = BuildAssetBundle.GetCachedAssetBundleHash();

                // CRIアセットを生成.
                #if ENABLE_CRIWARE_ADX || ENABLE_CRIWARE_SOFDEC
                CriAssetGenerator.Generate(exportPath, externalResourcesPath, assetInfoManifest);
                #endif

                // AssetBundleをビルド.
                var assetBundleManifest = BuildAssetBundle.BuildAllAssetBundles();

                // 不要になった古いAssetBundle削除.
                BuildAssetBundle.CleanUnUseAssetBundleFiles();

                // ビルド成果物の情報をAssetInfoManifestに書き込み.

                var assetBundlePath = BuildAssetBundle.GetAssetBundleOutputPath();

                AssetInfoManifestGenerator.SetAssetBundleFileInfo(assetBundlePath, externalResourcesPath, assetBundleManifest);

                #if ENABLE_CRIWARE_ADX || ENABLE_CRIWARE_SOFDEC
                AssetInfoManifestGenerator.SetCriAssetFileInfo(exportPath, externalResourcesPath, assetBundleManifest);
                #endif

                // アセットバンドルの参照情報をAssetInfoManifestに書き込み.
                BuildAssetBundle.SetDependencies(assetInfoManifest, assetBundleManifest);

                // 再度AssetInfoManifestだけビルドを実行.
                BuildAssetBundle.BuildAssetInfoManifest(externalResourcesPath);

                // 更新が必要なパッケージファイルを削除.
                BuildAssetBundle.CleanOldPackage(cachedAssetBundleHashs);

                // AssetBundleファイルをパッケージ化.
                BuildAssetBundle.BuildPackage(exportPath, assetInfoManifest, assetManageConfig.CryptPassword);

                // 出力先フォルダを開く.
                UnityEditorUtility.OpenFolder(exportPath);

                UnityConsole.Event(ExternalResources.ConsoleEventName, ExternalResources.ConsoleEventColor, "Build ExternalResource Complete.");
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                throw;
            }
            finally
            {
                EditorApplication.UnlockReloadAssemblies();
            }
        }
Ejemplo n.º 3
0
        public static async Task <string> Build(string exportPath, AssetInfoManifest assetInfoManifest, bool openExportFolder = true)
        {
            if (string.IsNullOrEmpty(exportPath))
            {
                return(null);
            }

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

            var versionHash = string.Empty;

            var assetManagement = AssetManagement.Instance;

            assetManagement.Initialize();

            var buildAssetBundle = new BuildAssetBundle(BundlePipeline);

            EditorApplication.LockReloadAssemblies();

            try
            {
                var stopwatch = System.Diagnostics.Stopwatch.StartNew();

                var logBuilder = new StringBuilder();

                var manageConfig = ManageConfig.Instance;

                var cryptoKey = manageConfig.CryptoKey;
                var cryptoIv  = manageConfig.CryptoIv;

                var assetBundlePath = buildAssetBundle.GetAssetBundleOutputPath();

                // 暗号化鍵情報の変更チェック.

                var cryptoChanged = BuildAssetBundlePackage.CheckCryptoFile(assetBundlePath, cryptoKey, cryptoIv);

                using (new DisableStackTraceScope())
                {
                    var processTime = System.Diagnostics.Stopwatch.StartNew();

                    //------ アセットバンドル名を設定------

                    using (new BuildLogScope(logBuilder, processTime, "ApplyAllAssetBundleName"))
                    {
                        assetManagement.ApplyAllAssetBundleName();
                    }

                    //------ キャッシュ済みアセットバンドルの最終更新日時取得 ------

                    Dictionary <string, DateTime> cachedFileLastWriteTimeTable = null;

                    using (new BuildLogScope(logBuilder, processTime, "GetCachedFileLastWriteTimeTable"))
                    {
                        cachedFileLastWriteTimeTable = await buildAssetBundle.GetCachedFileLastWriteTimeTable();
                    }

                    //------ CRIアセットを生成 ------

                    #if ENABLE_CRIWARE_ADX || ENABLE_CRIWARE_SOFDEC
                    using (new BuildLogScope(logBuilder, processTime, "GenerateCriAsset"))
                    {
                        CriAssetGenerator.Generate(exportPath, assetInfoManifest);
                    }
                    #endif

                    //------ AssetBundleをビルド ------

                    BuildResult buildResult = null;

                    using (new BuildLogScope(logBuilder, processTime, "BuildAllAssetBundles"))
                    {
                        buildResult = buildAssetBundle.BuildAllAssetBundles();
                    }

                    if (!buildResult.IsSuccess)
                    {
                        Debug.LogError("Build ExternalResource failed.");

                        return(null);
                    }

                    //------ 未登録のアセットバンドル情報追加 ------

                    using (new BuildLogScope(logBuilder, processTime, "AddUnregisteredAssetInfos"))
                    {
                        buildAssetBundle.AddUnregisteredAssetInfos(assetInfoManifest, buildResult);
                    }

                    //------ 不要になった古いAssetBundle削除 ------

                    using (new BuildLogScope(logBuilder, processTime, "CleanUnUseAssetBundleFiles"))
                    {
                        buildAssetBundle.CleanUnUseAssetBundleFiles(buildResult);
                    }

                    //------ AssetBundleファイルをパッケージ化 ------

                    // 暗号化鍵情報の書き込み.

                    using (new BuildLogScope(logBuilder, processTime, "CreateCryptoFile"))
                    {
                        BuildAssetBundlePackage.CreateCryptoFile(assetBundlePath, cryptoKey, cryptoIv);
                    }

                    // 更新対象のアセット情報取得.

                    var assetInfos        = new AssetInfo[0];
                    var updatedAssetInfos = new AssetInfo[0];

                    using (new BuildLogScope(logBuilder, processTime, "GetUpdateTargetAssetInfo"))
                    {
                        assetInfos = buildAssetBundle.GetAllTargetAssetInfo(assetInfoManifest);

                        // 暗号化キーが変わっていたら全て更新対象.
                        if (cryptoChanged)
                        {
                            updatedAssetInfos = assetInfos;
                        }
                        // 差分がある対象だけ抽出.
                        else
                        {
                            updatedAssetInfos = await buildAssetBundle.GetUpdateTargetAssetInfo(assetInfoManifest, cachedFileLastWriteTimeTable);
                        }
                    }

                    // パッケージファイル作成.

                    using (new BuildLogScope(logBuilder, processTime, "BuildPackage"))
                    {
                        await BuildAssetBundlePackage.BuildAllAssetBundlePackage(exportPath, assetBundlePath, assetInfos, updatedAssetInfos, cryptoKey, cryptoIv);
                    }

                    //------ ビルド成果物の情報をAssetInfoManifestに書き込み ------

                    using (new BuildLogScope(logBuilder, processTime, "AssetInfoManifest : SetAssetBundleFileInfo"))
                    {
                        await AssetInfoManifestGenerator.SetAssetBundleFileInfo(assetBundlePath, assetInfoManifest, buildResult);

                        #if ENABLE_CRIWARE_ADX || ENABLE_CRIWARE_SOFDEC
                        await AssetInfoManifestGenerator.SetCriAssetFileInfo(exportPath, assetInfoManifest);
                        #endif
                    }

                    //------ アセットバンドルの参照情報をAssetInfoManifestに書き込み ------

                    using (new BuildLogScope(logBuilder, processTime, "AssetInfoManifest : SetAssetBundleDependencies"))
                    {
                        buildAssetBundle.SetDependencies(assetInfoManifest, buildResult);
                    }

                    //------ バージョンハッシュ情報をAssetInfoManifestに書き込み ------

                    using (new BuildLogScope(logBuilder, processTime, "AssetInfoManifest : SetAssetInfoHash"))
                    {
                        buildAssetBundle.SetAssetInfoHash(assetInfoManifest);
                    }

                    //------ 再度AssetInfoManifestだけビルドを実行 ------

                    using (new BuildLogScope(logBuilder, processTime, "Rebuild AssetInfoManifest"))
                    {
                        buildAssetBundle.BuildAssetInfoManifest();
                    }

                    //------ AssetInfoManifestファイルをパッケージ化 ------

                    using (new BuildLogScope(logBuilder, processTime, "BuildPackage AssetInfoManifest"))
                    {
                        await BuildAssetBundlePackage.BuildAssetInfoManifestPackage(exportPath, assetBundlePath, cryptoKey, cryptoIv);
                    }
                }

                versionHash = assetInfoManifest.VersionHash;

                //------ バージョンをファイル出力------

                GenerateVersionFile(exportPath, versionHash);

                //------ ログ出力------

                stopwatch.Stop();

                // ビルド情報.

                var buildLogText = new StringBuilder();

                var totalSeconds = stopwatch.Elapsed.TotalSeconds;

                buildLogText.AppendFormat("Build ExternalResource Complete. ({0:F2}sec)", totalSeconds).AppendLine();
                buildLogText.AppendLine();
                buildLogText.AppendFormat("VersionHash : {0}", versionHash).AppendLine();
                buildLogText.AppendLine();
                buildLogText.AppendLine(logBuilder.ToString());
                buildLogText.AppendLine();

                UnityConsole.Event(ExternalResources.ConsoleEventName, ExternalResources.ConsoleEventColor, buildLogText.ToString());

                //------ 出力先フォルダを開く------

                if (openExportFolder)
                {
                    UnityEditorUtility.OpenFolder(exportPath);
                }
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                throw;
            }
            finally
            {
                EditorApplication.UnlockReloadAssemblies();
            }

            return(versionHash);
        }