wangguan
2020-12-18 516ec29be5c667d9a7bd51edf08567674a166372
多语言脚本
7 files added
1 files modified
177 ■■■■■ changed files
Assets/Scripts/Language.meta 8 ●●●●● patch | view | raw | blame | history
Assets/Scripts/Language/LanguageManager.cs 37 ●●●●● patch | view | raw | blame | history
Assets/Scripts/Language/LanguageManager.cs.meta 11 ●●●●● patch | view | raw | blame | history
Assets/Scripts/Language/LocalizationText.cs 46 ●●●●● patch | view | raw | blame | history
Assets/Scripts/Language/LocalizationText.cs.meta 11 ●●●●● patch | view | raw | blame | history
Assets/Scripts/Language/MultiLangConst.cs 52 ●●●●● patch | view | raw | blame | history
Assets/Scripts/Language/MultiLangConst.cs.meta 11 ●●●●● patch | view | raw | blame | history
Assets/Scripts/Manager/ManagerRoot.cs 1 ●●●● patch | view | raw | blame | history
Assets/Scripts/Language.meta
New file
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 624d341d67611384c98ff6cb289dba7f
folderAsset: yes
DefaultImporter:
  externalObjects: {}
  userData:
  assetBundleName:
  assetBundleVariant:
Assets/Scripts/Language/LanguageManager.cs
New file
@@ -0,0 +1,37 @@
using UnityEngine;
/// <summary>
/// 多语言管理器
/// </summary>
public class LanguageManager : MonoBehaviour
{
    private static LanguageManager _ins;
    public static LanguageManager Ins
    {
        get { return _ins; }
    }
    private void Awake()
    {
        _ins = this;
        MultiLangConst.ChangeLanguage(LanguageEnum.CN);//设置为中文
        MultiLangConst.LoadLanguageData();//初始化
    }
    public delegate void OnLanguageChanged();
    public static OnLanguageChanged onLanguageChanged = null;
    /// <summary>
    /// 修改语音入口函数
    /// </summary>
    /// <param name="languageType"></param>
    public void SetupLanguage(LanguageEnum languageType)
    {
        MultiLangConst.ChangeLanguage(languageType);
        if (onLanguageChanged != null)
        {
            onLanguageChanged();
        }
    }
}
Assets/Scripts/Language/LanguageManager.cs.meta
New file
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ca2f3e9f5b36fba4c8027c6e3be9a4b5
MonoImporter:
  externalObjects: {}
  serializedVersion: 2
  defaultReferences: []
  executionOrder: 0
  icon: {instanceID: 0}
  userData:
  assetBundleName:
  assetBundleVariant:
Assets/Scripts/Language/LocalizationText.cs
New file
@@ -0,0 +1,46 @@

using UnityEngine;
using UnityEngine.UI;
using TMPro;
/// <summary>
/// 修改文字脚本
/// </summary>
public class LocalizationText : MonoBehaviour
{
    [SerializeField, Header("Langkey")]
    private string key = " ";
    private Text _text;
    private TextMeshProUGUI _textMeshPro;
    void Awake()
    {
        LanguageManager.onLanguageChanged += onLanguageChanged;
    }
    private void OnDestroy()
    {
        LanguageManager.onLanguageChanged -= onLanguageChanged;
    }
    /// <summary>
    /// Start is called on the frame when a script is enabled just before
    /// any of the Update methods is called the first time.
    /// </summary>
    void Start()
    {
        _text = GetComponent<Text>();
        _textMeshPro = GetComponent<TextMeshProUGUI>();
        onLanguageChanged();
    }
    void onLanguageChanged()
    {
        if (_text != null)
        {
            _text.text = MultiLangConst.GetValue(key);
        }
        else if (_textMeshPro != null)
        {
            _textMeshPro.text = MultiLangConst.GetValue(key);
        }
    }
}
Assets/Scripts/Language/LocalizationText.cs.meta
New file
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: da3b9b249703f6744a34f3cc4f40f3c5
MonoImporter:
  externalObjects: {}
  serializedVersion: 2
  defaultReferences: []
  executionOrder: 0
  icon: {instanceID: 0}
  userData:
  assetBundleName:
  assetBundleVariant:
Assets/Scripts/Language/MultiLangConst.cs
New file
@@ -0,0 +1,52 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum LanguageEnum
{
    CN,//中文
    En,//英文
}
public static class MultiLangConst
{
    static Dictionary<string, string> LangMap_en = new Dictionary<string, string>();
    static Dictionary<string, string> LangMap_cn = new Dictionary<string, string>();
    static LanguageEnum Languagetype = LanguageEnum.CN;
    public static void ChangeLanguage(LanguageEnum languageType)
    {
        Languagetype = languageType;
    }
    /// <summary>
    /// 获取文字
    /// </summary>
    /// <param name="languageType"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    public static string GetValue(string key)
    {
        if (Languagetype == LanguageEnum.En && LangMap_en.ContainsKey(key))
        {
            //英语
            return LangMap_en[key];
        }
        else if (Languagetype == LanguageEnum.CN && LangMap_cn.ContainsKey(key))
        {
            //中文
            return LangMap_cn[key];
        }
        return "N/A";
    }
    /// <summary>
    /// 初始化读取所有语音
    /// </summary>
    public static void LoadLanguageData()
    {
        Debug.Log("开始读取多语言脚本");
    }
}
Assets/Scripts/Language/MultiLangConst.cs.meta
New file
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fb8ef7f004060c848b11e2bd02ae0ba5
MonoImporter:
  externalObjects: {}
  serializedVersion: 2
  defaultReferences: []
  executionOrder: 0
  icon: {instanceID: 0}
  userData:
  assetBundleName:
  assetBundleVariant:
Assets/Scripts/Manager/ManagerRoot.cs
@@ -33,6 +33,7 @@
                root.AddComponent<AudioSourceManager>();
                root.AddComponent<JsonDataReader>();
                root.AddComponent<JsonDataInit>();
                root.AddComponent<LanguageManager>();
                // root.AddComponent<ErrorLogOnGUIMyTools>();
            }