Unity之Text组件换行\n没有实现+动态中英互换
前因:文本中的换行 \n没有换行而是打印出来了,解决方式
因为unity会默认把\n替换成\\n
面板中使用富文本这个选项啊 没有用
m_text.text = m_text.text.Replace("\\n", "\n");
###动态中英文互译
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using UnityEngine.Video; using System; using static EasyLanguage; public class UIManage : MonoBehaviour//, IPointerDownHandler, IPointerUpHandler { public VideoPlayer[] videoPlay; //实例 private EasyLanguage m_language; //中英按钮 public Button chinese_btn; public Button sound_btn; private AudioSource audios; private bool sound; private bool chinese=true;//中文 private void Awake() { m_language = EasyLanguage.GetInstance(); } void Start() { audios = GetComponent(); chinese_btn.onClick.AddListener(ChineseChanged); ChineseChanged(); } /// /// 中英互译 /// private void ChineseChanged() { if (chinese) { chinese_btn.GetComponent().sprite = Resources.Load("UI/cn"); m_language.Changedlanguage(EasyLanguage.LanguageList.cn); Debug.Log("Chinese"); chinese = false; } else { chinese_btn.GetComponent().sprite = Resources.Load("UI/en"); m_language.Changedlanguage(EasyLanguage.LanguageList.en); Debug.Log("English"); chinese = true; } } /// /// 音频播放 /// private void Sound() { if(sound) { sound_btn.GetComponent().sprite = Resources.Load("UI/on"); audios.volume = 1; sound = false; } else { sound_btn.GetComponent().sprite = Resources.Load("UI/off"); audios.volume = 0; sound = true; } } private void CloseVideo()//关闭视频 { for (int i = 0; iusing System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class EasyLanguageText : MonoBehaviour { public string m_key; private Text m_text; // Start is called before the first frame update void Start() { m_text = transform.GetChild(0).GetComponent(); //调用静态函数 注册事件 EasyLanguage.GetInstance().ReginsterELT(this); //加载中... OnLanguageChanged(); } // Update is called once per frame void Update() { } /// ///unity文本组件没有实现换行的效果 /// 因为unity会默认把\n替换成\\n /// m_text.text = m_text.text.Replace("\\n", "\n"); /// public void OnLanguageChanged() { m_text.text = EasyLanguage.GetInstance().GetTextByKey(m_key); m_text.text = m_text.text.Replace("\\n", "\n"); } }using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; /// /// 中英互译 /// key --> value /// 储存 /// public class EasyLanguage { #region 单例 //静态函数 private static EasyLanguage m_instance; //实例 public static EasyLanguage GetInstance() { if (m_instance == null) { m_instance = new EasyLanguage(); } return m_instance; } #endregion //键值储存 key=str value=123 private Dictionary m_dic_elt; //集合 private List m_list_elt; //构造函数 EasyLanguage() { //初始化容器 m_dic_elt = new Dictionary(); m_list_elt = new List(); //当前语言 m_currentLanguage = LanguageList.cn; //更新语言 loadLanguage(); OnLanguageChanged(); } //事件:注册 public void ReginsterELT(EasyLanguageText elt) { m_list_elt.Add(elt); } //事件:销毁 public void UnReginsterELT(EasyLanguageText elt) { m_list_elt.Remove(elt); } //枚举 语言类型 public enum LanguageList { en,//英文 cn//中文 } //当前语言类型 private LanguageList m_currentLanguage; //获取当前语言 public void Changedlanguage(LanguageList ll) {//当前值改成传入的 Debug.Log(m_currentLanguage+"-->"+ll); if (m_currentLanguage == ll) return; m_currentLanguage = ll; m_dic_elt.Clear(); loadLanguage(); OnLanguageChanged(); } /// /// 语言切换 /// public void OnLanguageChanged() { //if (m_currentLanguage == LanguageList.cn) //{ // m_dic_elt.Add("h3", "yuan庄"); // m_dic_elt.Add("h4", "lu庄"); //} //else //{ // m_dic_elt.Add("h3", "yuanzhuang"); // m_dic_elt.Add("h4", "luzhuang"); //} Debug.Log("OnLanguageChanged" ); foreach (var elt in m_list_elt) { elt.OnLanguageChanged(); } } /// /// 读取key /// /// /// public string GetTextByKey(string key) { return m_dic_elt[key]; } /// ///加载语言 /// public void loadLanguage() { switch (m_currentLanguage) { case LanguageList.en: //英文 StreamIOText("en"); break; case LanguageList.cn: //中文 StreamIOText("cn"); break; default: break; } } public void StreamIOText(string chinese) { TextAsset asset = Resources.Load(chinese) as TextAsset; //流取出字节 Stream st = new MemoryStream(asset.bytes); //逐行读取 StreamReader sr = new StreamReader(st); //每行都打印 只要不是最后一行 while (!sr.EndOfStream) { string line = sr.ReadLine(); Debug.Log(line); string[] tempStrings = line.Split('='); m_dic_elt[tempStrings[0]] = tempStrings[1]; } } }
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。