LabelUtility.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Xml;
  8. #if UNITY_EDITOR
  9. using UnityEditor;
  10. #endif
  11. using UnityEngine;
  12. using AtlasUtility;
  13. public class LabelUtility : MonoBehaviour
  14. {
  15. #region Config
  16. public TextAsset Script;
  17. public List<TextAsset> Languages;
  18. public List<GameObject> Prefabs;
  19. private static string Mark = "//Mark";
  20. private static string Prefix = "public static string ";
  21. private static string LanguagePageSeperator = "__";
  22. #endregion
  23. public void ClearLabels()
  24. {
  25. if (EditorUtility.DisplayDialog("注意", "清空所有标签?", "确定", "取消"))
  26. {
  27. string path = AssetDatabase.GetAssetPath(Script);
  28. string fileStr = File.ReadAllText(path);
  29. fileStr = Regex.Replace(fileStr, "(?<=\\{)[^\\}]*(?=\\})", $"\r\n\t{Mark}\r\n");
  30. File.WriteAllText(path, fileStr);
  31. }
  32. }
  33. public void CreateLabelFromPrefabs()
  34. {
  35. if (EditorUtility.DisplayDialog("注意", "新建Prefab标签?", "确定", "取消"))
  36. {
  37. StringBuilder stringBuilder = new StringBuilder(Script.text);
  38. foreach (var prefab in Prefabs)
  39. {
  40. CreateLabel(prefab, stringBuilder);
  41. }
  42. string path = AssetDatabase.GetAssetPath(Script);
  43. File.WriteAllText(path, stringBuilder.ToString());
  44. }
  45. }
  46. private void CreateLabel(GameObject prefab, StringBuilder stringBuilder)
  47. {
  48. Transform[] transforms = prefab.GetComponentsInChildren<Transform>(true);
  49. Match match = Regex.Match(stringBuilder.ToString(), Mark);
  50. if (match.Success)
  51. {
  52. stringBuilder.Replace(Mark, "", match.Index, match.Length);
  53. int insertIndex = match.Index + 2;
  54. for (int i = 0; i < transforms.Length; i++)
  55. {
  56. string insertStr = $"\t{Prefix}{transforms[i].name} = \"{transforms[i].name}\";";
  57. stringBuilder.Insert(insertIndex, insertStr);
  58. insertIndex += insertStr.Length;
  59. if (i < transforms.Length - 1)
  60. {
  61. stringBuilder.Insert(insertIndex, "\r\n");
  62. insertIndex += 2;
  63. }
  64. else if (i == transforms.Length - 1)
  65. {
  66. stringBuilder.Insert(insertIndex, $"\r\n\t{Mark}\r\n");
  67. }
  68. }
  69. }
  70. else
  71. {
  72. throw new Exception("没有找到标记");
  73. }
  74. }
  75. public void CreateLabelFromLanguage()
  76. {
  77. if (EditorUtility.DisplayDialog("注意", "新建Language标签?", "确定", "取消"))
  78. {
  79. StringBuilder stringBuilder = new StringBuilder(Script.text);
  80. foreach (var language in Languages)
  81. {
  82. XmlDocument document = new XmlDocument();
  83. document.LoadXml(language.text);
  84. CreateLabelFromLanguageDocument(document, stringBuilder);
  85. }
  86. string path = AssetDatabase.GetAssetPath(Script);
  87. File.WriteAllText(path, stringBuilder.ToString());
  88. }
  89. }
  90. private void CreateLabelFromLanguageDocument(XmlDocument document, StringBuilder stringBuilder)
  91. {
  92. XmlNodeList childNodes = document.SelectSingleNode("lan").ChildNodes;
  93. for (int i = 0; i < childNodes.Count; i++)
  94. {
  95. CreateLabel(childNodes[i], stringBuilder);
  96. }
  97. }
  98. private void CreateLabel(XmlNode node, StringBuilder stringBuilder)
  99. {
  100. Match match = Regex.Match(stringBuilder.ToString(), Mark);
  101. if (match.Success)
  102. {
  103. stringBuilder.Replace(Mark, "", match.Index, match.Length);
  104. int insertIndex = match.Index + 2;
  105. for (int i = 0; i < node.ChildNodes.Count; i++)
  106. {
  107. XmlNode childNode = node.ChildNodes[i];
  108. string insertStr;
  109. if (i == 0)
  110. {
  111. insertStr = $"\t{Prefix}{node.Name} = \"{node.Name}\";\r\n";
  112. stringBuilder.Insert(insertIndex, insertStr);
  113. insertIndex += insertStr.Length;
  114. }
  115. insertStr = $"\t{Prefix}{node.Name}{LanguagePageSeperator}{childNode.Name} = \"{node.Name}{LanguagePageSeperator}{childNode.Name}\";";
  116. stringBuilder.Insert(insertIndex, insertStr);
  117. insertIndex += insertStr.Length;
  118. if (i < node.ChildNodes.Count - 1)
  119. {
  120. stringBuilder.Insert(insertIndex, "\r\n");
  121. insertIndex += 2;
  122. }
  123. else if (i == node.ChildNodes.Count - 1)
  124. {
  125. stringBuilder.Insert(insertIndex, $"\r\n\t{Mark}\r\n");
  126. }
  127. }
  128. }
  129. else
  130. {
  131. throw new Exception("没有找到标记");
  132. }
  133. }
  134. public static string CombineLanguageLabel(string page, string id)
  135. {
  136. return $"{page}{LanguagePageSeperator}{id}";
  137. }
  138. //public void ReplaceLabels()
  139. //{
  140. // Transform[] transforms = Prefabs[0].GetComponentsInChildren<Transform>(true);
  141. // string[] pathes = Directory.GetFiles(Application.dataPath, "*.cs", SearchOption.AllDirectories);
  142. // foreach (var path in pathes)
  143. // {
  144. // if (path.Contains("PrefabLabel"))
  145. // {
  146. // continue;
  147. // }
  148. // string fileStr = File.ReadAllText(path);
  149. // foreach (var transform in transforms)
  150. // {
  151. // if (transform.name == "Canvas")
  152. // {
  153. // continue;
  154. // }
  155. // fileStr = Regex.Replace(fileStr, $"\"{transform.name}\"", $"PrefabLabel.{transform.name}");
  156. // }
  157. // File.WriteAllText(path, fileStr);
  158. // }
  159. //}
  160. //public void TestReplaceLabels()
  161. //{
  162. // Transform[] transforms = Prefabs[0].GetComponentsInChildren<Transform>(true);
  163. // string path = AssetDatabase.GetAssetPath(TextAsset);
  164. // string fileStr = File.ReadAllText(path);
  165. // foreach (var transform in transforms)
  166. // {
  167. // if (transform.name == "Canvas")
  168. // {
  169. // continue;
  170. // }
  171. // fileStr = Regex.Replace(fileStr, $"\"{transform.name}\"", $"PrefabLabel.{transform.name}");
  172. // }
  173. // File.WriteAllText(path, fileStr);
  174. //}
  175. //public void ReplaceLanguageLabels()
  176. //{
  177. // string[] pathes = Directory.GetFiles(Application.dataPath, "*.cs", SearchOption.AllDirectories);
  178. // foreach (var path in pathes)
  179. // {
  180. // ReplaceLanguageLabel(path);
  181. // }
  182. //}
  183. //public void ReplaceLanguageLabel(string path)
  184. //{
  185. // string fileStr = File.ReadAllText(path);
  186. // string pattern = "(?<=Language\\.GetStr\\()[^\\)]+(?=\\))|(?<=MulLanStr\\()[^\\)]+(?=\\))";
  187. // Match match = Regex.Match(fileStr, pattern);
  188. // while (match.Success)
  189. // {
  190. // string[] strings = match.Value.Replace(" ", "").Split(',');
  191. // if (strings.Length == 1)
  192. // {
  193. // match = match.NextMatch();
  194. // }
  195. // else
  196. // {
  197. // fileStr = fileStr.ReplaceByLength(match.Index, match.Length, $"LanguageLabel.{strings[0].Trim('"')}{LanguagePageSeperator}{strings[1].Trim('"')}");
  198. // match = Regex.Match(fileStr, pattern);
  199. // }
  200. // }
  201. // File.WriteAllText(path, fileStr);
  202. //}
  203. }