コード例 #1
0
        public IEnumerator DoInitialize()
        {
            string packageManifestPath = AssetManager.PathProvider.GetPackageManifestPath();
            string json = null;

            if (packageManifestPath.StartsWith("jar") || packageManifestPath.StartsWith("http"))
            {
                UnityWebRequest webRequest = UnityWebRequest.Get(packageManifestPath);
                webRequest.useHttpContinue = false;
                yield return(webRequest.SendWebRequest());

                if (webRequest.result != UnityWebRequest.Result.ConnectionError && webRequest.result != UnityWebRequest.Result.ProtocolError)
                {
                    json = webRequest.downloadHandler.text;
                }
            }
            else
            {
                json = File.ReadAllText(packageManifestPath);
            }
            PackageManifest packageManifest = new PackageManifest();

            if (!string.IsNullOrEmpty(json))
            {
                JsonUtility.FromJsonOverwrite(json, packageManifestPath);
            }
            else
            {
                throw new System.Exception("load PackageManifest fail : " + packageManifestPath);
            }
            string      bundleManifestPath = AssetManager.PathProvider.GetAssetBundleManifestPath();
            AssetBundle assetBundle;

            if (bundleManifestPath.StartsWith("jar") || bundleManifestPath.StartsWith("http"))
            {
                UnityWebRequest webRequest = UnityWebRequestAssetBundle.GetAssetBundle(bundleManifestPath);
                webRequest.useHttpContinue = false;
                yield return(webRequest.SendWebRequest());

                assetBundle = DownloadHandlerAssetBundle.GetContent(webRequest);
            }
            else
            {
                var createRequest = AssetBundle.LoadFromFileAsync(bundleManifestPath);
                yield return(createRequest);

                assetBundle = createRequest.assetBundle;
            }
            if (assetBundle == null)
            {
                throw new System.Exception("load PackageManifest fail " + bundleManifestPath);
            }
            var manifest = assetBundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest");

            assetProvider.InitByManifest(manifest, packageManifest);
            assetBundle.Unload(true);
            isFinish      = true;
            assetProvider = null;
            Finished();
        }
コード例 #2
0
        public void InitByManifest(AssetBundleManifest assetBundleManifest, PackageManifest packageManifest)
        {
            assetLocations.Clear();
            this.bundles.Clear();
            mainBundles.Clear();
            foreach (var bundle in packageManifest.Bundles)
            {
                var mainBundle = new MainBundle
                {
                    Assets = bundle.Assets,
                    Name   = bundle.Name,
                    Hash   = assetBundleManifest.GetAssetBundleHash(bundle.Name)
                };

                mainBundles.Add(mainBundle);
                this.bundles.Add(bundle.Name, mainBundle);
            }

            foreach (var asset in packageManifest.Assets)
            {
                assetLocations.Add(asset.Name, asset.Location);
            }
            string[] bundles = assetBundleManifest.GetAllAssetBundles();
            foreach (var name in bundles)
            {
                if (!this.bundles.ContainsKey(name))
                {
                    AssetBundleInfo bundleInfo = new AssetBundleInfo
                    {
                        Name = name,
                        Hash = assetBundleManifest.GetAssetBundleHash(name)
                    };
                    this.bundles.Add(name, bundleInfo);
                }
            }
            foreach (var kv in this.bundles)
            {
                string[] depens = assetBundleManifest.GetAllDependencies(kv.Key);
                foreach (var dep in depens)
                {
                    if (this.bundles.TryGetValue(dep, out var info))
                    {
                        kv.Value.Dependencies.Add(info);
                    }
                }
            }
        }
コード例 #3
0
        public bool Build(string outPutPath, IDependenciesPacakageProvider provider, BuildTarget targetPlatform, bool clearRemovedBundle)
        {
            if (!Check())
            {
                return(false);
            }
            if (!outPutPath.EndsWith("/") || !outPutPath.EndsWith("\\"))
            {
                outPutPath += "/";
            }
            string bundlePath = outPutPath + "bundle/";

            List <string>           packageFiles = new List <string>();
            List <AssetBundleBuild> builds       = new List <AssetBundleBuild>();

            foreach (var package in Packages)
            {
                if (!package || !package.Enable)
                {
                    continue;
                }
                var filePaths          = package.AssetPaths;
                AssetBundleBuild build = new AssetBundleBuild
                {
                    assetBundleName = ToAssetBundlName(package.name),
                    assetNames      = filePaths.ToArray()
                };
                packageFiles.AddRange(filePaths);
                builds.Add(build);
            }
            HashSet <string> dependencies = new HashSet <string>();
            int step = 0;

            foreach (var file in packageFiles)
            {
                EditorUtility.DisplayProgressBar("资源依赖分析", file, (float)++step / packageFiles.Count);
                var depenFiles = AssetDatabase.GetDependencies(file, true);
                foreach (var dep in depenFiles)
                {
                    var extension = Path.GetExtension(dep).ToLower();

                    if (!string.IsNullOrEmpty(extension) && !NonePackFiles.Contains(extension))
                    {
                        dependencies.Add(dep);
                    }
                }
            }
            EditorUtility.ClearProgressBar();

            if (provider == null)
            {
                provider = new DefaultDependenciesPacakageProvider();
            }
            //依赖文件分包
            var depBundles = provider.FilesToPackage(dependencies);

            if (depBundles != null)
            {
                builds.AddRange(depBundles);
            }
            for (int i = 0; i < builds.Count; ++i)
            {
                string name = builds[i].assetBundleName;
                if (!name.EndsWith(BundleExtension))
                {
                    name += BundleExtension;
                    var info = builds[i];
                    info.assetBundleName = name;
                    builds[i]            = info;
                }
            }
            BuildAssetBundleOptions bundleOptions = BuildAssetBundleOptions.UncompressedAssetBundle;

            if (CompressedAssetBundle)
            {
                bundleOptions = bundleOptions | BuildAssetBundleOptions.ChunkBasedCompression;
            }
            var resutlt = BuildPipeline.BuildAssetBundles(bundlePath, builds.ToArray(), bundleOptions, targetPlatform);

            if (resutlt)
            {
                PackageManifest packageManifest = ToManifest();
                string          json            = JsonUtility.ToJson(packageManifest);
                File.WriteAllText(Path.Combine(bundlePath, "PackageManifest.json"), json, new System.Text.UTF8Encoding(false));
                if (clearRemovedBundle)
                {
                    HashSet <string> bundles = new HashSet <string>(resutlt.GetAllAssetBundlesWithVariant());
                    var files = Directory.GetFiles(bundlePath, "*.*", SearchOption.AllDirectories);
                    foreach (var file in files)
                    {
                        string fileName = Path.GetFileName(file);
                        if (!fileName.Contains(BundleExtension) || fileName.EndsWith(".meta") || fileName.EndsWith(".Manifest"))
                        {
                            continue;
                        }
                        string partName = fileName.Replace(bundlePath, "");
                        if (!bundles.Contains(partName))
                        {
                            File.Delete(file);
                        }
                    }
                }
            }
            return(resutlt != null);
        }