Exemple #1
0
        public static void BuildAssetBundles()
        {
            // Choose the output path according to the build target.
            var outputPath = CreateAssetBundleDirectory();
            const BuildAssetBundleOptions options = BuildAssetBundleOptions.ChunkBasedCompression;
            var targetPlatform      = EditorUserBuildSettings.activeBuildTarget;
            var rules               = GetBuildRules();
            var builds              = rules.GetBuilds();
            var assetBundleManifest = BuildPipeline.BuildAssetBundles(outputPath, builds, options, targetPlatform);

            if (assetBundleManifest == null)
            {
                return;
            }

            var manifest   = GetManifest();
            var dirs       = new List <string>();
            var assets     = new List <AssetRef>();
            var bundles    = assetBundleManifest.GetAllAssetBundles();
            var bundle2Ids = new Dictionary <string, int>();

            for (var index = 0; index < bundles.Length; index++)
            {
                var bundle = bundles[index];
                bundle2Ids[bundle] = index;
            }

            var bundleRefs = new List <BundleRef>();

            for (var index = 0; index < bundles.Length; index++)
            {
                var bundle = bundles[index];
                var deps   = assetBundleManifest.GetAllDependencies(bundle);
                var path   = string.Format("{0}/{1}", outputPath, bundle);
                if (File.Exists(path))
                {
                    using (var stream = File.OpenRead(path))
                    {
                        bundleRefs.Add(new BundleRef
                        {
                            name = bundle,
                            id   = index,
                            deps = Array.ConvertAll(deps, input => bundle2Ids[input]),
                            len  = stream.Length,
                            hash = assetBundleManifest.GetAssetBundleHash(bundle).ToString(),
                        });
                    }
                }
                else
                {
                    Debug.LogError(path + " file not exsit.");
                }
            }

            for (var i = 0; i < rules.ruleAssets.Length; i++)
            {
                var item  = rules.ruleAssets[i];
                var path  = item.path;
                var dir   = Path.GetDirectoryName(path);
                var index = dirs.FindIndex(o => o.Equals(dir));
                if (index == -1)
                {
                    index = dirs.Count;
                    dirs.Add(dir);
                }

                var asset = new AssetRef {
                    bundle = bundle2Ids[item.bundle], dir = index, name = Path.GetFileName(path)
                };
                assets.Add(asset);
            }

            manifest.searchDirs = Array.ConvertAll(rules.rules, input => input.searchPath);
            manifest.dirs       = dirs.ToArray();
            manifest.assets     = assets.ToArray();
            manifest.bundles    = bundleRefs.ToArray();
            EditorUtility.SetDirty(manifest);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            var version     = 0;
            var versionFile = string.Format("{0}/{1}", outputPath, Versions.BuildVersion);

            if (File.Exists(versionFile))
            {
                version = int.Parse(File.ReadAllText(versionFile));
                version = version + 1;
            }

            File.WriteAllText(versionFile, version.ToString());
            var manifestBundleName = "manifest.unity3d";

            builds = new[]
            {
                new AssetBundleBuild
                {
                    assetNames      = new[] { AssetDatabase.GetAssetPath(manifest), },
                    assetBundleName = manifestBundleName
                }
            };

            BuildPipeline.BuildAssetBundles(outputPath, builds, options, targetPlatform);
            ArrayUtility.Add(ref bundles, manifestBundleName);
            ArrayUtility.Add(ref bundles, Versions.BuildVersion);

            if (Directory.Exists(rules.outputPath))
            {
                var files = Directory.GetFiles(rules.outputPath);
                foreach (var file in files)
                {
                    File.Delete(file);
                }
            }
            else
            {
                Directory.CreateDirectory(rules.outputPath);
            }

            foreach (var item in bundles)
            {
                var sourceFileName = string.Format("{0}/{1}", outputPath, item);
                var destFileName   = string.Format("{0}/{1}", rules.outputPath, item);
                File.Copy(sourceFileName, destFileName, true);
            }

            Versions.MakeRecords(rules.outputPath);
        }