LabelUtility.cs 8.4 KB

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