//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 Languages; // public List Prefabs; // // public static string Mark = "//Mark"; // public static string Prefix = "public static string "; // public static string LanguagePageSeperator = "__"; // // #endregion // // #if UNITY_EDITOR // public void ClearLabels(bool showWarning) // { // if (showWarning) // { // if (!EditorUtility.DisplayDialog("注意", "清空所有标签?", "确定", "取消")) // { // return; // } // } // // 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()); // } // } // // public void ClearAndCreateLabelFromPrefabs() // { // ClearLabels(false); // CreateLabelFromPrefabs(); // } // // private void CreateLabel(GameObject prefab, StringBuilder stringBuilder) // { // Transform[] transforms = prefab.GetComponentsInChildren(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); // } // } // // public void ClearAndCreateLabelFromLanguageDocument() // { // ClearLabels(false); // CreateLabelFromLanguage(); // } // // 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("没有找到标记"); // } // } // #endif // // public static string CombineLanguageLabel(string page, string id) // { // return $"{page}{LanguagePageSeperator}{id}"; // } // // //public void ReplaceLabels() // //{ // // Transform[] transforms = Prefabs[0].GetComponentsInChildren(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(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); // //} //}