namespace labelUtility { #if UNITY_EDITOR using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Xml; using UnityEditor; using UnityEngine; using UnityEngine.UI; [Serializable] public class LabelSet { public string Name; public string LabelScriptPath; public string LabelScriptName; public string LabePrefix = "public static string"; public TextAsset LabelScript; public string ComponentScriptPath; public string ComponentScriptName; public string ComponentPrefix = "private"; public string NameExcludeString; public TextAsset ComponentScript; public List Languages; //todo 需要更新为LanguageXml public List Prefabs; public bool FoldOut; public float TotalHeight; public List ComponentPurviews = new List(); } public enum ComponentPurview //todo 更新 { Text, Slider, Image, Button, Transform, RectTransform, SpriteRenderer, } public class LabelUtility : MonoBehaviour { #region Config public List LabelSets; public static string StartMark = "//StartMark-Used by LabelUtility-Do not remove"; //todo 更新 public static string EndMark = "//EndMark-Used by LabelUtility-Do not remove"; //todo 更新 public static string Prefix = "public static string "; #endregion public static void CreateLabelScript(LabelSet labelSet) { if (!EditorUtility.DisplayDialog("注意", "新建LabelScript?", "确定", "取消")) { return; } labelSet.LabelScript = CreateScript(labelSet.LabelScriptName, labelSet.LabelScriptPath); } public static void CreateComponentScript(LabelSet labelSet) { if (!EditorUtility.DisplayDialog("注意", "新建ComponentScript?", "确定", "取消")) { return; } labelSet.ComponentScript = CreateScript(labelSet.ComponentScriptName, labelSet.ComponentScriptPath); } private static TextAsset CreateScript(string scriptName, string scriptPath) { string directory = scriptPath.TrimEnd('/', '\\') + "/"; if (directory.Length < 6 || directory.Substring(0, 6).ToLower() != "assets") { throw new Exception("ScripPath必须位置Assets目录内"); } if (!Directory.Exists(directory)) { throw new Exception("文件夹不存在"); } if (string.IsNullOrEmpty(scriptName) || scriptName.Any(Path.GetInvalidFileNameChars().Contains)) { throw new Exception("ScripName包含无效字符"); } string fullPath = $"{scriptPath}/{scriptName}.cs"; if (File.Exists(fullPath)) { throw new Exception($"已经存在一个 {fullPath}"); } 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}}"; File.WriteAllText(fullPath, scriptContent); AssetDatabase.Refresh(); return AssetDatabase.LoadAssetAtPath(fullPath); } public static void CreateLabelFromPrefab(LabelSet labelSet) //todo 更新 { if (!EditorUtility.DisplayDialog("注意", "重新生成Prefab Label?", "确定", "取消")) { return; } List labels = new List(); List transforms = GetAllTransformFromPrefab(labelSet.Prefabs); foreach (var transform in transforms) { labels.Add($"{labelSet.LabePrefix} {transform.name} = \"{transform.name}\";"); } InsertLineToScript(labelSet.LabelScript, labels); } public static void CreateLabelFromLanguageXml(LabelSet labelSet) //todo 更新 { if (!EditorUtility.DisplayDialog("注意", "重新生成LanguageXml Label?", "确定", "取消")) { return; } List labels = new List(); foreach (var language in labelSet.Languages) { XmlDocument document = new XmlDocument(); document.LoadXml(language.text); XmlNodeList childNodes = document.SelectSingleNode("lan").ChildNodes; for (int i = 0; i < childNodes.Count; i++) { labels.AddRange(GetAllLabelFromXmlNode(childNodes[i], labelSet)); } } InsertLineToScript(labelSet.LabelScript, labels); } private static List GetAllLabelFromXmlNode(XmlNode node, LabelSet labelSet) //todo 增加 { List labels = new List(); for (int i = 0; i < node.ChildNodes.Count; i++) { XmlNode childNode = node.ChildNodes[i]; string label; if (i == 0) { label = $"\t{labelSet.LabePrefix} {node.Name} = \"{node.Name}\";"; labels.Add(label); } label = $"\t{labelSet.LabePrefix} {node.Name}{LanguageLabel.LanguagePageSeperator}{childNode.Name} = \"{node.Name}{LanguageLabel.LanguagePageSeperator}{childNode.Name}\";"; labels.Add(label); } return labels; } public static void CreateComponentsFromPrefab(LabelSet labelSet) { if (!EditorUtility.DisplayDialog("注意", "重新生成PrefabComponent?", "确定", "取消")) { return; } List names = new List(); List textNames = new List(); List sliderNames = new List(); List imageNames = new List(); List buttonNames = new List(); List transformNames = new List(); List rectTransformNames = new List(); List spriteRendererNames = new List(); List transforms = GetAllTransformFromPrefab(labelSet.Prefabs); foreach (var transform in transforms) { if (labelSet.ComponentPurviews.Contains(ComponentPurview.Text) && transform.GetComponent() != null) { textNames.Add($"Text {transform.name}"); } if (labelSet.ComponentPurviews.Contains(ComponentPurview.Slider) && transform.GetComponent() != null) { sliderNames.Add($"Slider {transform.name}"); } if (labelSet.ComponentPurviews.Contains(ComponentPurview.Image) && transform.GetComponent() != null) { sliderNames.Add($"Image {transform.name}"); } if (labelSet.ComponentPurviews.Contains(ComponentPurview.Button) && transform.GetComponent