268 lines
7.9 KiB
C#
268 lines
7.9 KiB
C#
|
//文件是自动生成,请勿手动修改.此类只有一个接口用于填充字典数据.
|
||
|
using System;
|
||
|
using UnityEngine;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using FLanguage = UnityEngine.Gonbest.MagicCube.FLanguage;
|
||
|
|
||
|
namespace Thousandto.Launcher.Form
|
||
|
{
|
||
|
[ExecuteAlways]
|
||
|
public class UILauncherLangScript : MonoBehaviour
|
||
|
{
|
||
|
#region //静态变量
|
||
|
private static UILauncherLangScript _instance;
|
||
|
public static UILauncherLangScript Instance
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
return _instance;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 翻译字符串
|
||
|
/// </summary>
|
||
|
/// <param name="key"></param>
|
||
|
/// <returns></returns>
|
||
|
public static string ConvertLanText(string key)
|
||
|
{
|
||
|
if (_instance != null && _instance._langDatas.Count > 0)
|
||
|
{
|
||
|
var _chList = _instance._langDatas[0].Contents;
|
||
|
var _resultIndex = _chList.IndexOf(key);
|
||
|
if (_resultIndex >= 0)
|
||
|
{
|
||
|
var langdat = _instance._langDatas.Find(x => { return x.LanKey == FLanguage.Default; });
|
||
|
if (langdat != null && _resultIndex < langdat.Contents.Count)
|
||
|
{
|
||
|
return langdat.Contents[_resultIndex];
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return key;
|
||
|
}
|
||
|
#endregion
|
||
|
|
||
|
#region//序列化变量
|
||
|
[SerializeField]
|
||
|
private List<LanguageData> _langDatas = new List<LanguageData>();
|
||
|
[SerializeField]
|
||
|
private List<UILabel> _labelList = new List<UILabel>();
|
||
|
[SerializeField]
|
||
|
private List<UISprite> _spriteList = new List<UISprite>();
|
||
|
//当前语言的字体
|
||
|
[SerializeField]
|
||
|
private UIFont _curLanFont = null;
|
||
|
//当前图集
|
||
|
[SerializeField]
|
||
|
private UIAtlas _curAtlas = null;
|
||
|
#endregion
|
||
|
|
||
|
|
||
|
|
||
|
#region//MonoBehaviour
|
||
|
private void Awake()
|
||
|
{
|
||
|
LoadAltasAndFont();
|
||
|
}
|
||
|
private void Start()
|
||
|
{
|
||
|
_instance = this;
|
||
|
StartCoroutine(SetAtlasAndFont());
|
||
|
}
|
||
|
|
||
|
private void OnDestroy()
|
||
|
{
|
||
|
_instance = null;
|
||
|
#if !UNITY_EDITOR
|
||
|
if (_curAtlas != null)
|
||
|
{
|
||
|
_curAtlas.DestoryAssets();
|
||
|
_curAtlas = null;
|
||
|
}
|
||
|
if (_curLanFont != null)
|
||
|
{
|
||
|
_curLanFont.DestoryAssets();
|
||
|
_curLanFont = null;
|
||
|
}
|
||
|
#endif
|
||
|
}
|
||
|
#endregion
|
||
|
|
||
|
#region //运行时的处理
|
||
|
|
||
|
|
||
|
//设置图集和字体
|
||
|
private IEnumerator SetAtlasAndFont()
|
||
|
{
|
||
|
yield return null;
|
||
|
//设置字体
|
||
|
if (_labelList.Count > 0)
|
||
|
{
|
||
|
for (int i = 0; i < _labelList.Count; ++i)
|
||
|
{
|
||
|
if (_curLanFont != null)
|
||
|
{
|
||
|
_labelList[i].bitmapFont = _curLanFont;
|
||
|
}
|
||
|
SetStrByLan(_labelList[i]);
|
||
|
if (_labelList[i].TextChangeCallBack == null)
|
||
|
{
|
||
|
_labelList[i].TextChangeCallBack = OnLabelValueChange;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
yield return null;
|
||
|
if (_curAtlas != null && _spriteList.Count > 0)
|
||
|
{
|
||
|
for (int i = 0; i < _spriteList.Count; ++i)
|
||
|
{
|
||
|
_spriteList[i].atlas = _curAtlas;
|
||
|
}
|
||
|
}
|
||
|
yield return null;
|
||
|
}
|
||
|
|
||
|
//设置语言字符串
|
||
|
private void SetStrByLan(UILabel label)
|
||
|
{
|
||
|
if (label == null) return;
|
||
|
label.text = ConvertLanText(label.text);
|
||
|
}
|
||
|
|
||
|
//Label内容改变
|
||
|
private void OnLabelValueChange(UILabel label)
|
||
|
{
|
||
|
SetStrByLan(label);
|
||
|
}
|
||
|
|
||
|
//加载图集和字体
|
||
|
private void LoadAltasAndFont()
|
||
|
{
|
||
|
var _lan = FLanguage.Default;
|
||
|
string fontPath = string.Format("LauncherUI/Base/{0}/Font/LauncherFont", _lan);
|
||
|
var font = Resources.Load<UIFont>(fontPath);
|
||
|
if (font != null)
|
||
|
{
|
||
|
if (_curLanFont != font)
|
||
|
{
|
||
|
#if !UNITY_EDITOR
|
||
|
if(_curLanFont != null)_curLanFont.DestoryAssets();
|
||
|
#endif
|
||
|
_curLanFont = font;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
string atlasPath = string.Format("LauncherUI/Base/{0}/Atlas/Update", _lan);
|
||
|
var atlas = Resources.Load<UIAtlas>(atlasPath);
|
||
|
if (atlas != null)
|
||
|
{
|
||
|
if (_curAtlas != atlas)
|
||
|
{
|
||
|
#if !UNITY_EDITOR
|
||
|
if(_curAtlas != null) _curAtlas.DestoryAssets();
|
||
|
#endif
|
||
|
_curAtlas = atlas;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
#region //编辑器状态下使用
|
||
|
public void ToCollection(Dictionary<string, string[]> dic, List<string> lanList)
|
||
|
{
|
||
|
//1.1检索所有的Label和sprite
|
||
|
_labelList.Clear();
|
||
|
_labelList.AddRange(transform.GetComponentsInChildren<UILabel>(true));
|
||
|
_spriteList.Clear();
|
||
|
_spriteList.AddRange(transform.GetComponentsInChildren<UISprite>(true));
|
||
|
|
||
|
//1.2读取默认的atlas和字体
|
||
|
foreach (var lbl in _labelList)
|
||
|
{
|
||
|
if (lbl.bitmapFont != null)
|
||
|
{
|
||
|
if (_curLanFont == null)
|
||
|
{
|
||
|
_curLanFont = lbl.bitmapFont;
|
||
|
}
|
||
|
else if(_curLanFont != lbl.bitmapFont)
|
||
|
{
|
||
|
Debug.LogError("启动窗体竟然关联两个字体,请检查一下!!"+ lbl.bitmapFont.name+";;;"+lbl.name);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
foreach (var spr in _spriteList)
|
||
|
{
|
||
|
if (spr.atlas != null)
|
||
|
{
|
||
|
if (_curAtlas == null)
|
||
|
{
|
||
|
_curAtlas = spr.atlas;
|
||
|
}
|
||
|
else if (_curAtlas != spr.atlas)
|
||
|
{
|
||
|
Debug.LogError("启动窗体竟然关联两个图集,请检查一下!!"+ spr.atlas.name + ";;;" + spr.name);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//2.初始化所有语言数据
|
||
|
_langDatas.Clear();
|
||
|
_langDatas.Add(new LanguageData(FLanguage.CH));
|
||
|
var cloneList = new List<string>(lanList);
|
||
|
cloneList.Remove(FLanguage.CH);
|
||
|
for (int i = 0; i < cloneList.Count; i++)
|
||
|
{
|
||
|
_langDatas.Add(new LanguageData(cloneList[i]));
|
||
|
}
|
||
|
|
||
|
//3.填充语言数据
|
||
|
var e = dic.GetEnumerator();
|
||
|
try
|
||
|
{
|
||
|
while (e.MoveNext())
|
||
|
{
|
||
|
_langDatas[0].Contents.Add(e.Current.Key);
|
||
|
|
||
|
for (int i = 1; i < _langDatas.Count; i++)
|
||
|
{
|
||
|
var lang = _langDatas[i];
|
||
|
if (e.Current.Value.Length > lang.LanIndex)
|
||
|
lang.Contents.Add(e.Current.Value[lang.LanIndex]);
|
||
|
else
|
||
|
lang.Contents.Add(e.Current.Key);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
e.Dispose();
|
||
|
}
|
||
|
}
|
||
|
#endregion
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
#region//内部数据类
|
||
|
[System.Serializable]
|
||
|
public class LanguageData
|
||
|
{
|
||
|
public string LanKey;
|
||
|
public List<string> Contents;
|
||
|
[NonSerialized]
|
||
|
public int LanIndex;
|
||
|
public LanguageData(string lan)
|
||
|
{
|
||
|
LanKey = lan;
|
||
|
LanIndex = FLanguage.CastToIndex(lan);
|
||
|
Contents = new List<string>();
|
||
|
}
|
||
|
}
|
||
|
#endregion
|
||
|
}
|