很多游戏中都有语言设置选项,NGUI插件中自带了国际化脚本,但是灵活性较低,而且目前项目是UGUI,以下是修改后,以便记录。
Localization和NGUI中用法一样,挂在在一个不销毁的游戏物体上,并设置当前语言,及所有语言的陪标
//----------------------------------------------
//----------------------------------------------
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
public class Localization : MonoBehaviour
{
static Localization mInst;
static public Localization instance
{
get
{
if (mInst == null)
{
mInst = Object.FindObjectOfType(typeof(Localization)) as Localization;
if (mInst == null)
{
GameObject go = new GameObject("_Localization");
DontDestroyOnLoad(go);
mInst = go.AddComponent<Localization>();
}
}
return mInst;
}
}
public string startingLanguage;
public TextAsset[] languages;
Dictionary<int, string> mDictionary = new Dictionary<int, string>();
string mLanguage;
public string currentLanguage
{
get
{
//if (string.IsNullOrEmpty(mLanguage))
{
currentLanguage = PlayerPrefs.GetString("Language");
if (string.IsNullOrEmpty(mLanguage))
{
currentLanguage = startingLanguage;
if (string.IsNullOrEmpty(mLanguage) && (languages != null && languages.Length > 0))
{
currentLanguage = languages[0].name;
}
}
}
return mLanguage;
}
set
{
if (mLanguage != value)
{
startingLanguage = value;
if (!string.IsNullOrEmpty(value))
{
if (languages != null)
{
for (int i = 0, imax = languages.Length; i < imax; ++i)
{
TextAsset asset = languages[i];
if (asset != null && asset.name == value)