I18nAssetsReferencesTask.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEditor;
  5. namespace XGame.Editor.Asset.Tools
  6. {
  7. public class I18nAssetsReferencesTask
  8. {
  9. private string[] _i18nAssetRoots;
  10. private HashSet<string> _i18nAssetPaths;
  11. private HashSet<string> _preAssetPaths;
  12. public void Run()
  13. {
  14. CollectI18nAssets();
  15. CollectAddressableAssets();
  16. var errorMsgs = VerifyAssetReferences();
  17. if (errorMsgs.Count == 0)
  18. {
  19. UnityEngine.Debug.Log($"I18nAssetsReferences 查找结束.");
  20. return;
  21. }
  22. Export(errorMsgs);
  23. }
  24. private void CollectI18nAssets()
  25. {
  26. _i18nAssetRoots = FileUtil.GetI18nAssetsRoots();
  27. _i18nAssetPaths = new HashSet<string>();
  28. var assetGuids = AssetDatabase.FindAssets("", _i18nAssetRoots);
  29. foreach (var assetGuid in assetGuids)
  30. {
  31. var assetPath = AssetDatabase.GUIDToAssetPath(assetGuid);
  32. if (Directory.Exists(assetPath) || FileUtil.IsFileIgnore(assetPath))
  33. {
  34. continue;
  35. }
  36. _i18nAssetPaths.Add(assetPath);
  37. }
  38. }
  39. private void CollectAddressableAssets()
  40. {
  41. _preAssetPaths = new HashSet<string>();
  42. var assetGuids = AssetDatabase.FindAssets("", new string[] { PathDefine.ResAddressableRelative });
  43. foreach (var assetGuid in assetGuids)
  44. {
  45. var assetPath = AssetDatabase.GUIDToAssetPath(assetGuid);
  46. if (Directory.Exists(assetPath) || FileUtil.IsFileIgnore(assetPath) || FileUtil.IsTextureFile(assetPath))
  47. {
  48. continue;
  49. }
  50. _preAssetPaths.Add(assetPath);
  51. }
  52. }
  53. private List<string> VerifyAssetReferences()
  54. {
  55. var errorMsgs = new List<string>();
  56. foreach (var assetPath in _preAssetPaths)
  57. {
  58. var deps = AssetDatabase.GetDependencies(assetPath, true);
  59. foreach (var dep in deps)
  60. {
  61. if (_i18nAssetPaths.Contains(dep))
  62. {
  63. errorMsgs.Add($"资源:{assetPath} 错误依赖:{dep}");
  64. }
  65. }
  66. }
  67. return errorMsgs;
  68. }
  69. /// <summary>
  70. /// 把日志内容显示在记事本中
  71. /// </summary>
  72. public void Export(List<string> errorMsgs)
  73. {
  74. try
  75. {
  76. var str = string.Join("\n", errorMsgs);
  77. var path = Path.Combine(Path.GetTempPath(), "I18nAssetsReferencesError.txt");
  78. File.WriteAllText(path, str);
  79. UnityEngine.Debug.Log($"I18nAssetsReferences 输出路径:{path}");
  80. #if UNITY_EDITOR_WIN
  81. var proc = System.Diagnostics.Process.Start("notepad.exe", path);
  82. #endif
  83. }
  84. catch (Exception e)
  85. {
  86. UnityEngine.Debug.LogException(e);
  87. }
  88. }
  89. }
  90. }