public override void BeforeBuild()
        {
            base.BeforeBuild();

            // Clear altas settings
            var resPath = Application.dataPath + "/" + ResConfig.UI_TEXTURE;
            var files   = ResBuildUtility.GetFiles(resPath);

            foreach (var file in files)
            {
                if (file.EndsWith(".png") == false && file
                    .EndsWith(".jpg") == false)
                {
                    continue;
                }

                var assetPath = "Assets/" + file.ReplaceFirst(Application.dataPath + "/", "");
                if (assetPath.StartsWith("Assets/" + ResConfig.UI_TEXTURE_RUNTIME))
                {
                    continue;
                }

                var textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
                ResTextureAutoConfigurer.ConfigureUITexture(textureImporter, true);
            }
        }
        public static void Build(BuildTarget targetPlatform)
        {
            BeforeBuild(targetPlatform);

            // Build
            var resPath = Application.dataPath + "/" + ResConfig.RES_ROOT;
            var files   = ResBuildUtility.GetFiles(resPath);

            BuildBundles(targetPlatform, files);

            AfterBuild();
        }
        public override void OverrideBuild(string outpathRoot, List <AssetBundleBuild> filteredBuildInfos)
        {
            for (int i = filteredBuildInfos.Count - 1; i >= 0; --i)
            {
                var buildInfo = filteredBuildInfos[i];
                // Remove runtime textures from buildInfos
                if (buildInfo.assetBundleName.StartsWith("Assets/" + ResConfig.UI_TEXTURE_RUNTIME) ||
                    buildInfo.assetBundleName.StartsWith(ResConfig.DEPENDENCIES_ROOT + "/" + ResConfig.UI_TEXTURE_RUNTIME))
                {
                    filteredBuildInfos.RemoveAt(i);
                }
            }

            var resPath = Application.dataPath + "/" + ResConfig.UI_TEXTURE_RUNTIME;
            var files   = ResBuildUtility.GetFiles(resPath);

            foreach (var file in files)
            {
                if (file.EndsWith(".png") == false &&
                    file.EndsWith(".jpg") == false)
                {
                    continue;
                }

                var fileName        = Path.GetFileName(file);
                var assetBundleName = file.ReplaceFirst(_rootPath + "/", "");
                assetBundleName = assetBundleName.ReplaceLast("/" + fileName, "").ReplaceFirst("Assets/", "") + ResConfig.BUNDLE_EXTENSION;
                var assetName = file.ReplaceFirst(_rootPath + "/", "");

                var index = ResBuildUtility.TryGetBuildInfo(filteredBuildInfos, assetBundleName);
                if (index == -1)
                {
                    var buildInfo = new AssetBundleBuild();
                    buildInfo.assetNames      = new string[] { assetName };
                    buildInfo.assetBundleName = assetBundleName;
                    filteredBuildInfos.Add(buildInfo);
                }
                else
                {
                    var buildInfo  = filteredBuildInfos[index];
                    var assetNames = buildInfo.assetNames.ToList();
                    if (assetNames.Contains(assetName))
                    {
                        continue;
                    }

                    assetNames.Add(assetName);
                    buildInfo.assetNames      = assetNames.ToArray();
                    filteredBuildInfos[index] = buildInfo;
                }
            }
        }
        static void BeforeBuild(BuildTarget targetPlatform)
        {
            EditorUtility.DisplayProgressBar(
                "Building AssetBundles",
                string.Format("Starting building..."),
                0);

            _target = targetPlatform;

            if (Directory.Exists(_outPath) == false)
            {
                Directory.CreateDirectory(_outPath);
            }
            ResBuildUtility.ClearAssetBundleConfigurations();
        }
        public static void Build(BuildTarget targetPlatform)
        {
            BeforeBuild(targetPlatform);

            // Build bundles first
            var resPath = Application.dataPath + "/" + ResConfig.RES_ROOT;
            var files   = ResBuildUtility.GetFiles(resPath);

            BuildBundles(targetPlatform, files);

            // Build version file
            var bundlesPath = _outPath;

            files = ResBuildUtility.GetFiles(bundlesPath);
            BuildVersionFile(targetPlatform, files);
        }
        // BuildAssetBundleOptions.UncompressedAssetBundle;

        public static void Clear()
        {
            EditorUtility.DisplayProgressBar(
                "Clear AssetBundles",
                string.Format("Removing..."),
                1f);
            if (Directory.Exists(Application.streamingAssetsPath))
            {
                Directory.Delete(Application.streamingAssetsPath, true);
                Directory.CreateDirectory(Application.streamingAssetsPath);
            }
            EditorUtility.ClearProgressBar();

            ResBuildUtility.ClearAssetBundleConfigurations();
            AssetDatabase.Refresh();
        }
        static ResVersionFile BuildVersionFile(string[] files)
        {
            var versionFile = new ResVersionFile(files.Length);

            for (var i = 0; i < files.Length; ++i)
            {
                var file        = files[i];
                var versionInfo = new ResVersionInfo();
                versionInfo.File = file.ReplaceFirst(_outPath + "/", "");
                versionInfo.MD5  = MD5Utility.GetFileMD5(file);

                versionFile.Files[i] = file.ReplaceFirst(_outPath + "/", "");
                versionFile.Infos[i] = versionInfo;
            }

            var filePath = Application.dataPath + "/" + ResConfig.RES_ROOT + "/" + ResConfig.VERSION_FILE;

            FileUtility.WriteFile(filePath, versionFile.Serialize());
            AssetDatabase.Refresh();

            var buildInfo = new AssetBundleBuild();

            buildInfo.assetBundleName = ResConfig.VERSION_FILE;
            var assetName = "Assets/" + ResConfig.RES_ROOT + "/" + ResConfig.VERSION_FILE;

            buildInfo.assetNames = new string[] { assetName };
            ResBuildUtility.ResetBundleName(ref buildInfo);

            var options = _options & (~BuildAssetBundleOptions.ForceRebuildAssetBundle);

            BuildPipeline.BuildAssetBundles(
                _outPath,
                // new AssetBundleBuild[] { buildInfo },
                options,
                _target
                );

            // Remove temp versionFile
            File.Delete(filePath);

            return(versionFile);
        }
        static void BuildBundles(BuildTarget targetPlatform, string[] files)
        {
            var buildInfos = new Dictionary <string, AssetBundleBuild>();

            var filters = new List <ResAbstractBuildFilter>()
            {
                new ResPrefabBuildFilter(),
                new ResLuaBuildFilter(),
            };

            // BeforeBuild
            foreach (var filter in filters)
            {
                filter.BeforeBuild();
            }

            // Prepare build
            foreach (var file in files)
            {
                foreach (var filter in filters)
                {
                    if (filter.Filtered(file))
                    {
                        AssetBundleBuild?buildInfoNullable = filter.PrepareBuild(file);
                        if (buildInfoNullable != null)
                        {
                            var buildInfo = (AssetBundleBuild)buildInfoNullable;
                            buildInfos[file] = buildInfo;
                        }

                        if (filter.BlockOthers)
                        {
                            break;
                        }
                    }
                }
            }
            AssetDatabase.Refresh();

            try
            {
                var filteredBuildInfos = new List <AssetBundleBuild>();

                // Real build
                var overrideFilters = new HashSet <ResAbstractBuildFilter>();
                foreach (var pair in buildInfos)
                {
                    var filepath  = pair.Key;
                    var buildInfo = pair.Value;

                    var overrided = false;
                    foreach (var filter in filters)
                    {
                        overrided |= filter.OverrideBuild(filepath, buildInfo);
                        if (overrided)
                        {
                            overrideFilters.Add(filter);
                            if (filter.BlockOthers)
                            {
                                break;
                            }
                        }
                    }

                    if (overrided == false)
                    {
                        // Add dependent buildInfos
                        ResBuildUtility.AppendDependencies(buildInfo, filteredBuildInfos);

                        // Add self buildInfo
                        filteredBuildInfos.Add(buildInfo);
                    }
                }

                // Build override filters first
                foreach (var filter in overrideFilters)
                {
                    filter.DoOverrideBuild(_outPath, filteredBuildInfos);
                }

                // Build
                var options = BuildAssetBundleOptions.DeterministicAssetBundle;
                BuildPipeline.BuildAssetBundles(
                    _outPath,
                    filteredBuildInfos.ToArray(),
                    options,
                    targetPlatform
                    );
            }
            catch (Exception e)
            {
                LogManager.Error(e.Message);
            }
            finally
            {
                // After build
                foreach (var filter in filters)
                {
                    filter.AfterBuild();
                }
                AssetDatabase.RemoveUnusedAssetBundleNames();
                AssetDatabase.Refresh();
            }
        }
        static void BuildBundles(BuildTarget targetPlatform, string[] files)
        {
            var buildInfos = new Dictionary <string, AssetBundleBuild>();

            var filters = new List <ResAbstractBuildFilter>()
            {
                new ResLuaBuildFilter(),
                new ResSpriteBuildFilter(),

                // Assets contain dependence
                new ResPrefabBuildFilter(),
            };

            // 10
            var basePercent = 0f;
            var percent     = 10f;
            var index       = 0f;
            var count       = filters.Count;

            // BeforeBuild
            EditorUtility.DisplayProgressBar(
                "Building AssetBundles",
                string.Format("Before building..."),
                ((1) * percent + basePercent) / 100);
            foreach (var filter in filters)
            {
                filter.BeforeBuild();
                index++;
            }

            // 30
            basePercent = basePercent + percent;
            percent     = 20f;
            index       = 0;
            count       = files.Length * filters.Count;

            // Prepare build
            EditorUtility.DisplayProgressBar(
                "Building AssetBundles",
                string.Format("Preparing..."),
                ((1) * percent + basePercent) / 100);
            foreach (var file in files)
            {
                foreach (var filter in filters)
                {
                    index++;

                    if (filter.Filtered(file))
                    {
                        AssetBundleBuild?buildInfoNullable = filter.PrepareBuild(file);
                        if (buildInfoNullable != null)
                        {
                            var buildInfo = (AssetBundleBuild)buildInfoNullable;
                            buildInfos[file] = buildInfo;
                        }

                        if (filter.BlockOthers)
                        {
                            break;
                        }
                    }
                }
            }

            // 50
            basePercent = basePercent + percent;
            percent     = 20f;
            index       = 0;
            count       = files.Length * buildInfos.Count;

            try
            {
                var filteredBuildInfos = new List <AssetBundleBuild>();

                // Real build
                foreach (var pair in buildInfos)
                {
                    var filepath  = pair.Key;
                    var buildInfo = pair.Value;
                    {
                        // Add dependent buildInfos
                        ResBuildUtility.AppendDependencies(buildInfo, filteredBuildInfos);

                        // Add self buildInfo
                        filteredBuildInfos.Add(buildInfo);
                    }
                }

                // 60
                basePercent = basePercent + percent;
                percent     = 10f;
                index       = 0;
                count       = filters.Count;

                // Additive build
                foreach (var filter in filters)
                {
                    EditorUtility.DisplayProgressBar(
                        "Building AssetBundles",
                        string.Format(filter.GetType().Name + " override building..."),
                        ((index / count) * percent + basePercent) / 100);
                    index++;

                    filter.OverrideBuild(_outPath, filteredBuildInfos);
                }

                // 80
                basePercent = basePercent + percent;
                percent     = 20f;
                index       = 0;
                count       = filteredBuildInfos.Count;

                // Set bundle name
                EditorUtility.DisplayProgressBar(
                    "Building AssetBundles",
                    string.Format("Setting bundleNames..."),
                    ((1) * percent + basePercent) / 100);
                for (var i = 0; i < filteredBuildInfos.Count; ++i)
                {
                    index++;

                    var buildInfo = filteredBuildInfos[i];
                    ResBuildUtility.ResetBundleName(ref buildInfo);
                    filteredBuildInfos[i] = buildInfo;
                }

                // Build
                BuildPipeline.BuildAssetBundles(
                    _outPath,
                    // filteredBuildInfos.ToArray(),
                    _options,
                    targetPlatform
                    );

                // 100
                basePercent = basePercent + percent;
                percent     = 20f;
                index       = 1;
                count       = 1;

                // Build version file
                EditorUtility.DisplayProgressBar(
                    "Building AssetBundles",
                    string.Format("Building version file..."),
                    ((index / count) * percent + basePercent) / 100);

                var bundlesPath = _outPath;
                files = ResBuildUtility.GetFiles(bundlesPath);
                BuildVersionFile(files);
            }
            catch (Exception e)
            {
                LogManager.Error(e.Message);
            }
            finally
            {
                // After build
                foreach (var filter in filters)
                {
                    filter.AfterBuild();
                }
            }
        }
Beispiel #10
0
        public override void OverrideBuild(string outpathRoot, List <AssetBundleBuild> filteredBuildInfos)
        {
            for (int i = filteredBuildInfos.Count - 1; i >= 0; --i)
            {
                var buildInfo = filteredBuildInfos[i];
                if (buildInfo.assetBundleName.StartsWith("Assets/" + ResConfig.RES_ROOT + "/" + ResConfig.LUA_ROOT) ||
                    buildInfo.assetBundleName.StartsWith(ResConfig.DEPENDENCIES_ROOT + "/" + ResConfig.RES_ROOT + "/" + ResConfig.LUA_ROOT))
                {
                    filteredBuildInfos.RemoveAt(i);
                }
            }

            var resPath = Application.dataPath + "/" + ResConfig.RES_ROOT + "/" + ResConfig.LUA_ROOT;
            var files   = ResBuildUtility.GetFiles(resPath);

            _tmpFiles.Clear();

            foreach (var file in files)
            {
                if (file.EndsWith(ResConfig.LUA_EXTENSION) == false)
                {
                    continue;
                }

                var sourceFile = file;
                var targetFile = sourceFile.ReplaceLast(ResConfig.LUA_EXTENSION, ResConfig.MOBILE_LUA_EXTENSION);

                if (File.Exists(targetFile))
                {
                    File.Delete(targetFile);
                }
                File.Copy(sourceFile, targetFile);
                _tmpFiles.Add(targetFile);

                var assetBundleName = targetFile.ReplaceFirst(Application.dataPath + "/" + ResConfig.RES_ROOT + "/", "") + ResConfig.BUNDLE_EXTENSION;
                var assetName       = targetFile.ReplaceFirst(_rootPath + "/", "");

                var index = ResBuildUtility.TryGetBuildInfo(filteredBuildInfos, assetBundleName);
                if (index == -1)
                {
                    var buildInfo = new AssetBundleBuild();
                    buildInfo.assetNames      = new string[] { assetName };
                    buildInfo.assetBundleName = assetBundleName;
                    filteredBuildInfos.Add(buildInfo);

                    LogManager.Log("lua assetbundle " + assetBundleName);
                }
                else
                {
                    var buildInfo  = filteredBuildInfos[index];
                    var assetNames = buildInfo.assetNames.ToList();
                    if (assetNames.Contains(assetName))
                    {
                        continue;
                    }

                    assetNames.Add(assetName);
                    buildInfo.assetNames      = assetNames.ToArray();
                    filteredBuildInfos[index] = buildInfo;

                    LogManager.Log("lua assetbundle " + assetBundleName);
                }
            }
            AssetDatabase.Refresh();
        }