LabelUtility.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. namespace labelUtility
  2. {
  3. #if UNITY_EDITOR
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Xml;
  12. using UnityEditor;
  13. using UnityEngine;
  14. using UnityEngine.UI;
  15. [Serializable]
  16. public class LabelSet
  17. {
  18. public string Name;
  19. public string LabelScriptPath;
  20. public string LabelScriptName;
  21. public string LabePrefix = "public static string";
  22. public TextAsset LabelScript;
  23. public string ComponentScriptPath;
  24. public string ComponentScriptName;
  25. public string ComponentPrefix = "private";
  26. public TextAsset ComponentScript;
  27. public List<TextAsset> Languages; //todo 需要更新为LanguageXml
  28. public List<GameObject> Prefabs;
  29. public bool FoldOut;
  30. public float TotalHeight;
  31. public List<ComponentPurview> ComponentPurviews = new List<ComponentPurview>();
  32. }
  33. public enum ComponentPurview
  34. {
  35. Text,
  36. Button,
  37. Transform,
  38. RectTransform,
  39. SpriteRenderer,
  40. }
  41. public class LabelUtility : MonoBehaviour
  42. {
  43. #region Config
  44. public List<LabelSet> LabelSets;
  45. public static string StartMark = "//StartMark-Used by LabelUtility-Do not remove"; //todo 更新
  46. public static string EndMark = "//EndMark-Used by LabelUtility-Do not remove"; //todo 更新
  47. public static string Prefix = "public static string ";
  48. #endregion
  49. public static void CreateLabelScript(LabelSet labelSet)
  50. {
  51. if (!EditorUtility.DisplayDialog("注意", "新建LabelScript?", "确定", "取消"))
  52. {
  53. return;
  54. }
  55. labelSet.LabelScript = CreateScript(labelSet.LabelScriptName, labelSet.LabelScriptPath);
  56. }
  57. public static void CreateComponentScript(LabelSet labelSet)
  58. {
  59. if (!EditorUtility.DisplayDialog("注意", "新建ComponentScript?", "确定", "取消"))
  60. {
  61. return;
  62. }
  63. labelSet.ComponentScript = CreateScript(labelSet.ComponentScriptName, labelSet.ComponentScriptPath);
  64. }
  65. private static TextAsset CreateScript(string scriptName, string scriptPath)
  66. {
  67. string directory = scriptPath.TrimEnd('/', '\\') + "/";
  68. if (directory.Length < 6 || directory.Substring(0, 6).ToLower() != "assets")
  69. {
  70. throw new Exception("ScripPath必须位置Assets目录内");
  71. }
  72. if (!Directory.Exists(directory))
  73. {
  74. throw new Exception("文件夹不存在");
  75. }
  76. if (string.IsNullOrEmpty(scriptName) || scriptName.Any(Path.GetInvalidFileNameChars().Contains))
  77. {
  78. throw new Exception("ScripName包含无效字符");
  79. }
  80. string fullPath = $"{scriptPath}/{scriptName}.cs";
  81. if (File.Exists(fullPath))
  82. {
  83. throw new Exception($"已经存在一个 {fullPath}");
  84. }
  85. string scriptContent = $"public class {scriptName}\r\n{{\r\n\t#region Config\r\n\r\n\t{StartMark}\r\n\t{EndMark}\r\n\r\n\t#endregion\r\n}}";
  86. File.WriteAllText(fullPath, scriptContent);
  87. AssetDatabase.Refresh();
  88. return AssetDatabase.LoadAssetAtPath<TextAsset>(fullPath);
  89. }
  90. public static void CreateLabelFromPrefab(LabelSet labelSet) //todo 更新
  91. {
  92. if (!EditorUtility.DisplayDialog("注意", "重新生成Prefab Label?", "确定", "取消"))
  93. {
  94. return;
  95. }
  96. List<string> labels = new List<string>();
  97. List<Transform> transforms = GetAllTransformFromPrefab(labelSet.Prefabs);
  98. foreach (var transform in transforms)
  99. {
  100. labels.Add($"{labelSet.LabePrefix} {transform.name} = \"{transform.name}\";");
  101. }
  102. InsertLineToScript(labelSet.LabelScript, labels);
  103. }
  104. public static void CreateLabelFromLanguageXml(LabelSet labelSet) //todo 更新
  105. {
  106. if (!EditorUtility.DisplayDialog("注意", "重新生成LanguageXml Label?", "确定", "取消"))
  107. {
  108. return;
  109. }
  110. List<string> labels = new List<string>();
  111. foreach (var language in labelSet.Languages)
  112. {
  113. XmlDocument document = new XmlDocument();
  114. document.LoadXml(language.text);
  115. XmlNodeList childNodes = document.SelectSingleNode("lan").ChildNodes;
  116. for (int i = 0; i < childNodes.Count; i++)
  117. {
  118. labels.AddRange(GetAllLabelFromXmlNode(childNodes[i], labelSet));
  119. }
  120. }
  121. InsertLineToScript(labelSet.LabelScript, labels);
  122. }
  123. private static List<string> GetAllLabelFromXmlNode(XmlNode node, LabelSet labelSet) //todo 增加
  124. {
  125. List<string> labels = new List<string>();
  126. for (int i = 0; i < node.ChildNodes.Count; i++)
  127. {
  128. XmlNode childNode = node.ChildNodes[i];
  129. string label;
  130. if (i == 0)
  131. {
  132. label = $"\t{labelSet.LabePrefix} {node.Name} = \"{node.Name}\";";
  133. labels.Add(label);
  134. }
  135. label = $"\t{labelSet.LabePrefix} {node.Name}{LanguageLabel.LanguagePageSeperator}{childNode.Name} = \"{node.Name}{LanguageLabel.LanguagePageSeperator}{childNode.Name}\";";
  136. labels.Add(label);
  137. }
  138. return labels;
  139. }
  140. public static void CreateComponentsFromPrefab(LabelSet labelSet)
  141. {
  142. if (!EditorUtility.DisplayDialog("注意", "重新生成PrefabComponent?", "确定", "取消"))
  143. {
  144. return;
  145. }
  146. List<string> textNames = new List<string>();
  147. List<string> buttonNames = new List<string>();
  148. List<string> transformNames = new List<string>();
  149. List<string> rectTransformNames = new List<string>();
  150. List<string> spriteRendererNames = new List<string>();
  151. List<Transform> transforms = GetAllTransformFromPrefab(labelSet.Prefabs);
  152. foreach (var transform in transforms)
  153. {
  154. if (labelSet.ComponentPurviews.Contains(ComponentPurview.Text) && transform.GetComponent<Text>() != null)
  155. {
  156. textNames.Add($"{labelSet.ComponentPrefix} Text {transform.name};");
  157. }
  158. if (labelSet.ComponentPurviews.Contains(ComponentPurview.Button) && transform.GetComponent<Button>() != null)
  159. {
  160. buttonNames.Add($"{labelSet.ComponentPrefix} Button {transform.name};");
  161. }
  162. if (labelSet.ComponentPurviews.Contains(ComponentPurview.Transform) && transform.GetComponent<Transform>() != null)
  163. {
  164. transformNames.Add($"{labelSet.ComponentPrefix} Transform {transform.name};");
  165. }
  166. if (labelSet.ComponentPurviews.Contains(ComponentPurview.RectTransform) && transform.GetComponent<RectTransform>() != null)
  167. {
  168. rectTransformNames.Add($"{labelSet.ComponentPrefix} RectTransform {transform.name};");
  169. }
  170. if (labelSet.ComponentPurviews.Contains(ComponentPurview.SpriteRenderer) && transform.GetComponent<SpriteRenderer>() != null)
  171. {
  172. spriteRendererNames.Add($"{labelSet.ComponentPrefix} SpriteRenderer {transform.name};");
  173. }
  174. }
  175. List<string> components = new List<string>();
  176. components.AddRange(textNames);
  177. components.AddRange(buttonNames);
  178. components.AddRange(transformNames);
  179. components.AddRange(rectTransformNames);
  180. components.AddRange(spriteRendererNames);
  181. InsertLineToScript(labelSet.ComponentScript, components);
  182. }
  183. private static void InsertLineToScript(TextAsset textAsset, List<string> insertLines)
  184. {
  185. int? startMarkLineIndex = null;
  186. int? endMarkLineIndex = null;
  187. List<string> strings = GetScriptContentAfterClearMarks(textAsset, ref startMarkLineIndex, ref endMarkLineIndex);
  188. int prefixIndex = strings[startMarkLineIndex.Value].IndexOf("//");
  189. string prefix = strings[startMarkLineIndex.Value].Substring(0, prefixIndex);
  190. for (int i = 0; i < insertLines.Count; i++)
  191. {
  192. strings.Insert(startMarkLineIndex.Value + 1, prefix + insertLines[i]);
  193. startMarkLineIndex++;
  194. }
  195. StringBuilder stringBuilder = new StringBuilder();
  196. for (int i = 0; i < strings.Count; i++)
  197. {
  198. stringBuilder.AppendLine(strings[i]);
  199. }
  200. string content = stringBuilder.ToString();
  201. content = content.Substring(0, content.Length - 2);
  202. File.WriteAllText(AssetDatabase.GetAssetPath(textAsset), content);
  203. AssetDatabase.Refresh();
  204. }
  205. private static List<string> GetScriptContentAfterClearMarks(TextAsset textAsset, ref int? startMarkLineIndex, ref int? endMarkLineIndex)
  206. {
  207. List<string> strings = textAsset.text.Split(new[] { "\r\n" }, StringSplitOptions.None).ToList();
  208. //List<string> currentDefinedNames = new List<string>();
  209. for (int i = 0; i < strings.Count; i++)
  210. {
  211. if (strings[i].Contains(StartMark))
  212. {
  213. startMarkLineIndex = i;
  214. }
  215. else if (strings[i].Contains(EndMark))
  216. {
  217. endMarkLineIndex = i;
  218. }
  219. //if (startMarkLineIndex != null)
  220. //{
  221. // if (endMarkLineIndex != null)
  222. // {
  223. // if (i > startMarkLineIndex.Value && i < endMarkLineIndex.Value)
  224. // {
  225. // currentDefinedNames.Add(GetDefinedName(strings[i]));
  226. // }
  227. // }
  228. // else
  229. // {
  230. // if (i > startMarkLineIndex.Value)
  231. // {
  232. // currentDefinedNames.Add(GetDefinedName(strings[i]));
  233. // }
  234. // }
  235. //}
  236. }
  237. int definedCount = endMarkLineIndex.Value - startMarkLineIndex.Value - 1;
  238. for (int i = 0; i < definedCount; i++)
  239. {
  240. strings.RemoveAt(startMarkLineIndex.Value + 1);
  241. }
  242. return strings;
  243. }
  244. private static List<Transform> GetAllTransformFromPrefab(List<GameObject> prefabs)
  245. {
  246. List<Transform> transforms = new List<Transform>();
  247. for (int i = 0; i < prefabs.Count; i++)
  248. {
  249. transforms.AddRange(prefabs[i].GetComponentsInChildren<Transform>(true));
  250. }
  251. return transforms;
  252. }
  253. }
  254. #endif
  255. }