在Resources下创建文件夹LanguageTxt,再其创建“Chinese.txt”及“English.txt”(记得保存文本时选择UTF-8)
Chinese.txt
Btn:这是一个按钮
English.txt
Btn:This is a button
在Scripts下创建“LanguageManager.cs”及“UIText.cs”
LanguageManager.cs
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public enum Language
{
Chinese,
English,
}
public class LanguageManager : MonoBehaviour
{
public List<UIText> uitest;
public Language language;
private Dictionary<string, string> Chinesedict = new Dictionary<string, string>();
private Dictionary<string, string> Englishdict = new Dictionary<string, string>();
private static LanguageManager instance;
public static LanguageManager Instance
{
get
{
return instance;
}
}
private void Awake()
{
instance = this;
GetChineseLanguageContent(Language.Chinese);
GetChineseLanguageContent(Language.English);
}
public IEnumerable Start()
{
yield return new WaitForSeconds(1);
}
public void ChangeLanguage()
{
uitest.Clear();
uitest = FindObjectsOfType<UIText>().ToList();
if (language == Language.Chinese)
{
language = Language.English;
}
else
{
language = Language.Chinese;
}
for (int i = 0; i < uitest.Count; i++)
{
uitest[i].GetTextLanguage(language);
}
}
public void GetChineseLanguageContent(Language lan)
{
TextAsset ta = null;
if (lan == Language.Chinese)
ta = Resources.Load<TextAsset>("LanguageTxt/Chinese");
else if (lan == Language.English)
{
ta = Resources.Load<TextAsset>("LanguageTxt/English");
}
if (ta == null)
{
Debug.Log("语言文件为空,请检查");
return;
}
string[] lines = ta.text.Split('\n');
foreach (string str in lines)
{
if (string.IsNullOrEmpty(str))
{
continue;
}
string[] s = str.Split(':');
if (lan == Language.Chinese)
{
if (!Chinesedict.ContainsKey(s[0]))
{
Chinesedict.Add(s[0], s[1]);
}
}
else if (lan == Language.English)
{
if (!Englishdict.ContainsKey(s[0]))
{
Englishdict.Add(s[0], s[1]);
}
}
}
}
public string LanguageSetUIText(Language lan, string key)
{
if (lan == Language.Chinese)
{
if (!Chinesedict.ContainsKey(key))
{
return string.Empty;
}
else
{
return Chinesedict[key];
}
}
else
{
if (!Englishdict.ContainsKey(key))
{
return string.Empty;
}
else
{
return Englishdict[key];
}
}
}
}
===========================================================================================
UIText.cs
using UnityEngine;
using UnityEngine.UI;
public delegate void DelegateGetText();
public class UIText : MonoBehaviour
{
[SerializeField]
private string key;
void Start()
{
GetTextLanguage(LanguageManager.Instance.language);
}
void Update()
{
}
public void GetTextLanguage(Language lan)
{
if (!string.IsNullOrEmpty(key))
{
string value = LanguageManager.Instance.LanguageSetUIText(lan, key);
if (!string.IsNullOrEmpty(value))
GetComponent<Text>().text = value;
}
}
}
===========================================================================================
在场景中新建“GameObject”,命名为“LanguageManarger”,添加“LanguageManager.cs”
UI界面新建一个按钮,按钮调用“LanguageManarger”中“ChangeLanguage”方法
按钮下的Text添加“UIText.cs”在“Key”中输入字典名称
文本名称一定要与枚举名称对应上,不然读取不到文本数据
===========================================================================================
效果图