Unity 处理预设中的中文

需求由来

  • 项目接入越南版本

需要解决的文本问题

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

解决流程

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

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

  • 加载预设文件

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

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

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

  • 替换成越南文

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

  • 销毁实例化对象

实现代码

  • 获取UI预设Label里面的中文
[MenuItem("检查预设中文并且生成文本")]
static void CheckChinesePrefabsAndSerialization()
{
List<string> paths = GetAllFilePaths(); List<string> prefabPaths = GetAllPrefabFilePaths(paths); if (prefabPaths == null)
{
return;
} List<string> text = new List<string>(); for (int i = 0; i < prefabPaths.Count; i++)
{
string prefabPath = prefabPaths[i]; //修改路径格式
prefabPath = ChangeFilePath(prefabPath); AssetImporter tmpAssetImport = AssetImporter.GetAtPath(prefabPath); GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(tmpAssetImport.assetPath); if (prefab == null)
{
continue;
} UILabel[] uiLabels = prefab.GetComponentsInChildren<UILabel>(true); for (int j = 0; j < uiLabels.Length; j++)
{
UILabel uiLabel = uiLabels[j]; if (IsIncludeChinese(uiLabel.text))
{
Debug.LogError(string.Format("路径:{0} 预设名:{1} 对象名:{2} 中文:{3}", prefabPath, prefab.name, uiLabel.name, uiLabel.text));
text.Add(uiLabel.text);
}
} //进度条
float progressBar = (float)i / prefabPaths.Count;
EditorUtility.DisplayProgressBar("检查预设中文", "进度 :" + ((int)(progressBar * 100)).ToString() + "%", progressBar);
} SerializationText(Application.dataPath + "/中文.txt", text); EditorUtility.ClearProgressBar(); AssetDatabase.Refresh(); Debug.Log("完成检查预设中文并且生成文本");
}
  • Label里面的中文替换成越南文
    [MenuItem("ZouQiang/Prefab(预设)/检查预设中文并且替换为越南文")]
static void CheckChinesePrefabsAndReplaceChinese()
{
List<string> paths = GetAllFilePaths(); List<string> prefabPaths = GetAllPrefabFilePaths(paths); if (prefabPaths == null)
{
return;
} for (int i = 0; i < prefabPaths.Count; i++)
{
string prefabPath = prefabPaths[i]; //修改路径格式
prefabPath = ChangeFilePath(prefabPath); AssetImporter tmpAssetImport = AssetImporter.GetAtPath(prefabPath); GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(tmpAssetImport.assetPath); if (prefab == null)
{
continue;
} GameObject obj = Instantiate(prefab) as GameObject; UILabel[] uiLabels = obj.GetComponentsInChildren<UILabel>(true); bool isChange = false; for (int j = 0; j < uiLabels.Length; j++)
{
UILabel uiLabel = uiLabels[j]; if (IsIncludeChinese(uiLabel.text))
{
Debug.LogError(string.Format("路径:{0} 预设名:{1} 对象名:{2} 中文:{3}", prefabPath, prefab.name, uiLabel.name, uiLabel.text));
uiLabel.text = "越南文";
isChange = true;
}
} if (isChange)
{
PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ReplaceNameBased);
} DestroyImmediate(obj); //进度条
float progressBar = (float)i / prefabPaths.Count;
EditorUtility.DisplayProgressBar("检查预设中文并且替换为越南文", "进度 :" + ((int)(progressBar * 100)).ToString() + "%", progressBar);
} EditorUtility.ClearProgressBar(); AssetDatabase.Refresh(); Debug.Log("检查预设中文并且替换为越南文");
}

相关代码接口

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

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. oracle 查询索引和主键

    ORACLE: 1.查主键名称: select * from user_constraints where table_name = 'AAA' and constraint_type ='P'; 查 ...

  2. 网络基础-- 之 子网划分 and 一些基础解释

    子网划分的核心思想就是------   借主机为为网络位 最近几天--看了一下今天就分享一波. 首先我们先来理解一下 -----   one. 进制的转换   -----   two. IP地址 -- ...

  3. [原]Docker部署SuperMap8.1.1

    摘要:本文主要实践在Docker上制作SuperMap(超图)V8.1.1镜像,文中将使用 docker commit 方式创新镜像(Dockerfile择机再做).本文中使用的宿主环境及镜像环境都是 ...

  4. ES之五:ElasticSearch聚合

    前言 说完了ES的索引与检索,接着再介绍一个ES高级功能API – 聚合(Aggregations),聚合功能为ES注入了统计分析的血统,使用户在面对大数据提取统计指标时变得游刃有余.同样的工作,你在 ...

  5. python历史与基本类型

    前言 我自学的方式主要是看文档,看视频,第一次做写博客这么神圣的事情,内心是忐忑的,写的东西比较杂,路过的小伙伴不要嘲笑我,主要是记录一日所学,顺便锻炼一下语言组织能力吧,anyway,这些都不重要, ...

  6. ad_imh

    1. stc15    ad_1.7  9600 1t #include < #include <absacc.h> #include <stdio.h> #includ ...

  7. leveldb 学习记录(四) skiplist补与变长数字

    在leveldb 学习记录(一) skiplist 已经将skiplist的插入 查找等操作流程用图示说明 这里在介绍 下skiplist的代码 里面有几个模块 template<typenam ...

  8. Effective C++ 笔记:条款 32 确定你的public继承塑造出正确的is-a关系

    32 : Make sure public inheritance models "is-a." 0 引言 Inheritance and Object-Oriented Desi ...

  9. 使用jquery load遇到一个问题解决

    1.环境 动态加载页面后给页面中的控件动态赋值,赋完更新后一闪值就没了. 2.原因    load()方法是异步加载,页面没有加载完就开始赋值,导致情况出现. 3.解决   加载完成后赋值 $(&qu ...

  10. Codeforces Round #539 (Div. 2) 异或 + dp

    https://codeforces.com/contest/1113/problem/C 题意 一个n个数字的数组a[],求有多少对l,r满足\(sum[l,mid]=sum[mid+1,r]\), ...