Unity 处理预设中的中文

需求由来

  • 项目接入越南版本

需要解决的文本问题

  • 获取UI预设Label里面的中文(没被代码控制)提供给越南
  • Label里面的中文替换成越南文

解决流程

  • 迭代获取Assets目录下所有文件

  • 获取所有的.prefab预设文件

  • 加载预设文件

  • 获取预设下所有的UILabel组建

  • 判断UILabel中的值是否为中文

  • 把所有的中文实例化成文本

  • 替换成越南文

  • 保存实例化对象为预设文件

  • 销毁实例化对象

实现代码

  • 获取UI预设Label里面的中文
  1. [MenuItem("检查预设中文并且生成文本")]
  2. static void CheckChinesePrefabsAndSerialization()
  3. {
  4. List<string> paths = GetAllFilePaths();
  5. List<string> prefabPaths = GetAllPrefabFilePaths(paths);
  6. if (prefabPaths == null)
  7. {
  8. return;
  9. }
  10. List<string> text = new List<string>();
  11. for (int i = 0; i < prefabPaths.Count; i++)
  12. {
  13. string prefabPath = prefabPaths[i];
  14. //修改路径格式
  15. prefabPath = ChangeFilePath(prefabPath);
  16. AssetImporter tmpAssetImport = AssetImporter.GetAtPath(prefabPath);
  17. GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(tmpAssetImport.assetPath);
  18. if (prefab == null)
  19. {
  20. continue;
  21. }
  22. UILabel[] uiLabels = prefab.GetComponentsInChildren<UILabel>(true);
  23. for (int j = 0; j < uiLabels.Length; j++)
  24. {
  25. UILabel uiLabel = uiLabels[j];
  26. if (IsIncludeChinese(uiLabel.text))
  27. {
  28. Debug.LogError(string.Format("路径:{0} 预设名:{1} 对象名:{2} 中文:{3}", prefabPath, prefab.name, uiLabel.name, uiLabel.text));
  29. text.Add(uiLabel.text);
  30. }
  31. }
  32. //进度条
  33. float progressBar = (float)i / prefabPaths.Count;
  34. EditorUtility.DisplayProgressBar("检查预设中文", "进度 :" + ((int)(progressBar * 100)).ToString() + "%", progressBar);
  35. }
  36. SerializationText(Application.dataPath + "/中文.txt", text);
  37. EditorUtility.ClearProgressBar();
  38. AssetDatabase.Refresh();
  39. Debug.Log("完成检查预设中文并且生成文本");
  40. }
  • Label里面的中文替换成越南文
  1. [MenuItem("ZouQiang/Prefab(预设)/检查预设中文并且替换为越南文")]
  2. static void CheckChinesePrefabsAndReplaceChinese()
  3. {
  4. List<string> paths = GetAllFilePaths();
  5. List<string> prefabPaths = GetAllPrefabFilePaths(paths);
  6. if (prefabPaths == null)
  7. {
  8. return;
  9. }
  10. for (int i = 0; i < prefabPaths.Count; i++)
  11. {
  12. string prefabPath = prefabPaths[i];
  13. //修改路径格式
  14. prefabPath = ChangeFilePath(prefabPath);
  15. AssetImporter tmpAssetImport = AssetImporter.GetAtPath(prefabPath);
  16. GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(tmpAssetImport.assetPath);
  17. if (prefab == null)
  18. {
  19. continue;
  20. }
  21. GameObject obj = Instantiate(prefab) as GameObject;
  22. UILabel[] uiLabels = obj.GetComponentsInChildren<UILabel>(true);
  23. bool isChange = false;
  24. for (int j = 0; j < uiLabels.Length; j++)
  25. {
  26. UILabel uiLabel = uiLabels[j];
  27. if (IsIncludeChinese(uiLabel.text))
  28. {
  29. Debug.LogError(string.Format("路径:{0} 预设名:{1} 对象名:{2} 中文:{3}", prefabPath, prefab.name, uiLabel.name, uiLabel.text));
  30. uiLabel.text = "越南文";
  31. isChange = true;
  32. }
  33. }
  34. if (isChange)
  35. {
  36. PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ReplaceNameBased);
  37. }
  38. DestroyImmediate(obj);
  39. //进度条
  40. float progressBar = (float)i / prefabPaths.Count;
  41. EditorUtility.DisplayProgressBar("检查预设中文并且替换为越南文", "进度 :" + ((int)(progressBar * 100)).ToString() + "%", progressBar);
  42. }
  43. EditorUtility.ClearProgressBar();
  44. AssetDatabase.Refresh();
  45. Debug.Log("检查预设中文并且替换为越南文");
  46. }

相关代码接口

  • 迭代获取目录下所有文件路径
  1. public static void IterationGetFilesPath(string directory, List<string> outPaths)
  2. {
  3. string[] files = Directory.GetFiles(directory);
  4. outPaths.AddRange(files);
  5. string[] childDirectories = Directory.GetDirectories(directory);
  6. if (childDirectories != null && childDirectories.Length > 0)
  7. {
  8. for (int i = 0; i < childDirectories.Length; i++)
  9. {
  10. string dir = childDirectories[i];
  11. if (string.IsNullOrEmpty(dir)) continue;
  12. IterationGetFilesPath(dir, outPaths);
  13. }
  14. }
  15. }
  • 获取项目Assets下所有文件路径
  1. public static List<string> GetAllFilePaths()
  2. {
  3. List<string> paths = new List<string>();
  4. IterationGetFilesPath(Application.dataPath, paths);
  5. return paths;
  6. }
  • 获取所有预设文件路径
  1. public static List<string> GetAllPrefabFilePaths(List<string> paths)
  2. {
  3. if (paths == null)
  4. {
  5. return null;
  6. }
  7. List<string> prefabPaths = new List<string>();
  8. for (int i = 0; i < paths.Count; i++)
  9. {
  10. string path = paths[i];
  11. if (path.EndsWith(".prefab") == true)
  12. {
  13. prefabPaths.Add(path);
  14. }
  15. //进度条
  16. float progressBar = (float)i / paths.Count;
  17. EditorUtility.DisplayProgressBar("获取所有预设文件路径", "进度 : " + ((int)(progressBar * 100)).ToString() + "%", progressBar);
  18. }
  19. EditorUtility.ClearProgressBar();
  20. return prefabPaths;
  21. }
  • 是否包含是否有中文
  1. public static bool IsIncludeChinese(string content)
  2. {
  3. string regexstr = @"[\u4e00-\u9fa5]";
  4. if (Regex.IsMatch(content, regexstr))
  5. {
  6. return true;
  7. }
  8. else
  9. {
  10. return false;
  11. }
  12. }
  • 改变路径 例如 "C:/Users/XX/Desktop/aaa/New Unity Project/Assets\a.prefab" 改变成 "Assets/a.prefab"
  1. public static string ChangeFilePath(string path)
  2. {
  3. path = path.Replace("\\", "/");
  4. path = path.Replace(Application.dataPath + "/", "");
  5. path = "Assets/" + path;
  6. return path;
  7. }
  • 序列化
  1. public static void SerializationText(string filePath, List<string> content)
  2. {
  3. if (content == null)
  4. {
  5. return;
  6. }
  7. FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
  8. StreamWriter streamWriter = new StreamWriter(fileStream);
  9. StringBuilder stringBuilder = new StringBuilder();
  10. for (int i = 0; i < content.Count; i++)
  11. {
  12. stringBuilder.AppendLine(content[i]);
  13. }
  14. streamWriter.Write(stringBuilder);
  15. streamWriter.Close();
  16. }

Unity 处理预设中的中文的更多相关文章

  1. Unity 4.0 中的新动画系统——MecAnim

    分享一个文档资料,关于动画系统的,版本应该很老了,但是有借鉴意义的: Unity 4.0 已于 2012 年 11 月 15 日正式发布,Unity 每一次版本的提升,都给游戏开发者带来惊喜,这一次也 ...

  2. Unity 5.6中的混合光照(下)

    https://mp.weixin.qq.com/s/DNQFsWpZm-ybIlF3DTAk2A 在<Unity 5.6中的混合光照(上)>中,我们介绍了混合模式,以及Subtracti ...

  3. MAC下 mysql不能插入中文和中文乱码的问题总结

    MAC下 mysql不能插入中文和中文乱码的问题总结 前言 本文中所提到的问题解决方案,都是基于mac环境下的,但其他环境,比如windows应该也适用. 问题描述 本文解决下边两个问题: 往mysq ...

  4. sqlServer去除字段中的中文

    很多时候数据库表中某些字段是由中文和字母或数字组成,但有时我们又需要将字段中的中文去掉.想要实现这种需求的方法有很多,下面就是其中一种解决方法. 首先我们先建立测试数据 create table te ...

  5. C# 删除字符串中的中文

    /// <summary> /// 删除字符串中的中文 /// </summary> public static string Delete中文(string str) { s ...

  6. 在MySQL向表中插入中文时,出现:incorrect string value 错误

    在MySQL向表中插入中文时,出现:incorrect string value 错误,是由于字符集不支持中文.解决办法是将字符集改为GBK,或UTF-8.      一.修改数据库的默认字符集   ...

  7. lua中的中文乱码

    最近在用lua, 发现一个有点意思的槽点啊-____-! 那就是lua貌似会使用系统所用的字符集. 具体点说, 就是在windows上, 它会使用cp936来表示代码中的中文. 来个例子: print ...

  8. URL地址中使用中文作为的参数【转】

    原文:http://blog.csdn.net/blueheart20/article/details/43766713 引言: 在Restful类的服务设计中,经常会碰到需要在URL地址中使用中文作 ...

  9. PHP往mysql数据库中写入中文失败

    该类问题解决办法就是 在建立数据库连接之后,将该连接的编码方式改为中文. 代码如下: $linkID=@mysql_connect("localhost","root&q ...

随机推荐

  1. m3u8文件什么合成便于播放的MP4的方法

    先大家要知道M3U8文件不是一个视频文件,里面全是一些路径,说白了就是一个目录而已,所以要看视频,要找到对应存放视频的文件夹.如果不知道怎么找,可以使用文本格式打开M3U8文件,里面会有路径提示.   ...

  2. rhce 第十一题 挂载NFS共享

    挂载NFS共享 在system2上挂载一个来自 system1.group8.example.com 的NFS共享,并符合下列要求: /public 挂载在/mnt/nfsmount目录上 /prot ...

  3. 算法练习LeetCode初级算法之树

    二叉树的前序遍历 我的解法:利用递归,自底向下逐步添加到list,返回最终的前序遍历list class Solution { public List<Integer> preorderT ...

  4. ExecuteReader()获得数据

    ExecuteReader用于实现只进只读的高效数据查询.ExecuteReader:返回一个SqlDataReader对象,可以通过这个对象来检查查询结果,它提供了只进只读的执行方式,即从结果中读取 ...

  5. React-router4 第九篇 Ambiguous Matches 模糊匹配

    https://reacttraining.com/react-router/web/example/ambiguous-matches 看了官方的例子,我准备把阮一峰老师的代码再粘贴一次..!!

  6. 获取当前最顶层的ViewController

    - (UIViewController *)topViewController { UIViewController *resultVC; resultVC = [self _topViewContr ...

  7. Python之队列Queue

    今天我们来了解一下python的队列(Queue) queue is especiall useful in threaded programming when information must be ...

  8. finereport 下拉复选框多选

  9. 利用iptables防止ssh暴力破解和控制网速

    iptables -I INPUT -p tcp --dport 22 -i eth0 -m state --state NEW -m recent --setiptables -I INPUT -p ...

  10. Oracle学习——第一章

    Oracle数据库特点:安全性高,数据类型丰富 Oracle是由美国甲骨文公司开发的一款数据库产品 -------------------------------------------------- ...