private static bool BuildSingleFileToAssetBundleTemp(string exportPath, string assetPath) { bool buildSingleFileAb = false; foreach (var path in BuildSingleFilePaths) { if (assetPath.StartsWith(path)) { buildSingleFileAb = true; break; } } if (buildSingleFileAb) { var tempPath = EditorFileUtility.GetPathWithoutExt(assetPath); var assetbundlePath = exportPath + "/temp/" + tempPath + ".pkg"; if (HasBuildAssetBundleMapTemp.Contains(assetbundlePath)) { return(true); } EditorFileUtility.CreateParentDirecotry(assetbundlePath); List <Object> objects = new List <Object>(); List <string> assetNames = new List <string>(); objects.Add(AssetDatabase.LoadMainAssetAtPath(assetPath)); assetNames.Add(assetPath); BuildAssetBundleWithoutCollectDependencies(objects.ToArray(), assetNames.ToArray(), assetbundlePath); HasBuildAssetBundleMapTemp.Add(assetbundlePath); return(true); } return(false); }
//清理输出环境 private static void ClearExportDir() { var path = Path.GetDirectoryName(Application.dataPath); var exportPath = path + ExportDir; EditorFileUtility.ClearDirectory(exportPath); EditorFileUtility.ClearDirectory(exportPath + "/temp"); }
public static void ComplieXmlFile(string xmlExportDir, string xmlBinExportDir) { var fileInfos = EditorFileUtility.FilterDirectory(xmlExportDir, new string[] { ".xml" }, false); foreach (var file in fileInfos) { ParseXml(file.FullName, xmlBinExportDir); } }
public static void PackPatches(string dirPath, string patchName, string savePath, string ext) { var prefix = Path.GetFullPath(dirPath); var zipfiles = EditorFileUtility.FilterDirectory(savePath, new string[] { ".patch" }, false); if (zipfiles != null) { foreach (var zip in zipfiles) { File.Delete(zip.FullName); } } var files = EditorFileUtility.FilterDirectory(dirPath, new string[] { ext }); if (files != null) { int patchCount = 1; HashSet <string> patchList = new HashSet <string>(); foreach (var file in files) { if (!patchList.Contains(patchName + "p" + patchCount)) { patchList.Add(patchName + "p" + patchCount); using (ZipFile zip = ZipFile.Create(savePath + "/" + patchName + "p" + patchCount + ".patch")) { zip.BeginUpdate(); zip.Add(file.FullName, file.FullName.Replace(prefix, "")); zip.CommitUpdate(); } } else { using (ZipFile zip = new ZipFile(savePath + "/" + patchName + "p" + patchCount + ".patch")) { zip.BeginUpdate(); zip.Add(file.FullName, file.FullName.Replace(prefix, "")); zip.CommitUpdate(); } } FileInfo fi = new FileInfo(savePath + "/" + patchName + "p" + patchCount + ".patch"); if (fi.Length / 1024 > 1024 * 5) { ++patchCount; } } if (patchCount == 1) { FileInfo fi = new FileInfo(savePath + "/" + patchName + "p" + patchCount + ".patch"); fi.MoveTo(savePath + "/" + patchName + ".patch"); } } }
private static void BuildAssetBundleWithoutCollectDependencies(Object[] objs, string[] assetsName, string abPath) { for (int i = 0; i < assetsName.Length; i++) { assetsName[i] = EditorFileUtility.GetPathWithoutExt(assetsName[i]); } BuildPipeline.BuildAssetBundleExplicitAssetNames(objs, assetsName, abPath, BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.DeterministicAssetBundle, AssetBundleBuildTarget); }
private static bool BuildAllFileToAssetBundleTemp(List <string> allChildDependences, string exportPath, string assetPath) { bool isIncludeAllFile = false; string includeFilePath = ""; foreach (var path in BuildAllFilePaths) { if (assetPath.StartsWith(path)) { isIncludeAllFile = true; includeFilePath = path; break; } } if (isIncludeAllFile) { var tempPath = includeFilePath.Substring(0, includeFilePath.Length - 1); tempPath += "/" + Path.GetFileNameWithoutExtension(tempPath); var assetbundlePath = exportPath + "/temp/" + tempPath + ".pkg"; if (HasBuildAssetBundleMapTemp.Contains(assetbundlePath)) { return(true); } EditorFileUtility.CreateParentDirecotry(assetbundlePath); //收集所有同一个目录下(包括子目录)的asset List <string> allPath = new List <string>(); foreach (var dependencePath in allChildDependences) { if (dependencePath.StartsWith(includeFilePath)) { allPath.Add(dependencePath); } } List <Object> objects = new List <Object>(); List <string> assetNames = new List <string>(); foreach (var p in allPath) { objects.Add(AssetDatabase.LoadMainAssetAtPath(p)); assetNames.Add(p); } BuildAssetBundleWithoutCollectDependencies(objects.ToArray(), assetNames.ToArray(), assetbundlePath); HasBuildAssetBundleMapTemp.Add(assetbundlePath); return(true); } return(false); }
public static Dictionary <string, string> GetFilesMD5(string path, string ext) { Dictionary <string, string> fileMd5Map = new Dictionary <string, string>(); var files = EditorFileUtility.FilterDirectory(path, new string[] { ext }); if (files != null) { foreach (var file in files) { var md5 = EditorFileUtility.GetMD5HashFromFile(file.FullName); fileMd5Map.Add(file.FullName, md5); } } return(fileMd5Map); }
public static void CreateCsCode(string xmlExportDir, string csExportDir, string templateFile) { if (Directory.Exists(csExportDir)) { Directory.Delete(csExportDir, true); } Directory.CreateDirectory(csExportDir); var fileInfos = EditorFileUtility.FilterDirectory(xmlExportDir, new string[] { ".xml" }, false); foreach (var file in fileInfos) { ParseXml(file.FullName, csExportDir, templateFile); } }
private static void GetAssetDependences(Queue <string> assetQueue, string path) { if (IsIgoreAsset(path) || GameAssets.ContainsKey(path)) { return; } var assetInfo = new AssetInfo(); assetInfo.Path = path; var dependencesList = new HashSet <string>(); var dependences = AssetDatabase.GetDependencies(new string[] { path }); foreach (var dependencesPath in dependences) { if (dependencesPath == path) { continue; } if (IsIgoreAsset(dependencesPath)) { continue; } dependencesList.Add(dependencesPath); assetQueue.Enqueue(dependencesPath); } assetInfo.ChildDependences = dependencesList; GameAssets.Add(path, assetInfo); var pathWithoutExt = EditorFileUtility.GetPathWithoutExt(path); if (AssetWithoutExtMap.ContainsKey(pathWithoutExt)) { Debug.LogError("同一个目录下不应该有相同文件名,不同扩展名的资源命名存在:" + path); } else { AssetWithoutExtMap.Add(pathWithoutExt, true); } }
private string ListExcelFiles() { if (Directory.Exists(m_CacheExcelExportDir) && !string.IsNullOrEmpty(m_CacheExcelExportDir)) { FileInfo fileInfo = new FileInfo(m_CacheExcelExportDir); var lastFileWriteTime = EditorTimeUtility.DateTimeToUnixTimesStamp(fileInfo.LastWriteTimeUtc); if (lastFileWriteTime != m_ExcelExportDirChange) { m_ExcelFileList = EditorFileUtility.FilterDirectory(m_CacheExcelExportDir, new string[] { ".xlsx", ".xls", ".xlsm" }, false); m_ExcelExportDirChange = lastFileWriteTime; m_SelectionGridIndex = -1; m_LastSelectionGridIndex = -1; return(CheckDuplicateWorkSheet()); } } return(""); }
public static void CheckAssetBundleSize(string path, string ext) { var files = EditorFileUtility.FilterDirectory(path, new string[] { ext }); if (files != null) { StringBuilder sb = new StringBuilder(); sb.AppendLine("大于1m的AssetBundle包(建议AssetBundle的包小于1m,最好不要超过2m)"); foreach (var file in files) { FileInfo fi = new FileInfo(file.FullName); if (fi.Length / 1024 > 1024) { sb.AppendLine("\tAssetBundle文件路径:" + fi.FullName); sb.AppendLine("\t\t文件大小:" + (fi.Length / 1024f / 1024f) + "m"); } } File.WriteAllText(path + "/AssetBundleSize.txt", sb.ToString()); WindowsOSUtility.ExploreFile(path); } }
private static bool BuildCurrentDirectoryToAssetBundleTemp(List <string> allChildDependences, string exportPath, string assetPath) { var tempPath = EditorFileUtility.GetPath(assetPath); var assetBundleName = Path.GetFileName(Path.GetDirectoryName(assetPath)); var assetBundlePath = exportPath + "/temp/" + tempPath + "/" + assetBundleName + ".pkg"; if (HasBuildAssetBundleMapTemp.Contains(assetBundlePath)) { return(true); } EditorFileUtility.CreateParentDirecotry(assetBundlePath); //收集和自己同一个目录下的资源(不包括子目录) var currentDir = Path.GetDirectoryName(assetPath); List <string> readyToBuildAssets = new List <string>(); foreach (var dependence in allChildDependences) { if (Path.GetDirectoryName(dependence) == currentDir) { readyToBuildAssets.Add(dependence); } } List <Object> objects = new List <Object>(); List <string> assetNames = new List <string>(); foreach (var asset in readyToBuildAssets) { objects.Add(AssetDatabase.LoadMainAssetAtPath(asset)); assetNames.Add(asset); } BuildAssetBundleWithoutCollectDependencies(objects.ToArray(), assetNames.ToArray(), assetBundlePath); HasBuildAssetBundleMapTemp.Add(assetBundlePath); return(true); }
public string ExportConfig() { m_PreloadList = new List <short>(); m_HighList = new List <short>(); m_MediumList = new List <short>(); m_NormalList = new List <short>(); m_LowList = new List <short>(); m_DontLoadList = new List <short>(); var fileInfos = EditorFileUtility.FilterDirectory(m_Window.SettingUI.XmlExportDirectory, new string[] { ".xml" }, false); fileInfos.Sort(SortData); var priorityMap = new Dictionary <string, int>(); var xmlDoc = new XmlDocument(); xmlDoc.Load(m_Window.PriorityConfig.ConfigFilePath); var nodeList = xmlDoc.SelectSingleNode("root").ChildNodes; foreach (var childNode in nodeList) { var childElement = (XmlElement)childNode; foreach (var file in fileInfos) { if (Path.GetFileNameWithoutExtension(file.FullName) == childElement.Name) { priorityMap.Add(childElement.Name, int.Parse(childElement.InnerText)); break; } } } for (int i = 0; i < fileInfos.Count; i++) { var file = fileInfos[i]; var name = Path.GetFileNameWithoutExtension(file.FullName); switch (priorityMap[name]) { case 0: m_PreloadList.Add((short)i); break; case 1: m_HighList.Add((short)i); break; case 2: m_MediumList.Add((short)i); break; case 3: m_NormalList.Add((short)i); break; case 4: m_LowList.Add((short)i); break; case 5: m_DontLoadList.Add((short)i); break; } } WriteLoadConfig(priorityMap, fileInfos); return(string.Format("<color=#bbbbbb>写入{0}个xml的加载优先级</color>", fileInfos.Count)); }
private static void BuildCurrentDirectoryToAssetBundle(string exportPath, AssetInfo asset) { //判断是不是已经生成正常的AssetBundle var tempPath = EditorFileUtility.GetPath(asset.Path); var assetBundleName = Path.GetFileName(Path.GetDirectoryName(asset.Path)); var assetBundlePath = exportPath + "/" + tempPath + "/" + assetBundleName + ".pkg"; if (HasBuildAssetBundleMap.Contains(assetBundlePath)) { return; } EditorFileUtility.CreateParentDirecotry(assetBundlePath); //收集和自己同一个目录下的资源(不包括子目录) var currentDir = Path.GetDirectoryName(asset.Path); List <AssetInfo> readyToBuildAssets = new List <AssetInfo>(); foreach (var gameAsset in GameAssets) { if (Path.GetDirectoryName(gameAsset.Value.Path) == currentDir) { readyToBuildAssets.Add(gameAsset.Value); } } //收集asset的所有依赖,如果依赖是同一个assetBundle下的跳过 List <string> allChildDependences = new List <string>(); foreach (var gameAsset in readyToBuildAssets) { foreach (var dependence in gameAsset.ChildDependences) { if (Path.GetDirectoryName(dependence) != currentDir) { allChildDependences.Add(dependence); } } } //删除临时目录的AssetBundle EditorFileUtility.DeleteDirectory(exportPath + "/temp"); HasBuildAssetBundleMapTemp.Clear(); BuildPipeline.PushAssetDependencies(); foreach (var dependence in allChildDependences) { if (BuildAllFileToAssetBundleTemp(allChildDependences, exportPath, dependence)) { continue; } if (BuildSingleFileToAssetBundleTemp(exportPath, dependence)) { continue; } BuildCurrentDirectoryToAssetBundleTemp(allChildDependences, exportPath, dependence); } List <Object> objects = new List <Object>(); List <string> assetNames = new List <string>(); foreach (var buildAsset in readyToBuildAssets) { objects.Add(AssetDatabase.LoadMainAssetAtPath(buildAsset.Path)); assetNames.Add(buildAsset.Path); } BuildPipeline.PushAssetDependencies(); BuildAssetBundle(objects.ToArray(), assetNames.ToArray(), assetBundlePath); BuildPipeline.PopAssetDependencies(); BuildPipeline.PopAssetDependencies(); //配置asset的Assetbundle路径 foreach (var buildAsset in readyToBuildAssets) { buildAsset.AssetBundlePath = tempPath + "/" + assetBundleName + ".pkg"; } HasBuildAssetBundleMap.Add(assetBundlePath); }
private void ShowBuildUI(string platform) { versionPart = EditorGUILayout.Foldout(versionPart, "已打版本"); if (versionPart) { var versionList = GetVersions(m_ExportAssetBundleDir + platform + "/Version"); if (versionList != null && versionList.Count > 0) { GUILayoutOption[] options = { GUILayout.Width(150), GUILayout.Height(20), }; EditorGUILayout.BeginVertical(); EditorGUILayout.Space(); foreach (var v in versionList) { if (!m_VersionMap.ContainsKey(v.ToString())) { m_VersionMap.Add(v.ToString(), -1); } var selectIndex = m_VersionMap[v.ToString()]; selectIndex = GUILayout.SelectionGrid(selectIndex, new string[] { v.ToString() }, 1); m_VersionMap[v.ToString()] = selectIndex; if (selectIndex == 0) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.Space(); if (GUILayout.Button("浏览", options)) { WindowsOSUtility.ExploreFile(m_ExportAssetBundleDir + platform + "/Version/" + v.ToString()); } if (GUILayout.Button("检查ab包大小", options)) { AssetBundleBuilder.CheckAssetBundleSize(m_ExportAssetBundleDir + platform + "/Version/" + v, ".pkg"); } if (GUILayout.Button("取消查看", options)) { m_VersionMap[v.ToString()] = -1; Repaint(); } if (GUILayout.Button("拷贝到StreamingAssets", options)) { var targetPath = Application.dataPath + "/StreamingAssets/" + UGCoreConfig.MiddleFilePathName; EditorFileUtility.ClearDirectory(targetPath); EditorFileUtility.CopyDirectory(m_ExportAssetBundleDir + platform + "/Version/" + v.ToString(), targetPath); AssetDatabase.Refresh(); WindowsOSUtility.ExploreFile(targetPath); } if (GUILayout.Button("拷贝到persistentDataPath", options)) { var targetPath = Application.persistentDataPath + "/" + UGCoreConfig.MiddleFilePathName + "/" + UGCoreConfig.ResourcesFolderName; EditorFileUtility.ClearDirectory(targetPath); EditorFileUtility.CopyDirectory(m_ExportAssetBundleDir + platform + "/Version/" + v.ToString(), targetPath); AssetDatabase.Refresh(); WindowsOSUtility.ExploreFile(targetPath); } if (GUILayout.Button("删除版本", options)) { Directory.Delete(m_ExportAssetBundleDir + platform + "/Version/" + v.ToString(), true); Repaint(); } EditorGUILayout.Space(); EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); } } EditorGUILayout.Space(); EditorGUILayout.EndVertical(); } } patchPart = EditorGUILayout.Foldout(patchPart, "已打补丁"); if (patchPart) { var patchList = GetPatches(m_ExportAssetBundleDir + platform + "/Patches"); if (patchList != null && patchList.Count > 0) { GUILayoutOption[] options = { GUILayout.Width(150), GUILayout.Height(20), }; EditorGUILayout.BeginVertical(); EditorGUILayout.Space(); foreach (var p in patchList) { if (!m_VersionMap.ContainsKey(p.ToString())) { m_VersionMap.Add(p.ToString(), -1); } var selectIndex = m_VersionMap[p.ToString()]; selectIndex = GUILayout.SelectionGrid(selectIndex, new string[] { p.ToString() }, 1); m_VersionMap[p.ToString()] = selectIndex; if (selectIndex == 0) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.Space(); if (GUILayout.Button("浏览", options)) { WindowsOSUtility.ExploreFile(m_ExportAssetBundleDir + platform + "/Patches/" + p); } if (GUILayout.Button("取消查看", options)) { m_VersionMap[p.ToString()] = -1; Repaint(); } if (GUILayout.Button("删除补丁", options)) { Directory.Delete(m_ExportAssetBundleDir + platform + "/Patches/" + p, true); Repaint(); } EditorGUILayout.Space(); EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); } } EditorGUILayout.Space(); EditorGUILayout.EndVertical(); } } GUIContent[] guiBuildObjs = { new GUIContent("打版本"), new GUIContent("打补丁"), }; GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); m_BuildOperation = (BuildOperation)GUILayout.Toolbar((int)m_BuildOperation, guiBuildObjs); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); if (m_BuildOperation == BuildOperation.Version) { m_CreateVersion = EditorGUILayout.TextField("打包版本号:", m_CreateVersion); GUILayoutOption[] options = { GUILayout.Width(150), GUILayout.Height(26), }; GUILayout.FlexibleSpace(); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (GUILayout.Button("重新打包版本(慎用)", options)) { var versionNumber = ValidVersion(m_CreateVersion); if (versionNumber == null) { GetWindow <AssetBundleBuildEditor>().ShowNotification(new GUIContent("版本名称不符合规范,请重新输入")); return; } if (!Directory.Exists(m_ExportAssetBundleDir + platform + "/Version/" + versionNumber)) { GetWindow <AssetBundleBuildEditor>().ShowNotification(new GUIContent("版本号未经过打包,请使用打包版本选项来打包")); return; } CreateVersion(m_ExportAssetBundleDir + platform + "/Version/" + versionNumber, true); WindowsOSUtility.ExploreFile(m_ExportAssetBundleDir + platform + "/Version/" + versionNumber); } if (GUILayout.Button("打包版本", options)) { var versionNumber = ValidVersion(m_CreateVersion); if (versionNumber == null) { GetWindow <AssetBundleBuildEditor>().ShowNotification(new GUIContent("版本名称不符合规范,请重新输入")); return; } if (Directory.Exists(m_ExportAssetBundleDir + platform + "/Version/" + versionNumber)) { GetWindow <AssetBundleBuildEditor>().ShowNotification(new GUIContent("版本目录已经存在,请选择新的版本号或者清除现有的版本号目录")); return; } var versionList = GetVersions(m_ExportAssetBundleDir + platform + "/Version"); if (versionList != null && versionList.Count > 0) { if (VersionNumber.CompareTo(versionList[versionList.Count - 1], versionNumber) > 0) { GetWindow <AssetBundleBuildEditor>().ShowNotification(new GUIContent("当前版本号低于最新的版本号,无法打包,请重新选择版本号")); return; } } CreateVersion(m_ExportAssetBundleDir + platform + "/Version/" + versionNumber); WindowsOSUtility.ExploreFile(m_ExportAssetBundleDir + platform + "/Version/" + versionNumber); } GUILayout.EndHorizontal(); } else if (m_BuildOperation == BuildOperation.Patch) { var versionList = GetVersions(m_ExportAssetBundleDir + platform + "/Version"); if (versionList != null && versionList.Count > 0) { List <string> versionStringList = new List <string>(); foreach (var v in versionList) { versionStringList.Add(v.ToString()); } GUILayout.BeginHorizontal(); EditorGUILayout.Space(); GUILayout.BeginVertical(); EditorGUILayout.LabelField("低版本"); m_lowVersion = EditorGUILayout.Popup(m_lowVersion, versionStringList.ToArray()); GUILayout.EndVertical(); GUILayout.FlexibleSpace(); GUILayout.BeginVertical(); EditorGUILayout.LabelField("高版本"); m_highVersion = EditorGUILayout.Popup(m_highVersion, versionStringList.ToArray()); GUILayout.EndVertical(); EditorGUILayout.Space(); GUILayout.EndHorizontal(); GUILayoutOption[] options = { GUILayout.Width(150), GUILayout.Height(26), }; GUILayout.FlexibleSpace(); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (GUILayout.Button("打补丁", options)) { if (VersionNumber.CompareTo(versionList[m_lowVersion], versionList[m_highVersion]) >= 0) { GetWindow <AssetBundleBuildEditor>().ShowNotification(new GUIContent("低版本号不能大于等于高版本号")); return; } var patchName = versionList[m_lowVersion] + "-" + versionList[m_highVersion]; var fileMd5LowVersion = AssetBundleBuilder.GetFilesMD5( m_ExportAssetBundleDir + platform + "/Version/" + versionList[m_lowVersion], ".pkg"); var fileMd5HighVersion = AssetBundleBuilder.GetFilesMD5( m_ExportAssetBundleDir + platform + "/Version/" + versionList[m_highVersion], ".pkg"); Dictionary <string, string> lowVersionAssets = new Dictionary <string, string>(); foreach (var v in fileMd5LowVersion) { var str = v.Key.Replace(Path.GetFullPath(m_ExportAssetBundleDir + platform + "/Version/" + versionList[m_lowVersion] + "/"), ""); lowVersionAssets.Add(str, v.Value); } Dictionary <string, string> highVersionAssets = new Dictionary <string, string>(); foreach (var v in fileMd5HighVersion) { var str = v.Key.Replace(Path.GetFullPath(m_ExportAssetBundleDir + platform + "/Version/" + versionList[m_highVersion] + "/"), ""); highVersionAssets.Add(str, v.Value); } Dictionary <string, string> resultAssets = new Dictionary <string, string>(); foreach (var v in highVersionAssets) { if (!lowVersionAssets.ContainsKey(v.Key)) { resultAssets.Add(v.Key, v.Value); } else { if (lowVersionAssets[v.Key] != v.Value) { resultAssets.Add(v.Key, v.Value); } } } if (resultAssets.Count == 0) { GetWindow <AssetBundleBuildEditor>().ShowNotification(new GUIContent("两个版本之间没有差异,无需生成补丁")); return; } EditorFileUtility.ClearDirectory(m_ExportAssetBundleDir + platform + "/Patches/" + patchName); foreach (var v in resultAssets) { var sourceFileName = m_ExportAssetBundleDir + platform + "/Version/" + versionList[m_highVersion] + "/" + v.Key; var targetFileName = m_ExportAssetBundleDir + platform + "/Patches/" + patchName + "/" + v.Key; if (!Directory.Exists(Path.GetDirectoryName(targetFileName))) { Directory.CreateDirectory(Path.GetDirectoryName(targetFileName)); } File.Copy(sourceFileName, targetFileName); } var targetPath = m_ExportAssetBundleDir + platform + "/Patches/" + patchName + "/"; AssetBundleBuilder.PackPatches(targetPath, patchName, targetPath, ".pkg"); WindowsOSUtility.ExploreFile(targetPath); } GUILayout.EndHorizontal(); } } if (m_OldBuildOperation != m_BuildOperation) { m_lowVersion = 0; m_highVersion = 0; } m_OldBuildOperation = m_BuildOperation; }
public static void CheckResourceReference() { var filePaths = EditorFileUtility.FilterDirectoryIgnoreExt(Application.dataPath + "/Resources", IgnoreFileExtensionList); var rootPath = Path.GetDirectoryName(Application.dataPath); rootPath = rootPath.Replace("\\", "/") + "/"; if (filePaths != null) { Queue <string> assetQueue = new Queue <string>(); foreach (var filePath in filePaths) { var path = filePath.Replace("\\", "/"); path = path.Replace(rootPath, ""); assetQueue.Enqueue(path); } while (assetQueue.Count > 0) { var path = assetQueue.Dequeue(); if (GameAssets.ContainsKey(path)) { continue; } GetAssetDependences(assetQueue, path); } Dictionary <string, List <string> > sameAssets = new Dictionary <string, List <string> >(); foreach (var gameAsset in GameAssets) { var md5 = EditorFileUtility.GetMD5HashFromFile(Path.GetDirectoryName(Application.dataPath) + "/" + gameAsset.Value.Path); if (!sameAssets.ContainsKey(md5)) { sameAssets.Add(md5, new List <string>()); sameAssets[md5].Add(gameAsset.Value.Path); } else { sameAssets[md5].Add(gameAsset.Value.Path); } } StringBuilder sb = new StringBuilder(); foreach (var asset in sameAssets) { if (asset.Value.Count > 1) { sb.AppendLine("MD5: " + asset.Key); foreach (var p in asset.Value) { sb.AppendLine("\t资源路径:" + p); foreach (var gameAsset in GameAssets) { foreach (var cd in gameAsset.Value.ChildDependences) { if (cd == p) { sb.AppendLine("\t\t引用到它的资源:" + gameAsset.Key); break; } } } } } } sb.AppendLine(); sb.AppendLine("Resources目录下未被直接引用的资源(可能是代码中引用)"); foreach (var gameAsset in GameAssets) { var hasRefer = false; foreach (var gameAsset1 in GameAssets) { var isRefer = false; foreach (var cd in gameAsset1.Value.ChildDependences) { if (cd == gameAsset.Value.Path) { isRefer = true; break; } } if (isRefer) { hasRefer = true; break; } } if (!hasRefer) { sb.AppendLine("\t资源路径:" + gameAsset.Value.Path); } } File.WriteAllText(Path.GetDirectoryName(Application.dataPath) + "/CheckResourceReference.txt", sb.ToString()); WindowsOSUtility.ExploreFile(Path.GetDirectoryName(Application.dataPath)); } }
private static bool BuildSingleFileToAssetBundle(string exportPath, AssetInfo asset) { bool buildSingleFile = false; foreach (var path in BuildSingleFilePaths) { if (asset.Path.StartsWith(path)) { buildSingleFile = true; break; } } if (buildSingleFile) { //判断是不是已经生成正常的AssetBundle var tempPath = EditorFileUtility.GetPathWithoutExt(asset.Path); var assetbundlePath = exportPath + "/" + tempPath + ".pkg"; if (HasBuildAssetBundleMap.Contains(assetbundlePath)) { return(false); } EditorFileUtility.CreateParentDirecotry(assetbundlePath); //收集asset的所有依赖 List <string> allChildDependences = new List <string>(); foreach (var dependence in asset.ChildDependences) { if (asset.Path == dependence) { continue; } allChildDependences.Add(dependence); } //删除临时目录的AssetBundle EditorFileUtility.DeleteDirectory(exportPath + "/temp"); HasBuildAssetBundleMapTemp.Clear(); BuildPipeline.PushAssetDependencies(); foreach (var dependence in allChildDependences) { if (BuildAllFileToAssetBundleTemp(allChildDependences, exportPath, dependence)) { continue; } if (BuildSingleFileToAssetBundleTemp(exportPath, dependence)) { continue; } BuildCurrentDirectoryToAssetBundleTemp(allChildDependences, exportPath, dependence); } List <Object> objects = new List <Object>(); List <string> assetNames = new List <string>(); objects.Add(AssetDatabase.LoadMainAssetAtPath(asset.Path)); assetNames.Add(asset.Path); BuildPipeline.PushAssetDependencies(); BuildAssetBundle(objects.ToArray(), assetNames.ToArray(), assetbundlePath); BuildPipeline.PopAssetDependencies(); BuildPipeline.PopAssetDependencies(); //配置asset的Assetbundle路径 asset.AssetBundlePath = tempPath + ".pkg"; HasBuildAssetBundleMap.Add(assetbundlePath); return(true); } return(false); }
public static void LoadAllAssets() { GameAssets.Clear(); AssetWithoutExtMap.Clear(); HasBuildAssetBundleMap.Clear(); var filePaths = EditorFileUtility.FilterDirectoryIgnoreExt(Application.dataPath + "/Resources", IgnoreFileExtensionList); var rootPath = Path.GetDirectoryName(Application.dataPath); rootPath = rootPath.Replace("\\", "/") + "/"; if (filePaths != null) { Queue <string> assetQueue = new Queue <string>(); foreach (var filePath in filePaths) { var path = filePath.Replace("\\", "/"); path = path.Replace(rootPath, ""); bool canAdd = true; foreach (var ignoreFile in IgnoreFileList) { if (path.StartsWith(ignoreFile)) { canAdd = false; } } if (canAdd) { assetQueue.Enqueue(path); } } while (assetQueue.Count > 0) { var path = assetQueue.Dequeue(); if (GameAssets.ContainsKey(path)) { continue; } GetAssetDependences(assetQueue, path); } ReadBuildConfig(); ClearExportDir(); BuildAssetBundles(); ExportAssetDependences(); PackScripts(); string assetName = ""; try { foreach (var gameAsset in GameAssets) { assetName = gameAsset.Key; CheckAssetsDenpendences(assetName); } } catch (Exception e) { if (e.ToString().StartsWith("System.StackOverflowException")) { Debug.LogError("检查资源依赖关系出现栈溢出问题,可能是资源存在环形依赖问题,资源名称是:" + assetName + ",请修复问题并重新打包:" + e); } else { Debug.LogError("检查资源依赖关系出现错误,请修复问题并重新打包:" + e); } } } var exportRootPath = Path.GetDirectoryName(Application.dataPath); EditorFileUtility.DeleteDirectory(exportRootPath + ExportDir + "/Temp"); }
private static bool BuildAllFileToAssetBundle(string exportPath, AssetInfo asset) { bool isIncludeAllFile = false; string includeFilePath = ""; foreach (var path in BuildAllFilePaths) { if (asset.Path.StartsWith(path)) { isIncludeAllFile = true; includeFilePath = path; break; } } if (isIncludeAllFile) { //判断是不是已经生成正常的AssetBundle var tempPath = includeFilePath.Substring(0, includeFilePath.Length - 1); tempPath += "/" + Path.GetFileNameWithoutExtension(tempPath); var assetbundlePath = exportPath + "/" + tempPath + ".pkg"; if (HasBuildAssetBundleMap.Contains(assetbundlePath)) { return(false); } EditorFileUtility.CreateParentDirecotry(assetbundlePath); //收集所有同一个目录下(包括子目录)的asset List <AssetInfo> readyToBuildAssets = new List <AssetInfo>(); foreach (var gameAsset in GameAssets) { if (gameAsset.Value.Path.StartsWith(includeFilePath)) { readyToBuildAssets.Add(gameAsset.Value); } } //收集asset的所有依赖,如果依赖是同一个assetBundle下的跳过 List <string> allChildDependences = new List <string>(); foreach (var gameAsset in readyToBuildAssets) { foreach (var dependence in gameAsset.ChildDependences) { if (dependence.StartsWith(includeFilePath)) { continue; } allChildDependences.Add(dependence); } } //删除临时目录的AssetBundle EditorFileUtility.DeleteDirectory(exportPath + "/temp"); HasBuildAssetBundleMapTemp.Clear(); BuildPipeline.PushAssetDependencies(); foreach (var dependence in allChildDependences) { if (BuildAllFileToAssetBundleTemp(allChildDependences, exportPath, dependence)) { continue; } if (BuildSingleFileToAssetBundleTemp(exportPath, dependence)) { continue; } BuildCurrentDirectoryToAssetBundleTemp(allChildDependences, exportPath, dependence); } List <Object> objects = new List <Object>(); List <string> assetNames = new List <string>(); foreach (var buildAsset in readyToBuildAssets) { objects.Add(AssetDatabase.LoadMainAssetAtPath(buildAsset.Path)); assetNames.Add(buildAsset.Path); } BuildPipeline.PushAssetDependencies(); BuildAssetBundle(objects.ToArray(), assetNames.ToArray(), assetbundlePath); BuildPipeline.PopAssetDependencies(); BuildPipeline.PopAssetDependencies(); //配置asset的Assetbundle路径 foreach (var buildAsset in readyToBuildAssets) { buildAsset.AssetBundlePath = tempPath + ".pkg"; } HasBuildAssetBundleMap.Add(assetbundlePath); return(true); } return(false); }
private static void ExportAssetDependences() { List <string> stringBuff = new List <string>(); Dictionary <string, int> stringBuffIndex = new Dictionary <string, int>(); foreach (var gameAsset in GameAssets) { var path = EditorFileUtility.GetPathWithoutExt(gameAsset.Value.Path); if (!stringBuffIndex.ContainsKey(path)) { stringBuff.Add(path); stringBuffIndex.Add(path, stringBuff.Count - 1); } if (!stringBuffIndex.ContainsKey(gameAsset.Value.AssetBundlePath)) { stringBuff.Add(gameAsset.Value.AssetBundlePath); stringBuffIndex.Add(gameAsset.Value.AssetBundlePath, stringBuff.Count - 1); } foreach (var dependence in gameAsset.Value.ChildDependences) { path = EditorFileUtility.GetPathWithoutExt(dependence); if (!stringBuffIndex.ContainsKey(path)) { stringBuff.Add(path); stringBuffIndex.Add(path, stringBuff.Count - 1); } } } FileStream fs = File.Create(Application.dataPath + "/Resources/AssetsDependences.xml"); //资源的数量 var count = GameAssets.Count; var bytes = BitConverter.GetBytes(count); BitConverterUtility.ConvertToLittleEndian(bytes, 0, bytes.Length); fs.Write(bytes, 0, bytes.Length); //字符串的数量 count = stringBuff.Count; bytes = BitConverter.GetBytes(count); BitConverterUtility.ConvertToLittleEndian(bytes, 0, bytes.Length); fs.Write(bytes, 0, bytes.Length); //写入字符串资源 var buffsize = GetStringBufferSize(stringBuff); bytes = BitConverter.GetBytes(buffsize); BitConverterUtility.ConvertToLittleEndian(bytes, 0, bytes.Length); fs.Write(bytes, 0, bytes.Length); foreach (var str in stringBuff) { bytes = Encoding.UTF8.GetBytes(str); fs.Write(bytes, 0, bytes.Length); fs.WriteByte(0); } //写入每一个资源的条目 foreach (var gameAsset in GameAssets) { var index = stringBuffIndex[EditorFileUtility.GetPathWithoutExt(gameAsset.Value.Path)]; bytes = BitConverter.GetBytes(index); BitConverterUtility.ConvertToLittleEndian(bytes, 0, bytes.Length); fs.Write(bytes, 0, bytes.Length); index = stringBuffIndex[gameAsset.Value.AssetBundlePath]; bytes = BitConverter.GetBytes(index); BitConverterUtility.ConvertToLittleEndian(bytes, 0, bytes.Length); fs.Write(bytes, 0, bytes.Length); count = gameAsset.Value.ChildDependences.Count; bytes = BitConverter.GetBytes(count); BitConverterUtility.ConvertToLittleEndian(bytes, 0, bytes.Length); fs.Write(bytes, 0, bytes.Length); foreach (var d in gameAsset.Value.ChildDependences) { index = stringBuffIndex[EditorFileUtility.GetPathWithoutExt(d)]; bytes = BitConverter.GetBytes(index); BitConverterUtility.ConvertToLittleEndian(bytes, 0, bytes.Length); fs.Write(bytes, 0, bytes.Length); } } fs.Flush(); fs.Close(); AssetDatabase.Refresh(); Object[] objs = new Object[1]; objs[0] = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/AssetsDependences.xml"); string[] assetsName = new string[] { "Assets/Resources/AssetsDependences" }; BuildAssetBundle(objs, assetsName, Path.GetDirectoryName(Application.dataPath) + ExportDir + "/AssetsDependences.pkg"); File.Delete(Application.dataPath + "/Resources/AssetsDependences.xml"); File.Delete(Application.dataPath + "/Resources/AssetsDependences.xml.meta"); AssetDatabase.Refresh(); }