Unity实现语言国际话

代码一,语言管理类:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LanguageManager : MonoBehaviour {

    private static LanguageManager _instance = null;
    private Dictionary<string, string> dic = new Dictionary<string, string>();

    public static LanguageManager GetInstance()
    {
        return _instance;
    }

    void Awake()
    {
        if (_instance == null)
        {
            _instance = this;
            //如果切换场景时,希望单例中的数据不清空
            //DontDestroyOnLoad(gameObject);//加载场景时不消毁
        }
        else
        {
            //保留老的单例,删除新的单例对象
            Destroy(this.gameObject);
        }

        //得到语言
        //GetLanguage(GameController.Instance().gameData.language);
        GetLanguage(PlayerPrefs.GetString("language"));
        Debug.Log("得到语言 :" + PlayerPrefs.GetString("language"));
    }


    /// <summary>
    /// 获取语言
    /// </summary>
    /// <param name="language"></param>
    public void GetLanguage(string language)
    {
        TextAsset ta = Resources.Load("Language/" + language) as TextAsset;
        string text = ta.text;

        string[] lines = text.Split('\n');
        foreach (string line in lines)
        {
            if (line == null)
            {
                continue;
            }
            //如果单行有“=”才做操作
            if (line.Contains("="))
            {
                string[] keyAndValue = line.Split('=');
                //当字典中存在该key时,先移除在添加
                if (dic.ContainsKey(keyAndValue[0]))
                {
                    dic.Remove(keyAndValue[0]);
                    dic.Add(keyAndValue[0], keyAndValue[1]);
                }
                else
                {
                    dic.Add(keyAndValue[0], keyAndValue[1]);
                }
            }
        }
    }

    /// <summary>  
    /// 获取value  
    /// </summary>  
    /// <param name="key"></param>  
    /// <returns></returns>  
    public string GetValue(string key)
    {
        if (dic.ContainsKey(key) == false)
        {
            return null;
        }
        string value = null;
        dic.TryGetValue(key, out value);
        return value;
    }
}

需要设置的单个对象(将脚本挂载在该text对象上,设置Key,根据文本中的key找到value)

代码为:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LocalizationText : MonoBehaviour {

    public string key = "";
    string language = "";
    private Text nameText;

	void Start () {
        nameText = GetComponent<Text>();
        language = GameController.Instance().gameData.language;

        //语言是英语,根据语言设置字体大小
        if (GameController.Instance().gameData.language == "en")
        {
            nameText.fontSize = 50;
        }

        nameText.text = LanguageManager.GetInstance().GetValue(key);
	}

    void Update()
    {
        if (language != GameController.Instance().gameData.language)
        {
            language = GameController.Instance().gameData.language;
            nameText.text = LanguageManager.GetInstance().GetValue(key);
        }
    }
}

文本中的显示:

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章