|
@@ -0,0 +1,260 @@
|
|
|
+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<TextAsset> Languages;
|
|
|
+ public List<GameObject> Prefabs;
|
|
|
+
|
|
|
+ public bool FoldOut;
|
|
|
+ public float TotalHeight;
|
|
|
+ public List<ComponentPurview> ComponentPurviews = new List<ComponentPurview>();
|
|
|
+}
|
|
|
+
|
|
|
+public enum ComponentPurview
|
|
|
+{
|
|
|
+ Text,
|
|
|
+ Button,
|
|
|
+ Transform,
|
|
|
+ RectTransform,
|
|
|
+ SpriteRenderer,
|
|
|
+}
|
|
|
+
|
|
|
+public class LabelUtility : MonoBehaviour
|
|
|
+{
|
|
|
+ #region Config
|
|
|
+
|
|
|
+ public List<LabelSet> 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<TextAsset>(fullPath);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void CreateLabelFromPrefab(LabelSet labelSet)
|
|
|
+ {
|
|
|
+ if (!EditorUtility.DisplayDialog("注意", "重新生成PrefabLabel?", "确定", "取消"))
|
|
|
+ {
|
|
|
+ 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 CreateComponentsFromPrefab(LabelSet labelSet)
|
|
|
+ {
|
|
|
+ if (!EditorUtility.DisplayDialog("注意", "重新生成PrefabComponent?", "确定", "取消"))
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<string> textNames = 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($"{labelSet.ComponentPrefix} Text {transform.name};");
|
|
|
+ }
|
|
|
+ if (labelSet.ComponentPurviews.Contains(ComponentPurview.Button) && transform.GetComponent<Button>() != null)
|
|
|
+ {
|
|
|
+ buttonNames.Add($"{labelSet.ComponentPrefix} Button {transform.name};");
|
|
|
+ }
|
|
|
+ if (labelSet.ComponentPurviews.Contains(ComponentPurview.Transform) && transform.GetComponent<Transform>() != null)
|
|
|
+ {
|
|
|
+ transformNames.Add($"{labelSet.ComponentPrefix} Transform {transform.name};");
|
|
|
+ }
|
|
|
+ if (labelSet.ComponentPurviews.Contains(ComponentPurview.RectTransform) && transform.GetComponent<RectTransform>() != null)
|
|
|
+ {
|
|
|
+ rectTransformNames.Add($"{labelSet.ComponentPrefix} RectTransform {transform.name};");
|
|
|
+ }
|
|
|
+ if (labelSet.ComponentPurviews.Contains(ComponentPurview.SpriteRenderer) && transform.GetComponent<SpriteRenderer>() != null)
|
|
|
+ {
|
|
|
+ spriteRendererNames.Add($"{labelSet.ComponentPrefix} SpriteRenderer {transform.name};");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<string> components = new List<string>();
|
|
|
+ components.AddRange(textNames);
|
|
|
+ components.AddRange(buttonNames);
|
|
|
+ components.AddRange(transformNames);
|
|
|
+ components.AddRange(rectTransformNames);
|
|
|
+ components.AddRange(spriteRendererNames);
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/*To be delete
|
|
|
+ //private static string GetDefinedName(string str)
|
|
|
+ //{
|
|
|
+ // return Regex.Match(str, "(?<=string)[^=]+(?=\\=)").Value.Trim();
|
|
|
+ //}
|
|
|
+*/
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+}
|