/// <summary> /// 根据versionName获取需要打包的所有资源并返回AssetBundleBuild数组 /// </summary> /// <param name="versionName">一般是平台名字,也可以加其他文件夹在</param> /// <returns>返回一个数组,可以直接用于BuildPipeline.BuildAssetBundles</returns> private static AssetBundleBuild[] GetBuildMapByVersion(string versionName) { List <AssetBundleBuild> list = new List <AssetBundleBuild>(); if (!Directory.Exists(ABSH.NeedBuildABPath)) { return(null); } var NeedBuildABFileList = GetAllFilesWithoutMeta(ABSH.NeedBuildABPath); var dicDependencies = new Dictionary <string, string>(); //遍历这些文件去找到所有的依赖项 foreach (var file in NeedBuildABFileList) { //添加正向依赖,这个不可能会重复,所以不用判断ContainsKey ABDependenciesPositive.Add(file.Key, ABDependenciesPositive.GetDRAC(file.Key)); //var relativePath = Path.Combine(); var dps = AssetDatabase.GetDependencies(file.Key); // 这里文件名进行了转换,windows下都变成\\,OSX下都是/ //也转换了一下小写 foreach (var dp in dps.Where(d => !d.EndsWith(".cs")).Select(d => AssetBundleSettingHelper.PathToPlatformFormat(d.ToLower()))) //脚本文件排除 { { //增加反向依赖,这个会统计数量 if (!file.Key.Equals(dp)) { if (!ABDependenciesReverse.ContainsKey(dp)) { ABDependenciesReverse.Add(dp, ABDependenciesReverse.GetDRAC(dp)); } ABDependenciesReverse[dp].AddRef(file.Key); //正向依赖 ABDependenciesPositive[file.Key].AddRef(dp); } } if (dicDependencies.ContainsKey(dp)) { continue; } else { dicDependencies.Add(dp, dp); } } } //这里根据依赖关系,把只被一个单独AB依赖的资源不设置单独打包 foreach (var abd in ABDependenciesReverse.GetDic().Where(dr => dr.Value.Count == 1)) { if (dicDependencies.ContainsKey(abd.Key)) { dicDependencies.Remove(abd.Key); } } //这里已经获得了所有的资源名称,可以直接生成AssetBundleBuild了 foreach (var file in dicDependencies) { //目前先把所有资源单独打包,不做任何合并处理 var tmp = new AssetBundleBuild(); { tmp.assetBundleName = ABSH.ResourceNameToBundleName(file.Key); string[] assets = new string[1] { file.Key }; tmp.assetNames = assets; } list.Add(tmp); } return(list.ToArray()); }
/// <summary> /// 生成资源依赖信息 /// </summary> /// <param name="versionName">一般是平台名字,也可以加其他文件夹在</param> private static void GenABDependenciesInfo(string versionName) { var abpi = ABSH.GetABDependenciesXmlPath(versionName); XmlDocument xmlDoc = null; //创建XML文档实例 xmlDoc = new XmlDocument(); XmlElement AllRoot = xmlDoc.CreateElement(AssetBundleSettingHelper.xmlNode_ABDependencies); //创建个时间属性,可以更直观的对比不同版本的 AllRoot.SetAttribute(AssetBundleSettingHelper.xmlAttribute_CreateTime, VersionInfo.GetVersionString()); xmlDoc.AppendChild(AllRoot); //正向依赖 XmlElement ABDs_P = xmlDoc.CreateElement(AssetBundleSettingHelper.xmlNode_ABDependencies_P); foreach (var abd in ABDependenciesPositive.GetDic()) { XmlElement ABD_P = xmlDoc.CreateElement(AssetBundleSettingHelper.xmlNode_ABDependency_P); ABD_P.SetAttribute(AssetBundleSettingHelper.xmlNode_AB, abd.Key); ABD_P.SetAttribute(AssetBundleSettingHelper.xmlNode_ABDependency_Count, abd.Value.Count.ToString()); if (abd.Value.FinalSize > 0 && abd.Value.FinalSize < 1024) { ABD_P.SetAttribute(AssetBundleSettingHelper.xmlNode_ABSizeXB, ((float)abd.Value.FinalSize).ToString() + " B"); } else if (abd.Value.FinalSize >= 1024 && abd.Value.FinalSize < 1024 * 1024) { ABD_P.SetAttribute(AssetBundleSettingHelper.xmlNode_ABSizeXB, ((float)abd.Value.FinalSize / 1024).ToString("f3") + " KB"); } else if (abd.Value.FinalSize >= 1024 * 1024) { ABD_P.SetAttribute(AssetBundleSettingHelper.xmlNode_ABSizeXB, ((float)abd.Value.FinalSize / (1024 * 1024)).ToString("f3") + " MB"); } foreach (var d in abd.Value.Refs) { XmlElement d_P = xmlDoc.CreateElement(AssetBundleSettingHelper.xmlNode_ABDependency); d_P.SetAttribute(AssetBundleSettingHelper.xmlNode_Name, d); var size = ABDependenciesPositive.GetDRAC(d, true).FinalSize; if (size > 0 && size < 1024) { d_P.SetAttribute(AssetBundleSettingHelper.xmlNode_ABSizeXB, ((float)size).ToString() + " B"); } else if (size >= 1024 && size < 1024 * 1024) { d_P.SetAttribute(AssetBundleSettingHelper.xmlNode_ABSizeXB, ((float)size / 1024).ToString("f3") + " KB"); } else if (size >= 1024 * 1024) { d_P.SetAttribute(AssetBundleSettingHelper.xmlNode_ABSizeXB, ((float)size / (1024 * 1024)).ToString("f3") + " MB"); } ABD_P.AppendChild(d_P); } ABDs_P.AppendChild(ABD_P); } AllRoot.AppendChild(ABDs_P); //反向依赖 XmlElement ABDs_R = xmlDoc.CreateElement(AssetBundleSettingHelper.xmlNode_ABDependencies_R); foreach (var abd in ABDependenciesReverse.GetDic()) { XmlElement ABD_R = xmlDoc.CreateElement(AssetBundleSettingHelper.xmlNode_ABDependency_R); ABD_R.SetAttribute(AssetBundleSettingHelper.xmlNode_AB, abd.Key); ABD_R.SetAttribute(AssetBundleSettingHelper.xmlNode_ABDependency_Count, abd.Value.Count.ToString()); if (abd.Value.FinalSize > 0 && abd.Value.FinalSize < 1024) { ABD_R.SetAttribute(AssetBundleSettingHelper.xmlNode_ABSizeXB, ((float)abd.Value.FinalSize).ToString() + " B"); } else if (abd.Value.FinalSize >= 1024 && abd.Value.FinalSize < 1024 * 1024) { ABD_R.SetAttribute(AssetBundleSettingHelper.xmlNode_ABSizeXB, ((float)abd.Value.FinalSize / 1024).ToString("f3") + " KB"); } else if (abd.Value.FinalSize >= 1024 * 1024) { ABD_R.SetAttribute(AssetBundleSettingHelper.xmlNode_ABSizeXB, ((float)abd.Value.FinalSize / (1024 * 1024)).ToString("f3") + " MB"); } foreach (var d in abd.Value.Refs) { XmlElement d_R = xmlDoc.CreateElement(AssetBundleSettingHelper.xmlNode_ABDependency); d_R.SetAttribute(AssetBundleSettingHelper.xmlNode_Name, d); var size = ABDependenciesPositive.GetDRAC(d, true).FinalSize; if (size > 0 && size < 1024) { d_R.SetAttribute(AssetBundleSettingHelper.xmlNode_ABSizeXB, ((float)size).ToString() + " B"); } else if (size >= 1024 && size < 1024 * 1024) { d_R.SetAttribute(AssetBundleSettingHelper.xmlNode_ABSizeXB, ((float)size / 1024).ToString("f3") + " KB"); } else if (size >= 1024 * 1024) { d_R.SetAttribute(AssetBundleSettingHelper.xmlNode_ABSizeXB, ((float)size / (1024 * 1024)).ToString("f3") + " MB"); } ABD_R.AppendChild(d_R); } ABDs_R.AppendChild(ABD_R); } AllRoot.AppendChild(ABDs_R); //同名文件直接覆盖 xmlDoc.Save(abpi.FullName); }