Unity语言本地化

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

public class LocallizationManager{
    private static LocallizationManager _instance;
    private const string Chinese = "Localization/Chinese";
    private const string English = "Localization/English";

    private string language = Chinese;

    private Dictionary languageDic;

    public static LocallizationManager Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new LocallizationManager();
            }
            return _instance;
        }
    }

    private LocallizationManager()
    {
        //将文本内容转为字典
        //汉语
        languageDic = new Dictionary();
        TextAsset ta = Resources.Load(language);
        string[] lines = ta.text.Split('\n');
        foreach(string line in lines)
        {
            string[] tagAndContext = line.Split('=');
            //Debug.Log(tagAndContext[0] + tagAndContext[1]);
            languageDic.Add(tagAndContext[0], tagAndContext[1]);
        }
    }

    public void Init(){
        //Do nothing.
    }

    public string GetValue(string key)
    {
        string value = null;
        languageDic.TryGetValue(key, out value);
        //Debug.Log(value);
        return value;
    }
}



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

public class SetText : MonoBehaviour {//该脚本挂载到需要本地化语言的控件上设置相关的Key
    
    public string key;
    private Text text;
    
	// Use this for initialization
	void Start () {
	    text = transform.GetComponent();
        text.text = LocallizationManager.Instance.GetValue(key);
	}
	
	// Update is called once per frame
	void Update () {
	
	}

Chinese.txt

start=开始
exit=退出  

你可能感兴趣的:(Unity)