using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LanguageMgr : MonoBehaviour
{
private static LanguageMgr instance = null;
public static LanguageMgr Instance
{
get { return instance; }
}
//所有的需要修改的文本或图片上挂在的脚本
private UIText m_text_1;
private UIText m_text_2;
private UIText m_text_3;
private UIImage m_img_1;
private UIImage m_img_2;
private UIImage m_img_3;
private SystemLanguage m_nowLanguage = SystemLanguage.Unknown;
// 语言
//[SerializeField]
//private SystemLanguage language;
// 相同的key 对应 不同国家的value
private Dictionary<string, string> dict = new Dictionary<string, string>();
void Awake()
{
instance = this;
m_text_1 = GameObject.Find("Text").GetComponent<UIText>();
m_text_2 = GameObject.Find("Text1").GetComponent<UIText>();
m_text_3 = GameObject.Find("Text2").GetComponent<UIText>();
m_img_1 = GameObject.Find("Image1").GetComponent<UIImage>();
m_img_2 = GameObject.Find("Image2").GetComponent<UIImage>();
m_img_3 = GameObject.Find("Image3").GetComponent<UIImage>();
}
private void Start()
{
}
/// <summary>
/// 加载预翻译的语言
/// </summary>
public void LoadLanguage(SystemLanguage language)
{
if ((m_nowLanguage == SystemLanguage.Chinese && language == SystemLanguage.Chinese) || (m_nowLanguage == SystemLanguage.Chinese && language == SystemLanguage.Chinese))
{
return;
}
else
{
if ((m_nowLanguage == SystemLanguage.Chinese || m_nowLanguage == SystemLanguage.English) && dict.Count > 0)
{
dict.Clear();
}
}
//加载文件
//TextAsset ta = Resources.Load<TextAsset>(language.ToString());
TextAsset ta = null;
if (language == SystemLanguage.Chinese)
{
ta = Resources.Load<TextAsset>("Chinese/Txt/" + language.ToString());
m_nowLanguage = SystemLanguage.Chinese;
}
else if (language == SystemLanguage.English)
{
ta = Resources.Load<TextAsset>("English/Txt/" + language.ToString());
m_nowLanguage = SystemLanguage.English;
}
if (ta == null)
{
Debug.LogWarning("没有这个语言的翻译文件");
return;
}
//获取每一行
string[] lines = ta.text.Split('\r', '\n');
//获取key value
for (int i = 0; i < lines.Length; i++)
{
//检测
if (string.IsNullOrEmpty(lines[i]))
continue;
//获取 key:kv[0] value kv[1]
string[] kv = lines[i].Split(':');
//保存到字典
dict.Add(kv[0], kv[1]);
Debug.Log(string.Format("key:{0}, value:{1}", kv[0], kv[1]));
}
m_text_1.ChangLanguage(m_text_1.key);
m_text_2.ChangLanguage(m_text_2.key);
m_text_3.ChangLanguage(m_text_3.key);
m_img_1.ChangLanguage(m_img_1.key);
m_img_2.ChangLanguage(m_img_2.key);
m_img_3.ChangLanguage(m_img_3.key);
}
/// <summary>
/// 获取对应的value
/// </summary>
/// <param name="key">键</param>
/// <returns>返回对应的value 如果不存在这个key 就返回空串</returns>
public string GetText(string key)
{
if (dict.ContainsKey(key))
return dict[key];
else//没有这个key
{
return string.Empty;
}
}
}
//=========挂载到需要更改的图片上=============
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIImage : MonoBehaviour
{
public string key;
//private Sprite spr;
public void ChangLanguage(string key)
{
if (!string.IsNullOrEmpty(key))
{
string value = LanguageMgr.Instance.GetText(key);
if (!string.IsNullOrEmpty(value))
{
gameObject.GetComponent<Image>().sprite = Resources.Load<Sprite>(value);
}
}
}
}
============挂在到需要更改的text上===============
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using System.IO;
using System.Text;
public class UIText : MonoBehaviour
{
public string key;
public void ChangLanguage(string key)
{
if (!string.IsNullOrEmpty(key))
{
string value = LanguageMgr.Instance.GetText(key);
if (!string.IsNullOrEmpty(value))
{
gameObject.GetComponent<Text>().text = value;
}
}
}
}