Esempio n. 1
0
        private static int AddBundle(string path, AssetRef asset, ref List <BundleRef> bundles)
        {
            var bundleName = path.Replace("Assets/", "");
            var destFile   = Path.Combine(outputPath, bundleName);
            var destDir    = Path.GetDirectoryName(destFile);

            if (!Directory.Exists(destDir) && !string.IsNullOrEmpty(destDir))
            {
                Directory.CreateDirectory(destDir);
            }
            File.Copy(path, destFile, true);
            using (var stream = File.OpenRead(destFile))
            {
                var bundle = new BundleRef
                {
                    name = bundleName,
                    id   = bundles.Count,
                    len  = stream.Length,
                    crc  = Utility.GetCRC32Hash(stream),
                    hash = string.Empty
                };
                asset.bundle = bundles.Count;
                bundles.Add(bundle);
            }
            return(asset.bundle);
        }
Esempio n. 2
0
        public void Deserialize(BinaryReader reader)
        {
            ver = reader.ReadString();
            var count = reader.ReadInt32();

            dirs = new string[count];
            for (var i = 0; i < count; i++)
            {
                dirs[i] = reader.ReadString();
            }

            count          = reader.ReadInt32();
            activeVariants = new string[count];
            for (var i = 0; i < count; i++)
            {
                activeVariants[i] = reader.ReadString();
            }

            count = reader.ReadInt32();
            for (var i = 0; i < count; i++)
            {
                var file = new AssetRef();
                file.Deserialize(reader);
                assets.Add(file);
            }

            count = reader.ReadInt32();
            for (var i = 0; i < count; i++)
            {
                var file = new BundleRef();
                file.Deserialize(reader);
                file.id = bundles.Count;
                bundles.Add(file);
                _bundles[file.name] = file;
            }

            count = reader.ReadInt32();
            for (var i = 0; i < count; i++)
            {
                var patch = new Patch();
                patch.Deserialize(reader);
                patches.Add(patch);
                _patches[patch.name] = patch;
            }

            count = reader.ReadInt32();
            for (var i = 0; i < count; i++)
            {
                var patch = reader.ReadString();
                patchesInBuild.Add(patch);
            }
        }
Esempio n. 3
0
        public static void BuildManifest()
        {
            AssetDatabase.RemoveUnusedAssetBundleNames();
            var             bundles  = AssetDatabase.GetAllAssetBundleNames();
            var             manifest = GetManifest();
            List <string>   dirs     = new List <string>();
            List <AssetRef> assets   = new List <AssetRef>();

            for (int i = 0; i < bundles.Length; i++)
            {
                var paths = AssetDatabase.GetAssetPathsFromAssetBundle(bundles[i]);
                foreach (var path in paths)
                {
                    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();
                    asset.bundle = i;
                    asset.dir    = index;
                    asset.name   = Path.GetFileName(path);

                    assets.Add(asset);
                }
            }

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

            var assetPath  = AssetDatabase.GetAssetPath(manifest);
            var bundleName = Path.GetFileNameWithoutExtension(assetPath).ToLower();

            SetAssetBundleNameAndVariant(assetPath, bundleName, null);

            EditorUtility.SetDirty(manifest);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
Esempio n. 4
0
        private static void BuildVersions(AssetBundleManifest manifest, BuildRules rules)
        {
            var allBundles = manifest.GetAllAssetBundles();
            var bundle2Ids = GetBundle2Ids(allBundles);
            var bundles    = GetBundles(manifest, allBundles, bundle2Ids);
            var ver        = rules.AddVersion();

            var dirs          = new List <string>();
            var assets        = new List <AssetRef>();
            var patches       = new List <Patch>();
            var asset2Bundles = new Dictionary <string, BundleRef>();

            foreach (var item in rules.assets)
            {
                var path = item.name;
                var dir  = Path.GetDirectoryName(path);
                if (!string.IsNullOrEmpty(dir))
                {
                    dir = dir.Replace("\\", "/");
                }
                var index = dirs.FindIndex(o => o.Equals(dir));
                if (index == -1)
                {
                    index = dirs.Count;
                    dirs.Add(dir);
                }
                var asset = new AssetRef();
                if (item.groupBy == GroupBy.None)
                {
                    var id = AddBundle(path, asset, ref bundles);
                    asset.bundle = id;
                }
                else
                {
                    bundle2Ids.TryGetValue(item.bundle, out asset.bundle);
                }
                asset2Bundles[path] = bundles[asset.bundle];
                asset.dir           = index;
                asset.name          = Path.GetFileName(path);
                assets.Add(asset);
            }

            Func <List <string>, List <int> > getFiles = delegate(List <string> list)
            {
                var ret = new List <int>();
                foreach (var file in list)
                {
                    BundleRef bundle;
                    asset2Bundles.TryGetValue(file, out bundle);
                    if (bundle != null)
                    {
                        if (!ret.Contains(bundle.id))
                        {
                            ret.Add(bundle.id);
                        }
                        foreach (var child in bundle.children)
                        {
                            if (!ret.Contains(child))
                            {
                                ret.Add(child);
                            }
                        }
                    }
                    else
                    {
                        Debug.LogWarning("bundle == nil, file:" + file);
                    }
                }
                return(ret);
            };

            for (var i = 0; i < rules.patches.Count; i++)
            {
                var item = rules.patches[i];
                patches.Add(new Patch
                {
                    name  = item.name,
                    files = getFiles(item.assets),
                });
            }

            var versions = new Versions();

            versions.activeVariants = manifest.GetAllAssetBundlesWithVariant();
            versions.dirs           = dirs.ToArray();
            versions.assets         = assets;
            versions.bundles        = bundles;
            versions.patches        = patches;
            versions.ver            = ver;

            if (rules.allAssetsToBuild)
            {
                bundles.ForEach(obj => obj.location = 1);
            }
            else
            {
                foreach (var patchName in rules.patchesInBuild)
                {
                    var patch = versions.patches.Find((Patch item) => { return(item.name.Equals(patchName)); });
                    if (patch != null)
                    {
                        foreach (var file in patch.files)
                        {
                            if (file >= 0 && file < bundles.Count)
                            {
                                bundles[file].location = 1;
                            }
                        }
                    }
                }
            }

            versions.Save(outputPath + "/" + Assets.Versions);
        }
Esempio n. 5
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).Replace("\\", "/");
                var index = dirs.FindIndex(o => o.Equals(dir));
                if (index == -1)
                {
                    index = dirs.Count;
                    dirs.Add(dir);
                }

                try
                {
                    var asset = new AssetRef
                    {
                        bundle = bundle2Ids[item.bundle], dir = index, name = Path.GetFileName(path)
                    };
                    assets.Add(asset);
                }
                catch (KeyNotFoundException)
                {
                    Debug.LogError($"{Path.GetFileName(path)}资源无法打进AB包");
                }
            }

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

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

            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);

            Versions.BuildVersions(outputPath, bundles, GetBuildRules().AddVersion());
        }
        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());
        }
Esempio n. 7
0
        private static void OnInitialize()
        {
            var sceneAssets = new List <string>();
            var rules       = BuildScript.GetBuildRules();

            foreach (var guid in AssetDatabase.FindAssets("t:Scene", new[] { "Assets" }))
            {
                var assetPath = AssetDatabase.GUIDToAssetPath(guid);
                sceneAssets.Add(assetPath);
            }

            var patches     = new List <Patch>();
            var assets      = new List <AssetRef>();
            var searchPaths = new List <string>();
            var dirs        = new Dictionary <string, int>();

            foreach (var asset in rules.assets)
            {
                if (!File.Exists(asset.name))
                {
                    continue;
                }
                var dir = Path.GetDirectoryName(asset.name);
                if (!string.IsNullOrEmpty(dir))
                {
                    if (!searchPaths.Contains(dir))
                    {
                        dirs[dir] = searchPaths.Count;
                        searchPaths.Add(dir);
                    }
                }
                var ar = new AssetRef {
                    name = Path.GetFileName(asset.name), bundle = -1, dir = dirs[dir]
                };
                assets.Add(ar);
            }

            var scenes = new EditorBuildSettingsScene[sceneAssets.Count];

            for (var index = 0; index < sceneAssets.Count; index++)
            {
                var asset = sceneAssets[index];
                scenes[index] = new EditorBuildSettingsScene(asset, true);
                var dir = Path.GetDirectoryName(asset);
                if (!searchPaths.Contains(dir))
                {
                    searchPaths.Add(dir);
                }
            }

            for (var i = 0; i < rules.patches.Count; i++)
            {
                var item  = rules.patches[i];
                var patch = new Patch();
                patch.name = item.name;
                patches.Add(patch);
            }

            var developVersions = new Versions();

            developVersions.dirs    = searchPaths.ToArray();
            developVersions.assets  = assets;
            developVersions.patches = patches;

            Assets.basePath         = Environment.CurrentDirectory.Replace("\\", "/") + "/" + BuildScript.outputPath + "/";
            Assets.assetLoader      = AssetDatabase.LoadAssetAtPath;
            Assets.versionsLoader  += () => developVersions;
            Assets.onAssetLoaded   += rules.OnLoadAsset;
            Assets.onAssetUnloaded += rules.OnUnloadAsset;
            rules.BeginSample();
            EditorBuildSettings.scenes              = scenes;
            EditorApplication.playModeStateChanged += EditorApplicationOnplayModeStateChanged;
        }
Esempio n. 8
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);
        }