123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- 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<TextAsset> Languages; //todo 需要更新为LanguageXml
- public List<GameObject> Prefabs;
- public bool FoldOut;
- public float TotalHeight;
- public List<ComponentPurview> ComponentPurviews = new List<ComponentPurview>();
- }
- public enum ComponentPurview //todo 更新
- {
- Text,
- Slider,
- Image,
- Button,
- Transform,
- RectTransform,
- SpriteRenderer,
- }
- public class LabelUtility : MonoBehaviour
- {
- #region Config
- public List<LabelSet> 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<TextAsset>(fullPath);
- }
- public static void CreateLabelFromPrefab(LabelSet labelSet) //todo 更新
- {
- if (!EditorUtility.DisplayDialog("注意", "重新生成Prefab Label?", "确定", "取消"))
- {
- return;
- }
- List<string> labels = new List<string>();
- List<Transform> 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<string> labels = new List<string>();
- 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<string> GetAllLabelFromXmlNode(XmlNode node, LabelSet labelSet) //todo 增加
- {
- List<string> labels = new List<string>();
- 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<string> names = new List<string>();
- List<string> textNames = new List<string>();
- List<string> sliderNames = new List<string>();
- List<string> imageNames = new List<string>();
- List<string> buttonNames = new List<string>();
- List<string> transformNames = new List<string>();
- List<string> rectTransformNames = new List<string>();
- List<string> spriteRendererNames = new List<string>();
- List<Transform> transforms = GetAllTransformFromPrefab(labelSet.Prefabs);
- foreach (var transform in transforms)
- {
- if (labelSet.ComponentPurviews.Contains(ComponentPurview.Text) && transform.GetComponent<Text>() != null)
- {
- textNames.Add($"Text {transform.name}");
- }
- if (labelSet.ComponentPurviews.Contains(ComponentPurview.Slider) && transform.GetComponent<Slider>() != null)
- {
- sliderNames.Add($"Slider {transform.name}");
- }
- if (labelSet.ComponentPurviews.Contains(ComponentPurview.Image) && transform.GetComponent<Image>() != null)
- {
- sliderNames.Add($"Image {transform.name}");
- }
- if (labelSet.ComponentPurviews.Contains(ComponentPurview.Button) && transform.GetComponent<Button>() != null)
- {
- buttonNames.Add($"Button {transform.name}");
- }
- if (labelSet.ComponentPurviews.Contains(ComponentPurview.Transform) && transform.GetComponent<Transform>() != null)
- {
- transformNames.Add($"Transform {transform.name}");
- }
- if (labelSet.ComponentPurviews.Contains(ComponentPurview.RectTransform) && transform.GetComponent<RectTransform>() != null)
- {
- rectTransformNames.Add($"RectTransform {transform.name}");
- }
- if (labelSet.ComponentPurviews.Contains(ComponentPurview.SpriteRenderer) && transform.GetComponent<SpriteRenderer>() != null)
- {
- spriteRendererNames.Add($"SpriteRenderer {transform.name}");
- }
- }
- names.AddRange(textNames);
- names.AddRange(sliderNames);
- names.AddRange(imageNames);
- names.AddRange(buttonNames);
- names.AddRange(transformNames);
- names.AddRange(rectTransformNames);
- names.AddRange(spriteRendererNames);
- List<string> components = new List<string>();
- for (int i = 0; i < names.Count; i++)
- {
- string name = string.IsNullOrEmpty(labelSet.NameExcludeString) ? names[i] : names[i].Replace(labelSet.NameExcludeString, "");
- components.Add($"{labelSet.ComponentPrefix} {name};");
- }
- for (int i = 0; i < names.Count; i++)
- {
- components.Add(names[i].Substring(names[i].IndexOf(' ') + 1));
- }
- InsertLineToScript(labelSet.ComponentScript, components);
- }
- private static void InsertLineToScript(TextAsset textAsset, List<string> insertLines)
- {
- int? startMarkLineIndex = null;
- int? endMarkLineIndex = null;
- List<string> strings = GetScriptContentAfterClearMarks(textAsset, ref startMarkLineIndex, ref endMarkLineIndex);
- int prefixIndex = strings[startMarkLineIndex.Value].IndexOf("//");
- string prefix = strings[startMarkLineIndex.Value].Substring(0, prefixIndex);
- for (int i = 0; i < insertLines.Count; i++)
- {
- strings.Insert(startMarkLineIndex.Value + 1, prefix + insertLines[i]);
- startMarkLineIndex++;
- }
- StringBuilder stringBuilder = new StringBuilder();
- for (int i = 0; i < strings.Count; i++)
- {
- stringBuilder.AppendLine(strings[i]);
- }
- string content = stringBuilder.ToString();
- content = content.Substring(0, content.Length - 2);
- File.WriteAllText(AssetDatabase.GetAssetPath(textAsset), content);
- AssetDatabase.Refresh();
- }
- private static List<string> GetScriptContentAfterClearMarks(TextAsset textAsset, ref int? startMarkLineIndex, ref int? endMarkLineIndex)
- {
- List<string> strings = textAsset.text.Split(new[] {"\r\n"}, StringSplitOptions.None).ToList();
- //List<string> currentDefinedNames = new List<string>();
- for (int i = 0; i < strings.Count; i++)
- {
- if (strings[i].Contains(StartMark))
- {
- startMarkLineIndex = i;
- }
- else if (strings[i].Contains(EndMark))
- {
- endMarkLineIndex = i;
- }
- //if (startMarkLineIndex != null)
- //{
- // if (endMarkLineIndex != null)
- // {
- // if (i > startMarkLineIndex.Value && i < endMarkLineIndex.Value)
- // {
- // currentDefinedNames.Add(GetDefinedName(strings[i]));
- // }
- // }
- // else
- // {
- // if (i > startMarkLineIndex.Value)
- // {
- // currentDefinedNames.Add(GetDefinedName(strings[i]));
- // }
- // }
- //}
- }
- int definedCount = endMarkLineIndex.Value - startMarkLineIndex.Value - 1;
- for (int i = 0; i < definedCount; i++)
- {
- strings.RemoveAt(startMarkLineIndex.Value + 1);
- }
- return strings;
- }
- private static List<Transform> GetAllTransformFromPrefab(List<GameObject> prefabs)
- {
- List<Transform> transforms = new List<Transform>();
- for (int i = 0; i < prefabs.Count; i++)
- {
- transforms.AddRange(prefabs[i].GetComponentsInChildren<Transform>(true));
- }
- return transforms;
- }
- }
- #endif
- }
|