1.LanguageManagers的C#脚本,把数据保存到字典里。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public enum Language
{
Chinese, //中文
English, //英文
}
public delegate void ChangeLanguage();
public class LanguageDataManager : MonoBehaviour
{
[SerializeField]
internal Language currentLanguage = Language.Chinese; //当前设置的语言
//定义一个委托事件,用于改变语言时,可以改变当前所显示的文本
public event ChangeLanguage ChangeLangeuageEvent;
// 中英文字典
private Dictionary<string, string> ChineseDictionary = new Dictionary<string, string>();
private Dictionary<string, string> EnglishDictionary = new Dictionary<string, string>();
private static LanguageDataManager instancce = null;
public static LanguageDataManager Instancce
{
get
{
return instancce;
}
}
private void Awake()
{
instancce = this;
// 加载语言资源
LoadLanguageTxt(Language.Chinese);
LoadLanguageTxt(Language.English);
}
public void LanguageState(Language type) {
currentLanguage = type;
ChangeLangeuageEvent();
}
/// <summary>
/// 设置语言
/// </summary>
public void SetText(Text text, string target)
{
string val = instancce.GetLanguageText(target.ToString());
if (string.IsNullOrEmpty(val) != true) {
text.text = val;
}
}
/// <summary>
/// 加载对应的语言资源txt,保存到字典中
/// </summary>
/// <param name="language"></param>
internal void LoadLanguageTxt(Language language)
{
TextAsset txt = Resources.Load<TextAsset>("language/" + language.ToString());
if (txt == null)
{
Debug.LogWarning("没有该语言的文本文件");
return;
}
string[] lines = txt.text.Split('\n');
for (int i = 0; i < lines.Length; i++)
{
if (string.IsNullOrEmpty(lines[i]))
{
continue;
}
string[] kv = lines[i].Split(':');
if (language == Language.Chinese)
{
ChineseDictionary.Add(kv[0], kv[1]);
}
else if (language == Language.English)
{
EnglishDictionary.Add(kv[0], kv[1]);
}
}
}
/// <summary>
/// 获得对应语言字典中的对应的 key 值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
internal string GetLanguageText(string key)
{
if (currentLanguage == Language.Chinese)
{
if (ChineseDictionary.ContainsKey(key))
{
return ChineseDictionary[key];
}
else
{
Debug.Log("empty");
}
}
else if (currentLanguage == Language.English)
{
if (EnglishDictionary.ContainsKey(key))
{
return EnglishDictionary[key];
}
else
{
Debug.Log("empty)");
}
}
return string.Empty;
}
}
2.新建SelectLanguageToggleC#脚本,绑定下拉框选择事件changed(关联方法setLanguage),option(中文,English)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SelectLanguageToggle : MonoBehaviour
{
private Dropdown dropdown;
private Text label;
private Button button;
private List<string> keys;
void Start()
{
}
public void setLanguage() {
Dropdown dr = GetComponent<Dropdown>();
if (dr.value == 0)
{
LanguageState(Language.Chinese);
}
else {
LanguageState(Language.English);
}
}
public void LanguageState(Language type)
{
LanguageDataManager.Instancce.LanguageState(type);
}
}
3.新建LanguageUIText,挂载到有Text组件的对象上,填写key(和翻译文本的key一样,如text1)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LanguageUIText : MonoBehaviour
{
[SerializeField] private string key;
private Text text;
private void Start()
{
text = GetComponent<Text>();
GetLanguageText();
LanguageDataManager.Instancce.ChangeLangeuageEvent += GetLanguageText;
}
/// <summary>
/// 根据当前语言设置 Text 文本
/// </summary>
///
void GetLanguageText() {
LanguageDataManager.Instancce.SetText(text, key);
}
}
4.文件目录