123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Xml;
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
- using UnityEngine;
- using AtlasUtility;
- public class LabelUtility : MonoBehaviour
- {
- #region Config
- public TextAsset Script;
- public List<TextAsset> Languages;
- public List<GameObject> Prefabs;
- private static string Mark = "//Mark";
- private static string Prefix = "public static string ";
- private static string LanguagePageSeperator = "__";
- #endregion
- public void ClearLabels()
- {
- if (EditorUtility.DisplayDialog("注意", "清空所有标签?", "确定", "取消"))
- {
- string path = AssetDatabase.GetAssetPath(Script);
- string fileStr = File.ReadAllText(path);
- fileStr = Regex.Replace(fileStr, "(?<=\\{)[^\\}]*(?=\\})", $"\r\n\t{Mark}\r\n");
- File.WriteAllText(path, fileStr);
- }
- }
- public void CreateLabelFromPrefabs()
- {
- if (EditorUtility.DisplayDialog("注意", "新建Prefab标签?", "确定", "取消"))
- {
- StringBuilder stringBuilder = new StringBuilder(Script.text);
- foreach (var prefab in Prefabs)
- {
- CreateLabel(prefab, stringBuilder);
- }
- string path = AssetDatabase.GetAssetPath(Script);
- File.WriteAllText(path, stringBuilder.ToString());
- }
- }
- private void CreateLabel(GameObject prefab, StringBuilder stringBuilder)
- {
- Transform[] transforms = prefab.GetComponentsInChildren<Transform>(true);
- Match match = Regex.Match(stringBuilder.ToString(), Mark);
- if (match.Success)
- {
- stringBuilder.Replace(Mark, "", match.Index, match.Length);
- int insertIndex = match.Index + 2;
- for (int i = 0; i < transforms.Length; i++)
- {
- string insertStr = $"\t{Prefix}{transforms[i].name} = \"{transforms[i].name}\";";
- stringBuilder.Insert(insertIndex, insertStr);
- insertIndex += insertStr.Length;
- if (i < transforms.Length - 1)
- {
- stringBuilder.Insert(insertIndex, "\r\n");
- insertIndex += 2;
- }
- else if (i == transforms.Length - 1)
- {
- stringBuilder.Insert(insertIndex, $"\r\n\t{Mark}\r\n");
- }
- }
- }
- else
- {
- throw new Exception("没有找到标记");
- }
- }
- public void CreateLabelFromLanguage()
- {
- if (EditorUtility.DisplayDialog("注意", "新建Language标签?", "确定", "取消"))
- {
- StringBuilder stringBuilder = new StringBuilder(Script.text);
- foreach (var language in Languages)
- {
- XmlDocument document = new XmlDocument();
- document.LoadXml(language.text);
- CreateLabelFromLanguageDocument(document, stringBuilder);
- }
- string path = AssetDatabase.GetAssetPath(Script);
- File.WriteAllText(path, stringBuilder.ToString());
- }
- }
- private void CreateLabelFromLanguageDocument(XmlDocument document, StringBuilder stringBuilder)
- {
- XmlNodeList childNodes = document.SelectSingleNode("lan").ChildNodes;
- for (int i = 0; i < childNodes.Count; i++)
- {
- CreateLabel(childNodes[i], stringBuilder);
- }
- }
- private void CreateLabel(XmlNode node, StringBuilder stringBuilder)
- {
- Match match = Regex.Match(stringBuilder.ToString(), Mark);
- if (match.Success)
- {
- stringBuilder.Replace(Mark, "", match.Index, match.Length);
- int insertIndex = match.Index + 2;
- for (int i = 0; i < node.ChildNodes.Count; i++)
- {
- XmlNode childNode = node.ChildNodes[i];
- string insertStr;
- if (i == 0)
- {
- insertStr = $"\t{Prefix}{node.Name} = \"{node.Name}\";\r\n";
- stringBuilder.Insert(insertIndex, insertStr);
- insertIndex += insertStr.Length;
- }
- insertStr = $"\t{Prefix}{node.Name}{LanguagePageSeperator}{childNode.Name} = \"{node.Name}{LanguagePageSeperator}{childNode.Name}\";";
- stringBuilder.Insert(insertIndex, insertStr);
- insertIndex += insertStr.Length;
- if (i < node.ChildNodes.Count - 1)
- {
- stringBuilder.Insert(insertIndex, "\r\n");
- insertIndex += 2;
- }
- else if (i == node.ChildNodes.Count - 1)
- {
- stringBuilder.Insert(insertIndex, $"\r\n\t{Mark}\r\n");
- }
- }
- }
- else
- {
- throw new Exception("没有找到标记");
- }
- }
- public static string CombineLanguageLabel(string page, string id)
- {
- return $"{page}{LanguagePageSeperator}{id}";
- }
- //public void ReplaceLabels()
- //{
- // Transform[] transforms = Prefabs[0].GetComponentsInChildren<Transform>(true);
- // string[] pathes = Directory.GetFiles(Application.dataPath, "*.cs", SearchOption.AllDirectories);
- // foreach (var path in pathes)
- // {
- // if (path.Contains("PrefabLabel"))
- // {
- // continue;
- // }
- // string fileStr = File.ReadAllText(path);
- // foreach (var transform in transforms)
- // {
- // if (transform.name == "Canvas")
- // {
- // continue;
- // }
- // fileStr = Regex.Replace(fileStr, $"\"{transform.name}\"", $"PrefabLabel.{transform.name}");
- // }
- // File.WriteAllText(path, fileStr);
- // }
- //}
- //public void TestReplaceLabels()
- //{
- // Transform[] transforms = Prefabs[0].GetComponentsInChildren<Transform>(true);
- // string path = AssetDatabase.GetAssetPath(TextAsset);
- // string fileStr = File.ReadAllText(path);
- // foreach (var transform in transforms)
- // {
- // if (transform.name == "Canvas")
- // {
- // continue;
- // }
- // fileStr = Regex.Replace(fileStr, $"\"{transform.name}\"", $"PrefabLabel.{transform.name}");
- // }
- // File.WriteAllText(path, fileStr);
- //}
- //public void ReplaceLanguageLabels()
- //{
- // string[] pathes = Directory.GetFiles(Application.dataPath, "*.cs", SearchOption.AllDirectories);
- // foreach (var path in pathes)
- // {
- // ReplaceLanguageLabel(path);
- // }
- //}
- //public void ReplaceLanguageLabel(string path)
- //{
- // string fileStr = File.ReadAllText(path);
- // string pattern = "(?<=Language\\.GetStr\\()[^\\)]+(?=\\))|(?<=MulLanStr\\()[^\\)]+(?=\\))";
- // Match match = Regex.Match(fileStr, pattern);
- // while (match.Success)
- // {
- // string[] strings = match.Value.Replace(" ", "").Split(',');
- // if (strings.Length == 1)
- // {
- // match = match.NextMatch();
- // }
- // else
- // {
- // fileStr = fileStr.ReplaceByLength(match.Index, match.Length, $"LanguageLabel.{strings[0].Trim('"')}{LanguagePageSeperator}{strings[1].Trim('"')}");
- // match = Regex.Match(fileStr, pattern);
- // }
- // }
- // File.WriteAllText(path, fileStr);
- //}
- }
|