Unity实现本地文本多语言化
Anfeng0228 人气:1本文着重为大家仔细讲解了Unity实现本地文本多语言化,文中代码实例讲解的非常细致,希望能够帮助到您,欢迎大家阅读和收藏
在unity项目过程中大多都会遇到多语言化,下面讲一下自己的一些实现思路。
1. 创建一个要实现多语言化的基类
public abstract class BaseString { public abstract string text_test{get;} }
2. 对应的语言要继承BaseString类,这里实现中文和英文的两个类
public class ChineseString: BaseString { public override string text_test { get {return "哈喽";}} } public class EnglishString : BaseString { public override string text_test { get {return "Hello";}} }
3. 新建一个管理类 TextManager.cs
public class TextManager { public BaseString strComponent; private ChineseString chineseStrCompenent = new ChineseString(); private EnglishString englishStrComponent = new EnglishString(); public TextManager() { strComponent = chineseStrCompenent; } public void ChangeLanguage(LanguageType type) { if (type == LanguageType.ZH) { strComponent = chineseStrCompenent; } else if (type == LanguageType.EN) { strComponent = englishStrComponent; } } }
4. 物体上挂载一个Global.cs类,实现调用
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public enum LanguageType { ZH, EN } public class Global : MonoBehaviour { public Text textView; private LanguageType mLanguage = LanguageType.ZH; private TextManager textManager = new TextManager(); // Use this for initialization void Start () { textManager.ChangeLanguage(mLanguage); } // Update is called once per frame void Update () { textView.text = textManager.strComponent.text_test; } public void SwitchLanguage() { if (mLanguage == LanguageType.ZH) { mLanguage = LanguageType.EN; } else if (mLanguage == LanguageType.EN) { mLanguage = LanguageType.ZH; } textManager.ChangeLanguage(mLanguage); } }
上面的代码是通过点击一个按钮来进行切换语言,进而在Text组件上显示对应语言的文本。
当然根据系统语言来进行切换就可以实现了。
加载全部内容