private static void CleanAllSonDir()
        {
            locAppId  = UnityEditorUtility.GetLocAppIdAtSelectDir();
            locU3DApp = ProjectInfoDati.GetActualInstance();
            if (string.IsNullOrEmpty(locAppId))
            {
                UnityEditorUtility.DisplayError("所选择的目录不是一个应用下的有效目录!");
                return;
            }
            var assetbundleEditor = AssetBundleEditorDati.GetActualInstance();

            if (assetbundleEditor == null)
            {
                UnityEditorUtility.DisplayError($"应用{locAppId}没有AssetBundle打包配置!");
                throw new Exception($"应用{locAppId}没有AssetBundle打包配置!");
            }
            foreach (var dir in AllSonDirs)
            {
                if (!dir.StartsWith(locU3DApp.ProjectAssetDatabaseDir))
                // dir.StartsWith(appHelper.StreamingAssetsDir))
                {
                    Debug.LogError($"目标目录不是一个有效的AssetBundle打包目录!");
                    continue;
                }
                assetbundleEditor.CleanBuildSettingAtDir(dir);
            }
            AssetBundleEditorDati.GetSingleDati().Save();
        }
        public AssetBundleBuildSizeInfo GetSizeInfo()
        {
            if (BuildType != AssetBundleBuildType.BuildAtSize)
            {
                throw new Exception($"目录{Dir}的打包方式不是尺寸打包!");
            }

            var dirId = IOUtility.GetSomeDirPath(Dir, 3);

            AtSizeSetting.SizeInfo = new AssetBundleBuildSizeInfo();
            var fileInfos = IOUtility.GetFileInfosAtDir(Dir, true)
                            .Where(d => AssetBundleBuilder.EndCheck(IgnoreRule, d.FullName))
                            .Where(d => AssetBundleBuilder.ContainCheck(IgnoreRule, d.FullName)).ToList();
            long maxSize     = AtSizeSetting.PackageSize * 1024;
            long currentSize = 0;
            var  bundleIndex = 0;
            var  paths       = new List <string>();

            foreach (var fileInfo in fileInfos)
            {
                var path = fileInfo.FullName;
                path         = path.ReplaceDoubleBackslash();
                currentSize += fileInfo.Length;
                if (fileInfo.Length >= maxSize)
                {
                    var assetId = Path.GetFileNameWithoutExtension(fileInfo.Name);
                    AtSizeSetting.SizeInfo.AppOutFile(path, assetId);
                    Debug.Log($"{fileInfo.Name} 文件大小大于尺寸分包的最大尺寸");
                    continue;
                }

                if (currentSize <= maxSize)
                {
                    paths.Add(path);
                }
                else
                {
                    AddSubPackage();
                    paths.Add(path);
                }
            }

            AddSubPackage(); // 添加最后一个子包尺寸小于分包限制的剩余资源
            AtSizeSetting.SetSizeTotal(AtSizeSetting.SizeInfo.GetAssetTotal());
            AssetBundleEditorDati.GetSingleDati().Save();
            return(AtSizeSetting.SizeInfo);

            void AddSubPackage()
            {
                var subBundleId = $"{dirId}_{bundleIndex}".ToLower();

                AtSizeSetting.SizeInfo.AddSubPacakage(subBundleId, paths);
                bundleIndex++;
                currentSize = 0;
                paths.Clear();
            }
        }
        /// <summary>
        /// 设置目标目录的打包类型
        /// </summary>
        /// <param name="buildType"></param>
        private static void SetTargetDirAtildType(AssetBundleBuildType buildType)
        {
            var projectInfo       = ProjectInfoDati.GetActualInstance();
            var assetbundleEditor = AssetBundleEditorDati.GetActualInstance();

            locAppId = projectInfo.DevelopProjectName;

            if (string.IsNullOrEmpty(locAppId))
            {
                UnityEditorUtility.DisplayError("未设置项目名称");
                return;
            }

            //appHelper = locU3DApp.Helper;

            if (assetbundleEditor == null)
            {
                Debug.LogError($"项目的 AssetBundleSetting 实例未创建 !");
                return;
            }

            var dirs = UnityEditorUtility.GetSelectDirs();

            foreach (var dir in dirs)
            {
                if (!dir.StartsWith(projectInfo.ProjectAssetDatabaseDir))
                {
                    Debug.LogError($"目标目录不是一个有效的AssetBundle打包目录!");
                    return;
                }
                assetbundleEditor.SetBuildAtTargetBuildType(dir, buildType);
                if (buildType == AssetBundleBuildType.BuildAtDirTree) // 清理所有子目录的打包配置
                {
                    var sonDirs = IOUtility.GetAllDir(dir, null, true, false);
                    foreach (var sonDir in sonDirs)
                    {
                        assetbundleEditor.CleanBuildSettingAtDir(sonDir);
                    }
                    Debug.Log($"目录{dir}已设置为目录树打包,其所有子目录的打包配置都已被清空!");
                }
            }
            AssetBundleEditorDati.GetSingleDati().Save();
        }
        private static void SetSonDirAtildType(AssetBundleBuildType buildType, List <string> dirs)
        {
            locAppId  = UnityEditorUtility.GetLocAppIdAtSelectDir();
            locU3DApp = ProjectInfoDati.GetActualInstance();
            if (string.IsNullOrEmpty(locAppId))
            {
                UnityEditorUtility.DisplayError("所选择的目录不是一个应用下的有效目录!");
                return;
            }

            foreach (var dir in dirs)
            {
                if (!dir.StartsWith(locU3DApp.ProjectAssetDatabaseDir))
                //&& dir.StartsWith(appHelper.StreamingAssetsDir))
                {
                    Debug.LogError($"目标目录不是一个有效的AssetBundle打包目录!");
                    continue;
                }

                var assetbundleEditor = AssetBundleEditorDati.GetActualInstance();
                if (assetbundleEditor == null)
                {
                    Debug.Log($"应用{locAppId}没有AssetBundle打包配置!");
                    return;
                }

                assetbundleEditor.SetBuildAtTargetBuildType(dir, buildType);
                if (buildType == AssetBundleBuildType.BuildAtDirTree) // 清理所有子目录的打包配置
                {
                    var sonDirs = IOUtility.GetAllDir(dir, null, true, false);
                    foreach (var sonDir in sonDirs)
                    {
                        assetbundleEditor.CleanBuildSettingAtDir(sonDir);
                    }
                    Debug.Log($"目录{dir}已设置为目录树打包,其所有子目录的打包配置都已被清空!");
                }
            }
            AssetBundleEditorDati.GetSingleDati().Save();
        }
        private static void CleanSelectDir()
        {
            var dirs      = UnityEditorUtility.GetSelectDirs();
            var targetApp = UnityEditorUtility.TryGetLocProjectInfoAtDir(dirs.First());

            if (targetApp == null)
            {
                UnityEditorUtility.DisplayError($"第一个目录必须是一个应用的允许打包目录!");
                return;
            }

            var assetbundleEditor = AssetBundleEditorDati.GetActualInstance();

            foreach (var dir in dirs)
            {
                //var dirSetting = appAssetBundleSetting.GetDirSetting(dir);
                //List<String> paths = null;
                //paths = dirSetting?.GetFullPathsAtDirTree();
                //assetbundleEditor.CleanBuildSettingAtDir(dir, paths);
            }
            AssetBundleEditorDati.GetSingleDati().Save();
        }
        private static void SetTargetDirsBundleIdAndSelectBuild(bool isBuild, bool isSaveSetting)
        {
            var selectDir = UnityEditorUtility.GetSelectDir();
            var targetApp = UnityEditorUtility.TryGetLocProjectInfoAtDir(selectDir);

            if (targetApp == null)
            {
                UnityEditorUtility.DisplayError("所选择的目录是一个无效目录,必须是一个应用允许打包的目录!");
                return;
            }

            var dirs = IOUtility.GetAllDir(selectDir);
            var appAssetBundleSetting = AssetBundleEditorDati.GetActualInstance();
            var dirSettings           = new List <AssetBundleBuildSetting>();

            foreach (var dir in dirs)
            {
                var setting = appAssetBundleSetting.GetSetting(dir);
                if (setting == null)
                {
                    continue;
                }

                dirSettings.Add(setting);
            }
            AssetBundleBuilder.currentAssetBundleInfo =
                AssetBundleBuilder.GetAppAssetBundleInfo(targetApp.DevelopProjectName);
            for (int i = 0; i < dirSettings.Count; i++)
            {
                EditorUtility.DisplayProgressBar("设置 AB 包名", $"正在设置 {dirSettings[i].Dir} 文件夹的 AB 包名", i * 1.0f / dirSettings.Count);
                AssetBundleBuilder.SetBundleIdAndSelectIsBuild(dirSettings[i],
                                                               isBuild, true, isSaveSetting);
            }
            EditorUtility.ClearProgressBar();
            AssetBundleBuilder.SaveCurrentAppAssetBundleInfo();

            AssetBundleEditorDati.GetSingleDati().Save();
        }
Exemple #7
0
        /// <summary>
        /// 设置给定配置下资源的AssetBundle名并选择是否打包。
        /// </summary>
        /// <param name="dirSetting">打包配置</param>
        /// <param name="isBuild">是否在设置完AssetBundle名后进行打包,默认进行打包。</param>
        /// <param name="isRefresh">是否在操作全部完毕后进行刷新,默认不刷新。</param>
        /// <param name="isSaveBundleSetting">是否在设置完AssetBundle名进行全局AssetBundle配置的保存,默认保存。</param>
        public static void SetBundleIdAndSelectIsBuild(AssetBundleBuildSetting dirSetting,
                                                       bool isBuild             = true,
                                                       bool isRefresh           = true,
                                                       bool isSaveBundleSetting = true)
        {
            switch (dirSetting.BuildType)
            {
            case AssetBundleBuildType.EveryBuild:
                SetBundleIdANdBuild_AtEveryFile(dirSetting, isBuild);
                break;

            case AssetBundleBuildType.BuildAtSize:
                SetBundleIdANdBuild_AtSize(dirSetting, isBuild);
                break;

            case AssetBundleBuildType.BuildAtDirSelf:
                SetBundleIdANdBuild_AtDirSelf(dirSetting, isBuild);
                break;

            case AssetBundleBuildType.BuildAtDirTree:
                SetBundleIdANdBuild_AtDirTree(dirSetting, isBuild);
                break;

            case AssetBundleBuildType.BuildAtList:
                break;
            }

            if (isSaveBundleSetting)
            {
                AssetBundleEditorDati.GetSingleDati().Save();
            }

            if (isRefresh)
            {
                AssetDatabase.Refresh();
            }
        }