我想为我的游戏添加多种语言支持,目前我正在使用资产商店中的智能本地化。我在第一个场景中有4个按钮用于切换语言,如果选择了一个按钮,浮动“语言”将变为1到4之间的值,语言会发生变化。如果继续下一个场景,“语言”将保存在PlayerPrefs中,如果再次加载脚本,“language”将从PlayerPrefs获取值,因此它应该以之前选择的语言显示文本但是虽然“语言”具有应该改变语言的有效值(1-4),但是文本字段只是空的。有什么想法吗?
继承我的代码:
public class Test : MonoBehaviour {
public Text PlayTxt, BackTxt, EarthTxt, HealthTxt, MenuTxt, MissingTxt, RestartTxt, DamageTxt;
public string playKey = "Play";
public string earthKey = "Earth";
public string healthKey = "Health";
public string menuKey = "Menue";
public string missingKey = "Missing";
public string restartKey = "Restart";
public string damageKey = "Damage";
public string backKey = "Back";
public float language;
void Start(){
if(PlayerPrefs.HasKey ("language")) {
language = PlayerPrefs.GetFloat ("language");
}
}
void Update(){
if (language <= 1) {
LanguageManager.Instance.ChangeLanguage ("en");
}
if (language == 2) {
LanguageManager.Instance.ChangeLanguage ("de");
}
if (language == 3) {
LanguageManager.Instance.ChangeLanguage ("es");
}
if (language == 4) {
LanguageManager.Instance.ChangeLanguage ("tr");
}
PlayTxt.text = "" + LanguageManager.Instance.GetTextValue(playKey);
BackTxt.text = "" + LanguageManager.Instance.GetTextValue(backKey);
EarthTxt.text = "" + LanguageManager.Instance.GetTextValue(earthKey);
HealthTxt.text = "" + LanguageManager.Instance.GetTextValue(healthKey);
MenuTxt.text = "" + LanguageManager.Instance.GetTextValue(menuKey);
MissingTxt.text = "" + LanguageManager.Instance.GetTextValue(missingKey);
RestartTxt.text = "" + LanguageManager.Instance.GetTextValue(restartKey);
DamageTxt.text = "" + LanguageManager.Instance.GetTextValue(damageKey);
}
public void engl(){
LanguageManager.Instance.ChangeLanguage ("en");
language = 1;
}
public void de(){
LanguageManager.Instance.ChangeLanguage ("de");
language = 2;
}
public void es(){
LanguageManager.Instance.ChangeLanguage ("es");
language = 3;
}
public void tr(){
LanguageManager.Instance.ChangeLanguage ("tr");
language = 4;
}
void OnDestroy(){
PlayerPrefs.SetFloat ("language", language);
}
}