LabelUtility.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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;
  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 = "//StartMarkMark-LabelUtility使用-勿删";
  46. public static string EndMark = "//EndMarkMark-LabelUtility使用-勿删";
  47. public static string Prefix = "public static string ";
  48. public static string LanguagePageSeperator = "__";
  49. #endregion
  50. public static void CreateLabelScript(LabelSet labelSet)
  51. {
  52. if (!EditorUtility.DisplayDialog("注意", "新建LabelScript?", "确定", "取消"))
  53. {
  54. return;
  55. }
  56. labelSet.LabelScript = CreateScript(labelSet.LabelScriptName, labelSet.LabelScriptPath);
  57. }
  58. public static void CreateComponentScript(LabelSet labelSet)
  59. {
  60. if (!EditorUtility.DisplayDialog("注意", "新建ComponentScript?", "确定", "取消"))
  61. {
  62. return;
  63. }
  64. labelSet.ComponentScript = CreateScript(labelSet.ComponentScriptName, labelSet.ComponentScriptPath);
  65. }
  66. private static TextAsset CreateScript(string scriptName, string scriptPath)
  67. {
  68. string directory = scriptPath.TrimEnd('/', '\\') + "/";
  69. if (directory.Length < 6 || directory.Substring(0, 6).ToLower() != "assets")
  70. {
  71. throw new Exception("ScripPath必须位置Assets目录内");
  72. }
  73. if (!Directory.Exists(directory))
  74. {
  75. throw new Exception("文件夹不存在");
  76. }
  77. if (string.IsNullOrEmpty(scriptName) || scriptName.Any(Path.GetInvalidFileNameChars().Contains))
  78. {
  79. throw new Exception("ScripName包含无效字符");
  80. }
  81. string fullPath = $"{scriptPath}/{scriptName}.cs";
  82. if (File.Exists(fullPath))
  83. {
  84. throw new Exception($"已经存在一个 {fullPath}");
  85. }
  86. 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}}";
  87. File.WriteAllText(fullPath, scriptContent);
  88. AssetDatabase.Refresh();
  89. return AssetDatabase.LoadAssetAtPath<TextAsset>(fullPath);
  90. }
  91. public static void CreateLabelFromPrefab(LabelSet labelSet)
  92. {
  93. if (!EditorUtility.DisplayDialog("注意", "重新生成PrefabLabel?", "确定", "取消"))
  94. {
  95. return;
  96. }
  97. List<string> labels = new List<string>();
  98. List<Transform> transforms = GetAllTransformFromPrefab(labelSet.Prefabs);
  99. foreach (var transform in transforms)
  100. {
  101. labels.Add($"{labelSet.LabePrefix} {transform.name} = \"{transform.name}\";");
  102. }
  103. InsertLineToScript(labelSet.LabelScript, labels);
  104. }
  105. public static void CreateComponentsFromPrefab(LabelSet labelSet)
  106. {
  107. if (!EditorUtility.DisplayDialog("注意", "重新生成PrefabComponent?", "确定", "取消"))
  108. {
  109. return;
  110. }
  111. List<string> textNames = new List<string>();
  112. List<string> buttonNames = new List<string>();
  113. List<string> transformNames = new List<string>();
  114. List<string> rectTransformNames = new List<string>();
  115. List<string> spriteRendererNames = new List<string>();
  116. List<Transform> transforms = GetAllTransformFromPrefab(labelSet.Prefabs);
  117. foreach (var transform in transforms)
  118. {
  119. if (labelSet.ComponentPurviews.Contains(ComponentPurview.Text) && transform.GetComponent<Text>() != null)
  120. {
  121. textNames.Add($"{labelSet.ComponentPrefix} Text {transform.name};");
  122. }
  123. if (labelSet.ComponentPurviews.Contains(ComponentPurview.Button) && transform.GetComponent<Button>() != null)
  124. {
  125. buttonNames.Add($"{labelSet.ComponentPrefix} Button {transform.name};");
  126. }
  127. if (labelSet.ComponentPurviews.Contains(ComponentPurview.Transform) && transform.GetComponent<Transform>() != null)
  128. {
  129. transformNames.Add($"{labelSet.ComponentPrefix} Transform {transform.name};");
  130. }
  131. if (labelSet.ComponentPurviews.Contains(ComponentPurview.RectTransform) && transform.GetComponent<RectTransform>() != null)
  132. {
  133. rectTransformNames.Add($"{labelSet.ComponentPrefix} RectTransform {transform.name};");
  134. }
  135. if (labelSet.ComponentPurviews.Contains(ComponentPurview.SpriteRenderer) && transform.GetComponent<SpriteRenderer>() != null)
  136. {
  137. spriteRendererNames.Add($"{labelSet.ComponentPrefix} SpriteRenderer {transform.name};");
  138. }
  139. }
  140. List<string> components = new List<string>();
  141. components.AddRange(textNames);
  142. components.AddRange(buttonNames);
  143. components.AddRange(transformNames);
  144. components.AddRange(rectTransformNames);
  145. components.AddRange(spriteRendererNames);
  146. InsertLineToScript(labelSet.ComponentScript, components);
  147. }
  148. private static void InsertLineToScript(TextAsset textAsset, List<string> insertLines)
  149. {
  150. int? startMarkLineIndex = null;
  151. int? endMarkLineIndex = null;
  152. List<string> strings = GetScriptContentAfterClearMarks(textAsset, ref startMarkLineIndex, ref endMarkLineIndex);
  153. int prefixIndex = strings[startMarkLineIndex.Value].IndexOf("//");
  154. string prefix = strings[startMarkLineIndex.Value].Substring(0, prefixIndex);
  155. for (int i = 0; i < insertLines.Count; i++)
  156. {
  157. strings.Insert(startMarkLineIndex.Value + 1, prefix + insertLines[i]);
  158. startMarkLineIndex++;
  159. }
  160. StringBuilder stringBuilder = new StringBuilder();
  161. for (int i = 0; i < strings.Count; i++)
  162. {
  163. stringBuilder.AppendLine(strings[i]);
  164. }
  165. string content = stringBuilder.ToString();
  166. content = content.Substring(0, content.Length - 2);
  167. File.WriteAllText(AssetDatabase.GetAssetPath(textAsset), content);
  168. AssetDatabase.Refresh();
  169. }
  170. private static List<string> GetScriptContentAfterClearMarks(TextAsset textAsset, ref int? startMarkLineIndex, ref int? endMarkLineIndex)
  171. {
  172. List<string> strings = textAsset.text.Split(new[] { "\r\n" }, StringSplitOptions.None).ToList();
  173. //List<string> currentDefinedNames = new List<string>();
  174. for (int i = 0; i < strings.Count; i++)
  175. {
  176. if (strings[i].Contains(StartMark))
  177. {
  178. startMarkLineIndex = i;
  179. }
  180. else if (strings[i].Contains(EndMark))
  181. {
  182. endMarkLineIndex = i;
  183. }
  184. //if (startMarkLineIndex != null)
  185. //{
  186. // if (endMarkLineIndex != null)
  187. // {
  188. // if (i > startMarkLineIndex.Value && i < endMarkLineIndex.Value)
  189. // {
  190. // currentDefinedNames.Add(GetDefinedName(strings[i]));
  191. // }
  192. // }
  193. // else
  194. // {
  195. // if (i > startMarkLineIndex.Value)
  196. // {
  197. // currentDefinedNames.Add(GetDefinedName(strings[i]));
  198. // }
  199. // }
  200. //}
  201. }
  202. int definedCount = endMarkLineIndex.Value - startMarkLineIndex.Value - 1;
  203. for (int i = 0; i < definedCount; i++)
  204. {
  205. strings.RemoveAt(startMarkLineIndex.Value + 1);
  206. }
  207. return strings;
  208. }
  209. private static List<Transform> GetAllTransformFromPrefab(List<GameObject> prefabs)
  210. {
  211. List<Transform> transforms = new List<Transform>();
  212. for (int i = 0; i < prefabs.Count; i++)
  213. {
  214. transforms.AddRange(prefabs[i].GetComponentsInChildren<Transform>(true));
  215. }
  216. return transforms;
  217. }
  218. }
  219. /*To be delete
  220. //private static string GetDefinedName(string str)
  221. //{
  222. // return Regex.Match(str, "(?<=string)[^=]+(?=\\=)").Value.Trim();
  223. //}
  224. */
  225. #endif
  226. }