ReferenceManager.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. namespace AtlasUtility
  2. {
  3. using UnityEditor;
  4. using UnityEngine;
  5. using UnityEditor.SceneManagement;
  6. using System;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Collections.Generic;
  10. using System.Text.RegularExpressions;
  11. public class ReferenceManager
  12. {
  13. public static void ChangeReference(List<string> fromReferenceList, List<string> toReferenceList)
  14. {
  15. if (fromReferenceList.Count != toReferenceList.Count)
  16. {
  17. throw new Exception("An error occured");
  18. }
  19. if (fromReferenceList.Count == 0)
  20. {
  21. return;
  22. }
  23. AssetDatabase.SaveAssets();
  24. EditorSceneManager.SaveOpenScenes();
  25. SerializationMode userSerializationMode = EditorSettings.serializationMode;
  26. EditorSettings.serializationMode = SerializationMode.ForceText;
  27. List<string> pathList = GetAllFilePath();
  28. for (int i = 0; i < pathList.Count; i++)
  29. {
  30. EditorUtility.DisplayProgressBar("Changing reference", Path.GetFileNameWithoutExtension(pathList[i]), (i + 1)/(float) pathList.Count);
  31. string originText = File.ReadAllText(pathList[i]);
  32. string newText = originText;
  33. for (int j = 0; j < fromReferenceList.Count; j++)
  34. {
  35. newText = newText.Replace(fromReferenceList[j], toReferenceList[j]);
  36. }
  37. if (newText != originText)
  38. {
  39. File.WriteAllText(pathList[i], newText);
  40. }
  41. }
  42. EditorUtility.ClearProgressBar();
  43. EditorSettings.serializationMode = userSerializationMode;
  44. AssetDatabase.Refresh();
  45. }
  46. public static void ChangeAllReference()
  47. {
  48. if (!EditorUtility.DisplayDialog("Warning", "Are you sure you want to set all the references?", "Go ahead", "Cancel"))
  49. {
  50. return;
  51. }
  52. List<string> fromReferenceList = new List<string>();
  53. List<string> toReferenceList = new List<string>();
  54. List<string> itemList = ReferenceTable.ReadAllLine();
  55. for (int i = 0; i < itemList.Count; i++)
  56. {
  57. itemList[i] = itemList[i].TrimEnd((char)13);
  58. string atlasReference = itemList[i].Split('|')[0];
  59. string sourceReference = itemList[i].Split('|')[1];
  60. if (fromReferenceList.Contains(sourceReference))
  61. {
  62. int index = fromReferenceList.IndexOf(sourceReference);
  63. Debug.LogWarning($"Repeat sprite detected {ReferenceToFakePath(atlasReference)} {ReferenceToFakePath(toReferenceList[index])}");
  64. }
  65. fromReferenceList.Add(sourceReference);
  66. toReferenceList.Add(atlasReference);
  67. }
  68. ChangeReference(fromReferenceList, toReferenceList);
  69. }
  70. public static void ResetReference(List<string> pngPathList)
  71. {
  72. if (pngPathList.Count == 0)
  73. {
  74. return;
  75. }
  76. List<string> fromReferenceList = new List<string>();
  77. List<string> toReferenceList = new List<string>();
  78. List<string> itemList = ReferenceTable.ReadAllLine();
  79. foreach (string path in pngPathList)
  80. {
  81. string atlasGUID = AssetDatabase.AssetPathToGUID(path);
  82. for (int i = 0; i < itemList.Count; i++)
  83. {
  84. itemList[i] = itemList[i].TrimEnd((char)13);
  85. string atlasReference = itemList[i].Split('|')[0];
  86. string sourceReference = itemList[i].Split('|')[1];
  87. if (atlasReference.Contains(atlasGUID))
  88. {
  89. fromReferenceList.Add(atlasReference);
  90. toReferenceList.Add(sourceReference);
  91. itemList.RemoveAt(i--);
  92. }
  93. }
  94. }
  95. ChangeReference(fromReferenceList, toReferenceList);
  96. ReferenceTable.WriteAllLine(itemList);
  97. }
  98. public static void ResetAllReference()
  99. {
  100. if (!EditorUtility.DisplayDialog("Warning", "Are you sure you want to reset all the references?", "Go ahead", "Cancel"))
  101. {
  102. return;
  103. }
  104. List<string> fromReferenceList = new List<string>();
  105. List<string> toReferenceList = new List<string>();
  106. List<string> itemList = ReferenceTable.ReadAllLine();
  107. for (int i = 0; i < itemList.Count; i++)
  108. {
  109. itemList[i] = itemList[i].TrimEnd((char)13);
  110. string atlasReference = itemList[i].Split('|')[0];
  111. string sourceReference = itemList[i].Split('|')[1];
  112. fromReferenceList.Add(atlasReference);
  113. toReferenceList.Add(sourceReference);
  114. }
  115. ChangeReference(fromReferenceList, toReferenceList);
  116. }
  117. public static void FindReference(string path)
  118. {
  119. SerializationMode userSerializationMode = EditorSettings.serializationMode;
  120. EditorSettings.serializationMode = SerializationMode.ForceText;
  121. string guid = AssetDatabase.AssetPathToGUID(path);
  122. List<string> pathList = GetAllFilePath();
  123. for (int i = 0; i < pathList.Count; i++)
  124. {
  125. EditorUtility.DisplayProgressBar("Searching", pathList[i], (i + 1)/(float) pathList.Count);
  126. int count = Regex.Matches(File.ReadAllText(pathList[i]), guid).Count;
  127. if (count > 0)
  128. {
  129. Debug.LogWarning($"{count}处引用 {pathList[i].GetRelativePath()}");
  130. }
  131. }
  132. EditorUtility.ClearProgressBar();
  133. EditorSettings.serializationMode = userSerializationMode;
  134. }
  135. public static List<string> GetAllFilePath()
  136. {
  137. List<string> searchExtension = new List<string>()
  138. {
  139. ".asset",
  140. ".anim",
  141. ".unity",
  142. ".prefab"
  143. };
  144. List<string> pathList = Directory.GetFiles(Application.dataPath, "*.*", SearchOption.AllDirectories).Where
  145. (
  146. path =>
  147. {
  148. string extension = Path.GetExtension(path);
  149. if (searchExtension.Contains(extension))
  150. {
  151. return true;
  152. }
  153. else
  154. {
  155. return false;
  156. }
  157. }
  158. )
  159. .ToList();
  160. return pathList;
  161. }
  162. public static string ReferenceToFakePath(string reference)
  163. {
  164. string guid = ReferenceToGUID(reference);
  165. string fileId = ReferenceToFileId(reference);
  166. int spriteIndex = (int.Parse(fileId) - 21300000)/2;
  167. string atlasPath = AssetDatabase.GUIDToAssetPath(guid);
  168. TextureImporter textureImporter = (TextureImporter) AssetImporter.GetAtPath(atlasPath);
  169. return $"{atlasPath}/{textureImporter.spritesheet[spriteIndex].name}";
  170. }
  171. public static string ReferenceToGUID(string reference)
  172. {
  173. return reference.Split(',')[1].Split(' ')[2];
  174. }
  175. public static string ReferenceToFileId(string reference)
  176. {
  177. return reference.Split(',')[0].Split(' ')[1];
  178. }
  179. public static List<string> GetChildPathList(string path)
  180. {
  181. List<string> pathList = new List<string>();
  182. List<string> itemList = ReferenceTable.ReadAllLine();
  183. string atlasGUID = AssetDatabase.AssetPathToGUID(path);
  184. for (int i = 0; i < itemList.Count; i++)
  185. {
  186. itemList[i] = itemList[i].TrimEnd((char) 13);
  187. string atlasReference = itemList[i].Split('|')[0];
  188. string sourceReference = itemList[i].Split('|')[1];
  189. if (atlasReference.Contains(atlasGUID))
  190. {
  191. pathList.Add(AssetDatabase.GUIDToAssetPath(ReferenceToGUID(sourceReference)));
  192. }
  193. }
  194. return pathList;
  195. }
  196. }
  197. }