IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> Unity多语言(基于ET框架) -> 正文阅读

[游戏开发]Unity多语言(基于ET框架)

1.介绍

1.?代码枚举加语言(编辑器加语言)

2.一张Excle表配置所有语言

3.静态文本加载(编辑模式下可根据key看到value,Default Language为value的语言和游戏中的Language不同

4.动态文本加载?

?

2.核心代码示例

?1.LocalizationService.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using UnityEngine;

namespace ETModel
{
   
    public class LocalizationService : ScriptableObject
    {
        #region static
        private static LocalizationService _instance;
        public static LocalizationService Instance
        {
            get { return _instance ?? (_instance = Create()); }
        }

        /// <summary>
        /// LocalizationService的asset文件
        /// </summary>
        /// <returns></returns>
        static LocalizationService Create()
        {
            return Resources.Load<LocalizationService>("LocalizationService");
        }

        #endregion

        #region event
        /// <summary>
        /// 语言改变事件
        /// </summary>
        public static Action OnLanguageChanged;
        #endregion

        #region properties

        /// <summary>
        /// 存储所有语言信息
        /// </summary>
        public Dictionary<long, string> Strings = new Dictionary<long, string>();      

        /// <summary>
        /// 默认显示静态文本语言
        /// </summary>
        [HideInInspector]
        public LanguageInfo DefaultLanguage;

        /// <summary>
        /// 当前语言
        /// </summary>
        [HideInInspector]
        private LanguageInfo _language = LanguageInfo.Chinese;
        public LanguageInfo Language
        {
            get { return _language; }
            set
            {
                if (!HasLanguage(value))
                {
                    Debug.LogError("Invalid Language " + value);
                }

                _language = value;

                RaiseLanguageChanged();

                SaveToPrefs();
            }
        }

        /// <summary>
        /// 所有支持的语言
        /// </summary>
        [SerializeField]
        public LanguageInfo[] Languages = LanguageInfo.All;
        #endregion

        #region LocalizationService       

        public void Init()
        {
            LoadLanguage();

#if UNITY_EDITOR
            if (!UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
            {
                return;
            }
#endif
         //   ReadFiles();
        }

        /// <summary>
        /// 默认加载语言
        /// </summary>
        void LoadLanguage()
        {
            var raw = PlayerPrefs.GetString("CurrentLanguage");
            if (string.IsNullOrEmpty(raw))
            {
                bool hasLan = false;
                for (int i = 0; i < Languages.Length; i++)
                {
                    if (Application.systemLanguage == Languages[i].lan)
                    {
                        hasLan = true;
                        Language = Languages[i];
                        break;
                    }
                }
                if (!hasLan)
                {
                    Language = LanguageInfo.Chinese;
                }
                return;
            }


            var lan = Languages.FirstOrDefault(o => o.Name == raw);
            if (lan == null)
            {
                Debug.LogError("Unknown language saved to prefs : " + raw);
                Language = LanguageInfo.Chinese;
            }
            else
            {
                Language = lan;
            }
        }

        /// <summary>
        /// 保存到本地
        /// </summary>
        void SaveToPrefs()
        {
            PlayerPrefs.SetString("CurrentLanguage", Language.Name);
            PlayerPrefs.Save();
        }

        #endregion

        #region internal

        /// <summary>
        /// 是否有配置该语言
        /// </summary>
        /// <param name="language"></param>
        /// <returns></returns>
        bool HasLanguage(LanguageInfo language)
        {
            foreach (var systemLanguage in Languages)
            {
                if (systemLanguage.Equals(language))
                    return true;
            }
            return false;
        }


        /// <summary>
        /// 切换语言
        /// </summary>
        private void RaiseLanguageChanged()
        {
            
            if (Application.isPlaying)
            {
                ReadFiles();
                if (OnLanguageChanged != null)
                    OnLanguageChanged();
            }
        }

        /// <summary>
        /// 读取当前语言配置(ET为例)
        /// </summary>
        void ReadFiles()
        {
            Strings.Clear();

            var configs = Game.Scene.GetComponent<ConfigComponent>().GetAll(typeof(Localization));

            if (!configs.Any())
            {
                Debug.LogError("Localization Files Not Found : " + Language.Name);
            }


            foreach (Localization config in configs)
            {
                ReadCSVAsset(config);
            }
        }

        /// <summary>
        /// 读取单条配置到缓存
        /// </summary>
        /// <param name="config"></param>
        void ReadCSVAsset(Localization config)
        {
            try
            {
                FieldInfo info = typeof(Localization).GetField(Language.Name);
                Strings.Add(config.Id, info.GetValue(config).ToString());

            }
            catch (Exception ex)
            {
                Debug.LogError(ex);
                Debug.LogError(string.Format("Failed to read file : {0}/{1}  ", Language, config.Id));
            }

        }
        #endregion

        #region public

        /// <summary>
        /// 非运行模式下加载文本TODO
        /// </summary>
        public void LoadTextAssets()
        {
            // ReadFiles();
#if UNITY_EDITOR
            Strings.Clear();
            var jsonObj = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Res/Config/Localization.txt", typeof(TextAsset)) as TextAsset;
            string[] jsonArray = jsonObj.text.Split('\n');
            foreach (string tx in jsonArray)
            {
                if (string.IsNullOrEmpty(tx))
                    continue;
                Localization json = JsonHelper.FromJson<Localization>(tx);
                FieldInfo info = typeof(Localization).GetField(DefaultLanguage.Name);
                Strings.Add(json.Id, info.GetValue(json).ToString());
            }           
#endif
        }

        /// <summary>
        /// 从字典获取多语言文本
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string Get(long key)
        {
            return Get(key, string.Empty);
        }

        /// <summary>
        /// 获取多语言文本,没有则返回默认值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="fallback"> if not found</param>
        /// <returns></returns>
        public string Get(long key, string fallback)
        {
            if (!Strings.ContainsKey(key))
            {
             //   Debug.LogWarning(string.Format("Localization Key Not Found {0} : {1} ", Language.Name, key));
                return fallback;
            }

            return Strings[key];
        }


        #endregion


    }
}

2.LocalizationServiceEditor.cs

using ETModel;
using System;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace ETEditor
{
    /// <summary>
    /// 绘制LocalizationService面板
    /// </summary>
    [CustomEditor(typeof(LocalizationService), true)]
    public class LocalizationServiceEditor : UnityEditor.Editor
    {
        protected LocalizationService Target;

        
        [MenuItem("Tools/Foundation/Localization Service")]
        public static void ShowWindow()
        {
            LocalizationInitializer.Startup();
            Selection.activeObject = LocalizationService.Instance;
            EditorGUIUtility.PingObject(LocalizationService.Instance);

        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            LocalizationInitializer.Startup();

            Target = target as LocalizationService;

            if (Application.isPlaying)
                return;

            EditorGUILayout.LabelField("Default Language");
            var di = Array.IndexOf(Target.Languages, Target.DefaultLanguage);
            var di2 = EditorGUILayout.Popup(di, Target.Languages.Select(o => o.Name).ToArray());

            if (di != di2)
            {
                Target.DefaultLanguage = Target.Languages[di2];
                Target.LoadTextAssets();
                EditorUtility.SetDirty(target);
            }

            if (GUILayout.Button("Reset Language List"))
            {
                Target.Languages = LanguageInfo.All;
                Debug.LogError(LanguageInfo.All[0].Name);
                Debug.LogError(LanguageInfo.All[1].Name);
                EditorUtility.SetDirty(target);
            }
        }
    }
}

3.LocalizedText.cs

using UnityEngine;
using UnityEngine.UI;

namespace ETModel
{
    /// <summary>
    /// 静态文本
    /// </summary>
    [RequireComponent(typeof(Text))]
    [AddComponentMenu("Localization/LocalizedText")]
    public class LocalizedText : MonoBehaviour
    {

        [HideInInspector]
        public long Key;

        private void Awake()
        {
            OnLocalization();
            LocalizationService.OnLanguageChanged += OnLocalization;
        }

        private void OnDestroy()
        {
            LocalizationService.OnLanguageChanged -= OnLocalization;
        }

        public void OnLocalization()
        {
            var label = GetComponent<Text>();

            label.text = Game.Scene.GetComponent<LocaliztionComponent>().GetTextByKey(Key);
        }

    }
}

4.LocalizedTextEditor.cs

using ETModel;
using System;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace ETEditor
{
    /// <summary>
    /// LocalizedText面板
    /// </summary>
    [CustomEditor(typeof(LocalizedText), true)]
    public class LocalizedTextEditor : UnityEditor.Editor
    {
        protected LocalizedText Target;

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

         //   LocalizationInitializer.Startup();

            Target = target as LocalizedText;

            if (Application.isPlaying)
                return;

            var service = LocalizationService.Instance;

            if (service != null)
            {
                var p = EditorGUILayout.LongField("Key", Target.Key);

                if (p != Target.Key)
                {
                    Target.Key = p;
                    EditorUtility.SetDirty(target);
                }
                EditorGUILayout.LabelField("Value", service.Get(p));
            }
           
        }
    }
}


5.LocaliztionComponent.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace ETModel
{
    [ObjectSystem]
    public class LocaliztionAwakeSystem : AwakeSystem<LocaliztionComponent>
    {
        public override void Awake(LocaliztionComponent self)
        {
            LocalizationService.Instance.Init();
        }
    }

    [ObjectSystem]
    public class LocaliztionDestroySystem : DestroySystem<LocaliztionComponent>
    {
        public override void Destroy(LocaliztionComponent self)
        {
            if (LocalizationService.Instance.Language != LocalizationService.Instance.DefaultLanguage)
                LocalizationService.Instance.LoadTextAssets();
        }
    }
    public class LocaliztionComponent : Component
    {
        /// <summary>
        /// 获取文本
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetTextByKey(long key)
        {
            return LocalizationService.Instance.Get(key);
        }

        /// <summary>
        /// 切换语言
        /// </summary>
        /// <param name="info"></param>
        public void ChangeLanguage(LanguageInfo info)
        {
            LocalizationService.Instance.Language = info;
        }

        /// <summary>
        /// 动态文本如果切换语言时没有手动刷新界面的话,需要加上这个回调强制刷新
        /// </summary>
        /// <param name="act"></param>
        public void OnLanguageChanged(Action act)
        {
            LocalizationService.OnLanguageChanged += act;
        }
    }
}

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-11-25 08:26:52  更:2021-11-25 08:27:04 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/27 23:31:26-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码