Unity功能记录(十二) ------ 语言本地化
因为我们公司的产品涉及国外,所以需要中文英语两个版本
一.创建TXT脚本,按指定格式填写
我直接在Resources目录下创建了,小项目还没用AB包
注意中英文两个TXT文件中的key值保持一致
二.根据本机语言设置text上文字
1.读取TXT文件并存入Dictionary中
public SystemLanguage localLanguage = SystemLanguage.Chinese;
public Dictionary<string, string> LanguageDic = new Dictionary<string, string>();
private void Awake()
{
localLanguage = Application.systemLanguage;
TextAsset ta;
if (localLanguage == SystemLanguage.Chinese ||
localLanguage == SystemLanguage.ChineseSimplified ||
localLanguage == SystemLanguage.ChineseTraditional)
{
ta = Resources.Load<TextAsset>("LanguageLocal/Chinese");
Debug.Log(ta);
}
else
ta = Resources.Load<TextAsset>("LanguageLocal/English");
string text = ta.text;
string[] lines = text.Split('\n');
foreach(string line in lines)
{
if (line == null)
{
continue;
}
string[] keyAndValue = line.Split('=');
LanguageDic.Add(keyAndValue[0], keyAndValue[1]);
}
}
2.text设置
testText.text = LanguageDic["ApolloRecog"];