コード例 #1
0
        private static void BuildManifest(AssetBundleManifest assetBundleManifest, string bundleDir, BuildRules rules)
        {
            var manifest        = GetManifest();
            var allAssetBundles = assetBundleManifest.GetAllAssetBundles();
            var bundle2Ids      = GetBundle2Ids(allAssetBundles);
            var bundles         = GetBundles(assetBundleManifest, bundleDir, allAssetBundles, bundle2Ids);
            var dirs            = new List <string>();
            var assets          = new List <AssetRef>();
            var patches         = new List <VPatch>();

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

                var asset = new AssetRef();
                if (!bundle2Ids.TryGetValue(item.bundle, out asset.bundle))
                {
                    // 第三方资源
                    var bundle = new BundleRef();
                    bundle.id   = bundles.Count;
                    bundle.name = Path.GetFileName(path);
                    using (var stream = File.OpenRead(path))
                    {
                        bundle.len = stream.Length;
                        bundle.crc = Utility.GetCRC32Hash(stream);
                    }

                    bundles.Add(bundle);
                    asset.bundle = bundle.id;
                }

                asset.dir  = index;
                asset.name = Path.GetFileName(path);
                assets.Add(asset);
                var patch = patches.Find(pr => pr.@by == item.patch);
                if (patch == null)
                {
                    patch = new VPatch()
                    {
                        @by = item.patch
                    };
                    patches.Add(patch);
                }
                if (asset.bundle != -1)
                {
                    if (!patch.files.Contains(asset.bundle))
                    {
                        patch.files.Add(asset.bundle);
                    }
                    var bundle = bundles[asset.bundle];
                    foreach (var child in bundle.children)
                    {
                        if (!patch.files.Contains(child))
                        {
                            patch.files.Add(child);
                        }
                    }
                }
            }

            manifest.dirs    = dirs.ToArray();
            manifest.assets  = assets.ToArray();
            manifest.bundles = bundles.ToArray();

            EditorUtility.SetDirty(manifest);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

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

            var targetPlatform = EditorUserBuildSettings.activeBuildTarget;

            BuildPipeline.BuildAssetBundles(bundleDir, builds, rules.buildBundleOptions, targetPlatform);
            {
                var path   = bundleDir + "/" + manifestBundleName;
                var bundle = new BundleRef();
                bundle.id   = bundles.Count;
                bundle.name = Path.GetFileName(path);
                using (var stream = File.OpenRead(path))
                {
                    bundle.len = stream.Length;
                    bundle.crc = Utility.GetCRC32Hash(stream);
                }
                var patch = patches.Find(pr => pr.@by == PatchBy.Level0);
                if (patch == null)
                {
                    patch = new VPatch()
                    {
                        @by = PatchBy.Level0
                    };
                    patches.Add(patch);
                }
                bundles.Add(bundle);
            }
            Versions.BuildVersion(bundleDir, bundles, patches, GetBuildRules().AddVersion());
        }