游戏文本本地化【Unity / Luban / QFramework】

本文介绍了如何利用Luban处理Excel数据并将其转换为Json,配合QFramework的事件机制实现语言的实时切换。通过创建本地化控制器和TextLocalizationModule组件,为Unity项目中的UI元素提供多语言支持。
摘要由CSDN通过智能技术生成

基于 Luban + QFramework 的本地化

Luban:主要用于将 Excel 数据转换为可供游戏使用的数据(如 Json 格式),因此可将各国语言存储在 Excel 中便于管理,可用其他同类方案代替

QFramework:本案例主要使用它的事件机制(观察者模式),实现语言的即时切换,可用其他同类方案代替

一、建立多语言 Excel 表

##varidsimplified_chineseenglish
##typeintstringstring
10000穿甲弹Pierce Bullet
10001医疗包Medical Kit
10002兴奋剂Dope
10003荆棘Thorn
10005近视眼Myopia
10006印钞机Money Printing Machine
10007撬棍Crowbar
10008弹匣Gun Magazine

然后通过 Luban 生成代码:

  • 该表作为数据表 local.TbTextLocalization.cs

  • 该表的条目的对象为:LocalizedText.cs(大致内容如下)

    public sealed partial class LocalizedText : Luban.BeanBase
    {
        public readonly int Id;
        public readonly string SimplifiedChinese;
        public readonly string English;
    }
    

二、创建本地化控制器/文本本地化模块组件

using cfg;

namespace XXX
{
    public enum Language
    {
        SimplifiedChinese,
        English,
    }

    public static class LocalizationController
    {
        private static Language _curLanguage = Language.SimplifiedChinese;
        public static Language CurLanguage
        {
            get => _curLanguage;
            set
            {
                if (_curLanguage == value) return;
                
                _curLanguage = value;
                // QFramework 中提供的事件发送,用于通知所有文本本地化模块“当前语言已改变”
                XXXGame.Interface.SendEvent<CurrentLanguageChangedEvent>();
            }
        }

        public static string GetLocalizedText(int id)
        {
            // TbTextLocalization 为 Luban 生成的表数据
            LocalizedText lt = TableController.Tables.TbTextLocalization[id];
            return CurLanguage switch
            {
                Language.SimplifiedChinese => lt.SimplifiedChinese,
                Language.English => lt.English,
                _ => lt.English,
            };
        }
    }
}

文本本地化模块组件用于附加在普通的 Text 或 Text Mesh Pro 组件上,为其附加本地化功能

using QFramework;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace XXX
{
    public class TextLocalizationModule : MonoBehaviour, ICanRegisterEvent
    {
        [SerializeField]
        private int _id;
        public int Id
        {
            get { return _id; }
            set 
            { 
                _id = value;
                UpdateText();
            }
        }
        
        private Text _textComponent;
        private TextMeshPro _tmp;

        private void Awake()
        {
            _textComponent = GetComponent<Text>();
            _tmp = GetComponent<TextMeshPro>();
			
            // QFramework 中提供的事件注册:每当语言更改便触发 CurrentLanguageChangedEvent
            this.RegisterEvent<CurrentLanguageChangedEvent>(_ =>
            {
                UpdateText();
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }

        private void UpdateText()
        {
            if (_textComponent != null)
            {
                _textComponent.text = LocalizationController.GetLocalizedText(Id);
            }
            else if (_tmp != null)
            {
                _tmp.text = LocalizationController.GetLocalizedText(Id);
            }
        }

        public IArchitecture GetArchitecture()
        {
            return XXXGame.Interface;
        }
    }
}

三、使用

对于固定的 UI 元素,在其文本 UI 组件中附上刚才的“文本本地化模块组件”脚本组件,并在编辑器中指定对应的 Id 值即可;对于预制体中文本 UI 则用代码的方式指定对应的 Id 值即可。

当当前语言设置切换时可以即时切换画面中文本语言(由于游戏中敏感信息,效果图在脱敏制作后上传)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值