ReferenceManager.cs 7.4 KB

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