LabelUtility.cs 7.6 KB

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