/// <summary> /// 根据资源名称加载Bundle /// </summary> /// <param name="AssetName"><para>资源名需要后缀,以区分不同类型但名字相同的资源</para> /// <para>虽然资源名可以是全路径名,大小写不区分,但是在读配置文件时需要大小写,及仅需</para> /// <para>全名,并且资源名字命名中不应该有类似Cube/cube这样的命名存在</para></param> // 这是在所有资源都是最新的情况下,而资源的更新在打开应用的时候,检查版本,资源版本,以及新资源 public static GameObject LoadBundle(string AssetName) { if (string.IsNullOrEmpty(AssetName)) { Debug.Log("AssetName is Null."); return(null); } //从bundle配置文件中找到资源对应的bundle名 string BundleName = null; bundle_Asset bundleConfig = ConfigOperation <bundle_Asset> .Deserialize(defalteConfigPath); try { BundleName = bundleConfig.namekeys.Find(temp => AssetName == (temp.Name.Substring(temp.Name.LastIndexOf('/') + 1))).Key; } catch { Debug.Log("BundleName is Null or No Bundle for thisAsset"); return(null); } //获取根bundle文件,并取得bundle的根链表 AssetBundle baseBundle = AssetBundle.LoadFromFile(Util.PathADD(BundleBasePath, Util.GetPlatformFolder(Application.platform))); AssetBundleManifest mainfest = (AssetBundleManifest)baseBundle.LoadAsset("AssetBundleManifest"); baseBundle.Unload(false); //加载该bundle所需依赖bundle List <string> dps = mainfest.GetAllDependencies(BundleName).ToList(); List <AssetBundle> dpBundles = new List <AssetBundle>(); foreach (var dpBundleName in dps) { dpBundles.Add(AssetBundle.LoadFromFile(Util.PathADD(BundleBasePath, dpBundleName))); } //加载目标bundle AssetBundle targetBundle = AssetBundle.LoadFromFile(Util.PathADD(BundleBasePath, BundleName)); GameObject targetObj = targetBundle.LoadAsset(AssetName) as GameObject; //释放加载的bundle targetBundle.Unload(false); foreach (var ab in dpBundles) { ab.Unload(false); } if (targetObj != null) { return(MonoBehaviour.Instantiate(targetObj)); } else { return(null); } }
public static void Calculate() { assetList.Clear(); string sourcePath = EditorUtility.OpenFolderPanel("SeletionPanel", Application.dataPath, ""); if (string.IsNullOrEmpty(sourcePath)) { Debug.Log("路径为空!!"); return; } Pack(sourcePath); #region 遍历所有要打包的资源,计算出被直接引用的次数 foreach (BuildCell bc in assetList) { //获取的直接依赖中有自己 foreach (var dps in AssetDatabase.GetDependencies(bc.assetName, false)) { if (dps == bc.assetName) { continue; } else { //将被直接依赖资源的计数器加1(在父级的其他依赖中没有我,则为直接依赖) int dpsBCint = assetList.FindIndex(temp => temp.assetName == dps); if (dpsBCint >= 0) { assetList[dpsBCint] = new BuildCell(assetList[dpsBCint].referenceCount + 1, assetList[dpsBCint].assetName, null); } } } } #endregion #region 给资源设置AssetBundleName foreach (BuildCell bc in assetList) { if (bc.bundleName != null) { continue; } if (bc.referenceCount == 0 || bc.referenceCount >= 2) { //在代码中给资源设置AssetBundleName AssetImporter assetImporter = AssetImporter.GetAtPath(bc.assetName); //string assetName = AssetDatabase.AssetPathToGUID(_assetPath); string bundleName = bc.assetName.Replace("Assets/", "").Replace(Path.GetExtension(bc.assetName), "").ToLower(); assetList[assetList.IndexOf(bc)] = new BuildCell(bc.referenceCount, bc.assetName, bundleName); assetImporter.assetBundleName = bundleName; //收索直接依赖中被依赖数为1的资源, //将所有直接依赖中被依赖数为1的资源的bundle名设为父bundle名。 SearchDPS(bc.assetName, bundleName); } } #endregion #region 将资源名和bundle名作配置表 bundle_Asset baConfig = new bundle_Asset(); foreach (var bc in assetList) { NameKey nk = new NameKey(); nk.Name = bc.assetName; nk.Key = bc.bundleName; baConfig.namekeys.Add(nk); } if (!File.Exists(AssetBundlesOutputPath + "/bundleConfig")) { File.Create(AssetBundlesOutputPath + "/bundleConfig"); } ConfigOperation <bundle_Asset> .Serialize(AssetBundlesOutputPath + "/bundleConfig", baConfig); #endregion #region 打包 string outputPath = Path.Combine(AssetBundlesOutputPath, GetPlatformFolder(EditorUserBuildSettings.activeBuildTarget)); if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } //根据BuildSetting里面所激活的平台进行打包 BuildPipeline.BuildAssetBundles(outputPath, BuildAssetBundleOptions.UncompressedAssetBundle, EditorUserBuildSettings.activeBuildTarget); AssetDatabase.Refresh(); Debug.Log("打包完成"); #endregion }