Unity 中英文转换

在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;

        GetChineseLanguageContentLanguage.Chinese);
        GetChineseLanguageContentLanguage.English);
    }

    public IEnumerable Start)
    {
        yield return new WaitForSeconds1);
    }

    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].GetTextLanguagelanguage);
        }
    }

    public void GetChineseLanguageContentLanguage 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.IsNullOrEmptystr))
            {
                continue;
            }
            string[] s = str.Split':');

            if lan == Language.Chinese)
            {
                if !Chinesedict.ContainsKeys[0]))
                {
                    Chinesedict.Adds[0], s[1]);
                }
            }
            else if lan == Language.English)
            {
                if !Englishdict.ContainsKeys[0]))
                {
                    Englishdict.Adds[0], s[1]);
                }
            }
        }
    }

    public string LanguageSetUITextLanguage lan, string key)
    {
        if lan == Language.Chinese)
        {
            if !Chinesedict.ContainsKeykey))
            {
                return string.Empty;
            }
            else
            {
                return Chinesedict[key];
            }
        }
        else
        {
            if !Englishdict.ContainsKeykey))
            {
                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)
    {
        GetTextLanguageLanguageManager.Instance.language);
    }

    void Update)
    {

    }

    public void GetTextLanguageLanguage lan)
    {

        if !string.IsNullOrEmptykey))
        {
            string value = LanguageManager.Instance.LanguageSetUITextlan, key);

            if !string.IsNullOrEmptyvalue))
                GetComponent<Text>).text = value;
        }
    }
}

===========================================================================================

在场景中新建“GameObject”,命名为“LanguageManarger”,添加“LanguageManager.cs”

UI界面新建一个按钮,按钮调用“LanguageManarger”中“ChangeLanguage”方法

按钮下的Text添加“UIText.cs”在“Key”中输入字典名称

文本名称一定要与枚举名称对应上,不然读取不到文本数据

===========================================================================================

效果图

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注