//强制以文本的方式保存资源 EditorSettings.serializationMode = SerializationMode.ForceText; AssetDatabase.Refresh(); //获取你需要匹配的资源 一般通过后缀查找 List<string> withoutExtensions = new List<string>() { ".prefab", ".unity", ".mat" }; string[] files = Directory.GetFiles(Path.Combine(Application.dataPath, "AssetsPackage"), "*.*", SearchOption.AllDirectories) .Where(s => withoutExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray(); //你需要搜索的资源 通过路径转换为guid string guid = AssetDatabase.AssetPathToGUID(imagePaths); //遍历匹配的资源 匹配guid foreach (var file in files) { var content = File.ReadAllText(file); if (Regex.IsMatch(fileContent, guid)) { isHas = true; } }
如果是多对多的关系,例如查找项目中没有使用过的图片,就需要遍历找到所有图片资源,然后每一个图片都需要遍历一次匹配的资源。可以采用多线程的机制跑,且将IO操作提前缓存文本内容。可以质一般的提升速度。