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 TextAsset ComponentScript; public List Languages; public List Prefabs; public bool FoldOut; public float TotalHeight; public List ComponentPurviews = new List(); } public enum ComponentPurview { Text, Button, Transform, RectTransform, SpriteRenderer, } public class LabelUtility : MonoBehaviour { #region Config public List LabelSets; public static string StartMark = "//StartMarkMark-LabelUtility使用-勿删"; public static string EndMark = "//EndMarkMark-LabelUtility使用-勿删"; public static string Prefix = "public static string "; public static string LanguagePageSeperator = "__"; #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) { if (!EditorUtility.DisplayDialog("注意", "重新生成PrefabLabel?", "确定", "取消")) { 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 CreateComponentsFromPrefab(LabelSet labelSet) { if (!EditorUtility.DisplayDialog("注意", "重新生成PrefabComponent?", "确定", "取消")) { return; } List textNames = 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($"{labelSet.ComponentPrefix} Text {transform.name};"); } if (labelSet.ComponentPurviews.Contains(ComponentPurview.Button) && transform.GetComponent