FindReferences.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using UnityEditor;
  8. using UnityEngine;
  9. using Object = UnityEngine.Object;
  10. namespace XGame.Editor.Asset.Tools
  11. {
  12. public static class FindReferences
  13. {
  14. private class Logger
  15. {
  16. StringBuilder sb = new StringBuilder();
  17. public void Debug(string info)
  18. {
  19. sb.AppendLine(info.Replace(StrColor, "").Replace("</color>", ""));
  20. UnityEngine.Debug.Log(info);
  21. }
  22. public void Debug(string info, Object context)
  23. {
  24. sb.AppendLine(info.Replace(StrColor, "").Replace("</color>", ""));
  25. UnityEngine.Debug.Log(info, context);
  26. }
  27. public void Error(string error)
  28. {
  29. UnityEngine.Debug.LogError(error);
  30. }
  31. /// <summary>
  32. /// 把日志内容显示在记事本中
  33. /// </summary>
  34. public void ShowInNotepad()
  35. {
  36. #if UNITY_EDITOR_WIN
  37. try
  38. {
  39. var path = Path.GetTempPath() + Path.GetRandomFileName();
  40. File.WriteAllText(path, sb.ToString());
  41. var proc = System.Diagnostics.Process.Start("notepad.exe", path);
  42. }
  43. catch (Exception e)
  44. {
  45. UnityEngine.Debug.LogException(e);
  46. }
  47. #endif
  48. }
  49. public void Clear()
  50. {
  51. sb.Clear();
  52. }
  53. }
  54. private const string StrColor = "<color=darkblue>";
  55. #region 查找引用
  56. [MenuItem("Assets/查找引用", false, 130)]
  57. private static void Find()
  58. {
  59. var lstPath = new List<string>();
  60. foreach (var obj in Selection.objects)
  61. {
  62. var assetPath = AssetDatabase.GetAssetPath(obj);
  63. if (Directory.Exists(assetPath))
  64. {
  65. var files = Directory.GetFiles(assetPath, "*", SearchOption.AllDirectories);
  66. foreach (var file in files)
  67. {
  68. lstPath.Add(FileUtil.ToRelativePath(file).Replace("\\", "/"));
  69. }
  70. }
  71. else
  72. {
  73. lstPath.Add(assetPath);
  74. }
  75. }
  76. lstPath = lstPath.Distinct().ToList();// .Where(p => FileUtil.IsFileIgnore(p) == false)
  77. var count = lstPath.Count;
  78. if (count == 0) return;
  79. var logger = new Logger();
  80. try
  81. {
  82. var extensions = new List<string>() { ".prefab", ".unity", ".mat", ".asset", ".fontsettings", ".anim" };
  83. var files = Directory.GetFiles(Application.dataPath, "*.*", SearchOption.AllDirectories)
  84. .Where(s =>
  85. {
  86. var temp = s.Replace("\\", "/");
  87. if (FileUtil.IsFileIgnore(temp) || temp.Contains(PathDefine.ConfigsRelative))
  88. {
  89. return false;
  90. }
  91. return extensions.Contains(Path.GetExtension(temp).ToLower());
  92. }).ToArray();
  93. var fileCount = files.Length;
  94. foreach (var assetPath in lstPath)
  95. {
  96. var guid = AssetDatabase.AssetPathToGUID(assetPath);
  97. if (string.IsNullOrEmpty(guid)) continue;
  98. var addressableName = AddressableHelper.GUIDToName(guid);
  99. var fileIndex = 0;
  100. logger.Debug("====================================");
  101. logger.Debug($"{StrColor}匹配开始 Path:{assetPath} GUID:{guid}</color>", AssetDatabase.LoadAssetAtPath<Object>(assetPath));
  102. bool isCancel = false;
  103. foreach (var file in files)
  104. {
  105. var fileRelativePath = FileUtil.ToRelativePath(file.Replace("\\", "/"));
  106. isCancel = EditorUtility.DisplayCancelableProgressBar($"查找引用: {assetPath}", fileRelativePath, (float)(fileIndex++) / files.Length);
  107. var text = File.ReadAllText(file);
  108. if (Regex.IsMatch(text, guid) || (!string.IsNullOrEmpty(addressableName) && Regex.IsMatch(text, addressableName)))
  109. { //查找GUID或者addressableName的引用
  110. logger.Debug($"{StrColor}{fileRelativePath}</color>", AssetDatabase.LoadAssetAtPath<Object>(fileRelativePath));
  111. }
  112. if (isCancel)
  113. {
  114. logger.Debug("用户取消查找!");
  115. break;
  116. }
  117. }
  118. logger.ShowInNotepad();
  119. logger.Clear();
  120. EditorUtility.ClearProgressBar();
  121. if (isCancel)
  122. {
  123. break;
  124. }
  125. }
  126. }
  127. catch (Exception ex)
  128. {
  129. UnityEngine.Debug.LogException(ex);
  130. }
  131. }
  132. [MenuItem("Assets/查找引用", true)]
  133. private static bool VFind()
  134. {
  135. var path = AssetDatabase.GetAssetPath(Selection.activeObject);
  136. return (!string.IsNullOrEmpty(path));
  137. }
  138. #endregion
  139. #region 查找依赖
  140. [MenuItem("Assets/查找依赖", false, 131)]
  141. public static void FindDependencies()
  142. {
  143. AssetsDependenciesWindow.CreateWindow();
  144. }
  145. [MenuItem("Assets/查找依赖", true)]
  146. private static bool FindIncludeReferencesCheck()
  147. {
  148. var path = AssetDatabase.GetAssetPath(Selection.activeObject);
  149. return (!string.IsNullOrEmpty(path));
  150. }
  151. #endregion
  152. }
  153. }