前因:文本中的换行 \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<AudioSource>();chinese_btn.onClick.AddListener(ChineseChanged);ChineseChanged();}/// <summary>/// 中英互译/// </summary>private void ChineseChanged(){if (chinese){chinese_btn.GetComponent<Image>().sprite = Resources.Load<Sprite>("UI/cn");m_language.Changedlanguage(EasyLanguage.LanguageList.cn);Debug.Log("Chinese");chinese = false;}else{chinese_btn.GetComponent<Image>().sprite = Resources.Load<Sprite>("UI/en");m_language.Changedlanguage(EasyLanguage.LanguageList.en);Debug.Log("English");chinese = true;}}/// <summary>/// 音频播放/// </summary>private void Sound(){if(sound){sound_btn.GetComponent<Image>().sprite = Resources.Load<Sprite>("UI/on");audios.volume = 1;sound = false;}else{sound_btn.GetComponent<Image>().sprite = Resources.Load<Sprite>("UI/off");audios.volume = 0;sound = true;}}private void CloseVideo()//关闭视频{for (int i = 0; i < videoPlay.Length; i++){videoPlay[i].gameObject.SetActive(false);}}
}
using 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 updatevoid Start(){m_text = transform.GetChild(0).GetComponent<Text>();//调用静态函数 注册事件EasyLanguage.GetInstance().ReginsterELT(this);//加载中...OnLanguageChanged();}// Update is called once per framevoid Update(){}/// <summary>///unity文本组件没有实现换行的效果/// 因为unity会默认把\n替换成\\n/// m_text.text = m_text.text.Replace("\\n", "\n");/// </summary>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;
/// <summary>
/// 中英互译
/// key --> value
/// 储存
/// </summary>
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=123private Dictionary<string, string> m_dic_elt;//集合private List<EasyLanguageText> m_list_elt;//构造函数EasyLanguage(){//初始化容器m_dic_elt = new Dictionary<string, string>();m_list_elt = new List<EasyLanguageText>();//当前语言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();}/// <summary>/// 语言切换/// </summary>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(); }}/// <summary>/// 读取key/// </summary>/// <param name="key"></param>/// <returns></returns>public string GetTextByKey(string key){return m_dic_elt[key];}/// <summary>///加载语言/// </summary>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];}}
}