LabelUtility.cs 12 KB

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