快盘下载:好资源、好软件、快快下载吧!

快盘排行|快盘最新

当前位置:首页软件教程电脑软件教程 → Unity-本地化插件Localization

Unity-本地化插件Localization

时间:2022-10-24 10:48:41人气:作者:快盘下载我要评论

游戏本地化主要是文字和资源;图片等;随语言变化;比如游戏内一些艺术字使用的是图片;就要根据语言加载不同的图片

安装;配置

Unity-本地化插件Localization
Localization是unity官方推出的本地化插件;通过PackageManager安装

使用前需要先创建本地化配置;Edit > Project Settings > Localization;生成总的配置文件
Localization
Locale Generator用于添加或移出语言;每添加一种语言也会生成对应的配置文件;然后选择默认语言的配置文件;如上图

创建表格

Localization
Window > Asset Management > Localization Tables 创建表格;用于建立不同资源之间的对应关系;就是一个key对应多个语言的资源;可以选择创建文本表或者资源表;写好表名后就可以创建了

文本表;String Table;

Unity
这里先以文本表演示;创建一个表名为MyStringTable的String Table

UI上添加一个TextMeshPro;右键选择Localize
Unity
会自动添加一个脚本;指定表名和Entry Name;也就是表中的key;;然后在场景中挂脚本进行测试

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.ResourceManagement.AsyncOperations;

public class TestLocalization : MonoBehaviour
{
    AsyncOperationHandle m_InitializeOperation;
    private Locale _chineseLocale;
    private Locale _englishLocale;

    void Start()
    {
        // SelectedLocaleAsync will ensure that the locales have been initialized and a locale has been selected.
        m_InitializeOperation = LocalizationSettings.SelectedLocaleAsync;
        if (m_InitializeOperation.IsDone)
        {
            InitializeCompleted(m_InitializeOperation);
        }
        else
        {
            m_InitializeOperation.Completed ;= InitializeCompleted;
        }
    }

    void InitializeCompleted(AsyncOperationHandle obj)
    {
        var locales = LocalizationSettings.AvailableLocales.Locales;
        for (int i = 0; i < locales.Count; ;;i)
        {
            var locale = locales[i];
            if (locale.LocaleName == ;Chinese (Simplified) (zh-Hans);)
            {
                _chineseLocale = locale;
            }
            else if (locale.LocaleName == ;English (en);)
            {
                _englishLocale = locale;
            }
        }
    }

    private void OnGUI()
    {
        if (GUI.Button(new Rect(0, 0, 100, 50),;中文;))
        {
            LocalizationSettings.Instance.SetSelectedLocale(_chineseLocale);
        }
        
        if (GUI.Button(new Rect(0, 60, 100, 50),;英文;))
        {
            LocalizationSettings.Instance.SetSelectedLocale(_englishLocale);
        }
    }
}

Localization
运行时修改Locale就可以改变语言;这种方法适用于界面上的静态文本;也可以在代码中获取对应的文本;需要指定表名和key;如下

using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Localization.Settings;
using UnityEngine.ResourceManagement.AsyncOperations;

public class TestLocalization2 : MonoBehaviour
{
    public TextMeshProUGUI text;
    
    void Start()
    {
        StartCoroutine(LoadStrings());
    }
    
    IEnumerator LoadStrings()
    {
        // A string table may not be immediately available such as during initialization of the localization system or when a table has not been loaded yet.
        var loadingOperation = LocalizationSettings.StringDatabase.GetTableAsync(;MyStringTable;);
        yield return loadingOperation;

        if (loadingOperation.Status == AsyncOperationStatus.Succeeded)
        {
            var stringTable = loadingOperation.Result;
            text.text = stringTable.GetEntry(;id_hello;).GetLocalizedString();
        }
        else
        {
            Debug.LogError(;Could not load String Table
; ; loadingOperation.OperationException.ToString());
        }
    }
}

或者在初始化时通过表名获得该文本表;然后由文本表获取字符串

using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.Localization.Tables;

public class TestLocalization3 : MonoBehaviour
{
    public LocalizedStringTable stringTable = new LocalizedStringTable { TableReference = ;MyStringTable; };
    string m_TranslatedStringHello;

    void OnEnable()
    {
        stringTable.TableChanged ;= LoadStrings;
    }

    void OnDisable()
    {
        stringTable.TableChanged -= LoadStrings;
    }

    void LoadStrings(StringTable stringTable)
    {
        m_TranslatedStringHello = GetLocalizedString(stringTable, ;id_hello;);
    }

    static string GetLocalizedString(StringTable table, string entryName)
    {
        var entry = table.GetEntry(entryName);
        return entry.GetLocalizedString();
    }

    void OnGUI()
    {
        // We can check if the localization system is ready using the InitializationOperation.
        // Initialization involves loading locales and optionally preloading localized data for the current locale.
        if (!LocalizationSettings.InitializationOperation.IsDone)
        {
            GUILayout.Label(;Initializing Localization;);
            return;
        }
        GUILayout.Label(m_TranslatedStringHello);
    }
}

Localization
Window > Asset Management > Localization Scene Controls
编辑器下;我们可以通过Localization Scene Controls来修改当前Locale;便于debug
如果游戏中的文本量不是很大;可以通过Localization的文本表管理;文本量很大的话还是建议通过Excel来管理;自己写一个管理器来加载文本

资源表;Asset Table;

Localization
资源表的逻辑类似;先创建一个MyAssetTable;添加测试数据;因为资源项是Object类型;所以支持任何资源;这里添加两个sprite
Localization
在UI上添加一个Image;右键选择Localize;自动添加脚本;指定表名和key;就可以测试了
Unity
这个适用于界面上的静态图片;Raw Image和Audio Source组件同样支持右键选择Localize

拓展

我想让TextMeshPro的字体也随着语言改变;Localization也提供了接口支持拓展;首先创建三个脚本

using System;

namespace UnityEngine.Localization
{
    [Serializable]
    public class LocalizedTmpFont : LocalizedAsset<TMPro.TMP_FontAsset>
    {
        
    }
}
using System;
using UnityEngine.Events;

namespace UnityEngine.Localization.Events
{
    [Serializable]
    public class UnityEventTmpFont : UnityEvent<TMPro.TMP_FontAsset>
    {
    }
}
using TMPro;
using UnityEngine.Localization.Events;

namespace UnityEngine.Localization.Components
{
    [AddComponentMenu(;Localization/Asset/Localize TmpFont Event;)]
    public class LocalizeTmpFontEvent : LocalizedAssetEvent<TMPro.TMP_FontAsset, LocalizedTmpFont, UnityEventTmpFont>
    {
        private TextMeshProUGUI text;
        private void Awake()
        {
            text = GetComponent<TextMeshProUGUI>();
        }

        protected override void UpdateAsset(TMP_FontAsset localizedAsset)
        {
            base.UpdateAsset(localizedAsset);
            text.font = localizedAsset;
        }
    }
}

Unity
资源表中也添加想要的字体
Unity
最后就可以在TextMeshPro上挂LocalizeTmpFontEvent脚本;指定表名和id
Localization

相关文章

  • Unity中嵌入MySQL数据库

    Unity中嵌入MySQL数据库,注解:mysql - MySQL数据库;①.安装包mysql-for-visualstudio-1.2.10。Connector/NET安装到此结束,至于有啥用我也不知道!②.检测需要的素材包,也可能出现vs版本不对应之类的,悬在上面有提示。④.选择密码验证方式,选项依次是带加密的强密码校验和不带加密的密码验证。①.安装包mysql-connector-net-8.0.31。版本兼容问题:--这个还没解决,挖个坑...啊????。命令行输入:mysql -u root -...
  • FBX文件导入Unity导致贴图丢失问题解决,以3ds-max,Blender导入导出为例

    FBX文件导入Unity导致贴图丢失问题解决,以3ds-max,Blender导入导出为例,关于FBX模型导入Unity之后出现贴图丢失的问题,结合3ds max和Blender进行的解决方案...

网友评论

快盘下载暂未开通留言功能。

关于我们| 广告联络| 联系我们| 网站帮助| 免责声明| 软件发布

Copyright 2019-2029 【快快下载吧】 版权所有 快快下载吧 | 豫ICP备10006759号公安备案:41010502004165

声明: 快快下载吧上的所有软件和资料来源于互联网,仅供学习和研究使用,请测试后自行销毁,如有侵犯你版权的,请来信指出,本站将立即改正。