Unity 多语言功能

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.文件目录

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值