Unity多语言本地化

游戏开发中通过实现语言的切换,,,(联想小游戏中的中英文切换效果)

做了一个小Demo简单实现下:https://github.com/Czhenya/ChangeLanguage

效果图:
这里写图片描述
这里写图片描述

上图的Chinese,和English都是和下面代码中的“ SystemLanguage”名字对应的,,欲换其他语言名称也需对应,,主要中文的格式要存储为utf-8的格式,

txt 的内容就是要更换语言的内容,key保持一致,更换value的值,
这里写图片描述

实现代码:

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

public class LanguageMar : MonoBehaviour {

    private static LanguageMar instance;
    /// <summary>
    /// 单例
    /// </summary>
    public static LanguageMar Instance {
        get { return instance; }
    }

    //选择语言
    [SerializeField]
    private SystemLanguage language;

    //存储加载出来的Key,Value
    Dictionary<string, string> dict = new Dictionary<string, string>();


    /// <summary>
    /// 加载预翻译语言
    /// </summary>
    private void LoadLanguage()
    {

        TextAsset ta = Resources.Load<TextAsset>(language.ToString());

        if (ta == null) { Debug.LogWarning("没有此语言的预编译"); return; }

        //分割行,,
         string[] lines = ta.text.Split('\n');

        //读取key : value,并存到上面字典中
        for (int i = 0; i < lines.Length; i++)
        {
            if (string.IsNullOrEmpty(lines[i]))
            {
                continue;
            }
            else
            {
                string[] kv = lines[i].Split(':');                
                //存到字典中
                dict.Add(kv[0], kv[1]);

                //Debug.Log("Key:" + kv[0] + "Value:" + kv[1]);
            }
        }
    }

    /// <summary>
    /// 获取值,用于显示在所需的文本中
    /// </summary>
    /// <param name="key"></param>
    /// <returns>返回所对应的值,若不存在返回空</returns>
    public string GetTest(string key)
    {
        if (dict.ContainsKey(key))
        {
            return dict[key];
        }
        else {

            return null;
        }
    }

    void Awake () {
        instance = this;

        LoadLanguage();

    }

}

猜你喜欢

转载自blog.csdn.net/czhenya/article/details/80503521