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<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 update
    void Start()
    {
        m_text = transform.GetChild(0).GetComponent<Text>();
        //调用静态函数   注册事件
        EasyLanguage.GetInstance().ReginsterELT(this);
        //加载中...
        OnLanguageChanged();
    }

    // Update is called once per frame
    void 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=123
    private 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];
        }
    }
}
相关推荐
皮皮陶2 小时前
Unity WebGL交互通信
unity·交互·webgl
程序员正茂2 小时前
PICO+Unity MR空间网格
unity·mr·pico
程序员正茂2 小时前
PICO+Unity MR空间锚点
unity·pico·空间锚点
龙中舞王5 小时前
Unity学习笔记(2):场景绘制
笔记·学习·unity
逐·風10 小时前
Unity编辑器的高级扩展技术
unity·编辑器·游戏引擎
Go_Accepted1 天前
代码修改材质参数
unity
冰凌糕1 天前
Unity3D UI 双击和长按
unity
我与岁月的森林1 天前
Unity常见问题合集(一)
unity
夜梦说开发(VR)1 天前
【Unity】Game Framework框架学习使用
学习·unity·游戏引擎
小白要加油哈2 天前
Mesh网格
学习·unity·游戏引擎