/// <summary> /// 加载Bundle,会加载该Bundle的依赖 /// </summary> /// <param name="assetBundleName"></param> private void LoadBundle(AssetBundleInfo bundleInfo) { //加载自己 if (!this.CachedAssetBundleDictionary.ContainsKey(bundleInfo.AssetBundleName) || this.CachedAssetBundleDictionary[bundleInfo.AssetBundleName] == null) { string url = FilePathTools.persistentDataPath_Platform + "/" + bundleInfo.AssetBundleName; AssetBundle ab = AssetUtils.LoadLocalAssetBundle(url); if (ab != null) { CachedAssetBundle cab = new CachedAssetBundle(ab, bundleInfo, this); this.CachedAssetBundleDictionary[bundleInfo.AssetBundleName] = cab; } else { return; } } //加载依赖 string[] dependencies = bundleInfo.DependenciesBundleNames; foreach (string fileName in dependencies) { LoadBundle(fileName); } }
private void LoadBundle(string assetBundleName) { AssetBundleInfo abInfo = AssetBundleManager.Instance.GetAssetBundleInfo(assetBundleName); LoadBundle(abInfo); }
public CachedAssetBundle(AssetBundle assetBundle, AssetBundleInfo bundleInfo, CachedAssetBundleContainer _parent) { this.AssetBundleName = assetBundle.name; this.assetBundle = assetBundle; this.bundleInfo = bundleInfo; }
/// <summary> /// 打Group的Bundle,并生成Group的version信息 /// </summary> /// <param name="singleGroup"></param> public static void BuildGroup(BundleGroup singleGroup, string exportPath, BuildTarget buildTarget = BuildTarget.NoTarget) { List <string> paths = singleGroup.GetBundlePaths(GetBundleMethod.All); if (paths == null || paths.Count <= 0) { return; } string groupName = singleGroup.GroupName; if (!Directory.Exists(exportPath)) { Directory.CreateDirectory(exportPath); } List <AssetBundleBuild> buildMap = new List <AssetBundleBuild>(); //打AssetBundle包 for (int i = 0; i < paths.Count; i++) { string fullPath = string.Format("{0}/{1}/{2}", Application.dataPath, "Export", paths[i]); AssetBundleBuild build = new AssetBundleBuild(); List <string> assetnames = new List <string>(); if (File.Exists(fullPath)) { string itemPath = paths[i]; int lastDot = itemPath.LastIndexOf("."); if (lastDot > 0) { build.assetBundleName = itemPath.Substring(0, lastDot); } else { build.assetBundleName = itemPath; } assetnames.Add(FilePathTools.GetRelativePath(fullPath)); } else { build.assetBundleName = paths[i]; FileInfo[] files = FileUtil.GetFiles(fullPath); if (files.Length <= 0) { continue; } for (int j = 0; j < files.Length; j++) { //Debug.Log(files[j]); string path = FilePathTools.GetRelativePath(files[j].FullName); if (path.Contains(".DS_Store") || path.Contains(".gitkeep")) { continue; } assetnames.Add(path); } } foreach (string assetName in assetnames) { AssetImporter.GetAtPath(assetName).SetAssetBundleNameAndVariant(build.assetBundleName, string.Empty); } build.assetNames = assetnames.ToArray(); buildMap.Add(build); } AssetBundleManifest assetBundleManifest = BuildPipeline.BuildAssetBundles( exportPath, buildMap.ToArray(), BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.DeterministicAssetBundle, buildTarget == BuildTarget.NoTarget ? EditorUserBuildSettings.activeBuildTarget : buildTarget ); //生成Version Item Info AssetBundleGroupInfo assetBundleGroupInfo = new AssetBundleGroupInfo(singleGroup.GroupName, singleGroup.BaseGroup); string[] builtAssetBundleNames = assetBundleManifest.GetAllAssetBundles(); for (var i = 0; i < builtAssetBundleNames.Length; ++i) { string assetBundlePath = string.Format("{0}/{1}", exportPath, builtAssetBundleNames[i]); string path = Directory.GetCurrentDirectory(); BundleInfo bundleInfo = singleGroup.GetBundleInfo(builtAssetBundleNames[i]); string md5 = AssetUtils.BuildFileMd5(assetBundlePath); int size = AssetUtils.FileSize(assetBundlePath); AssetBundleInfo localAssetBundleInfo = new AssetBundleInfo { AssetBundleName = builtAssetBundleNames[i], DependenciesBundleNames = assetBundleManifest.GetAllDependencies(builtAssetBundleNames[i]), releaseMode = bundleInfo.releaseMode, BaseBundle = bundleInfo.BaseBundle }; localAssetBundleInfo.HashString = md5; // 初始包里的ab包,原样拷贝md5 localAssetBundleInfo.Size = size; // bundle的尺寸 assetBundleGroupInfo.Add(localAssetBundleInfo.AssetBundleName, localAssetBundleInfo); } assetBundleVersionInfo.Add(groupName, assetBundleGroupInfo); }