赞
踩
游戏本地化主要是文字和资源(图片等)随语言变化,比如游戏内一些艺术字使用的是图片,就要根据语言加载不同的图片
Localization是unity官方推出的本地化插件,通过PackageManager安装
使用前需要先创建本地化配置,Edit > Project Settings > Localization,生成总的配置文件
Locale Generator用于添加或移出语言,每添加一种语言也会生成对应的配置文件,然后选择默认语言的配置文件,如上图
Window > Asset Management > Localization Tables 创建表格,用于建立不同资源之间的对应关系,就是一个key对应多个语言的资源,可以选择创建文本表或者资源表,写好表名后就可以创建了
这里先以文本表演示,创建一个表名为MyStringTable的String Table
UI上添加一个TextMeshPro,右键选择Localize
会自动添加一个脚本,指定表名和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); } } }
运行时修改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\n" + 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); } }
Window > Asset Management > Localization Scene Controls
编辑器下,我们可以通过Localization Scene Controls来修改当前Locale,便于debug
如果游戏中的文本量不是很大,可以通过Localization的文本表管理,文本量很大的话还是建议通过Excel来管理,自己写一个管理器来加载文本
资源表的逻辑类似,先创建一个MyAssetTable,添加测试数据,因为资源项是Object类型,所以支持任何资源,这里添加两个sprite
在UI上添加一个Image,右键选择Localize,自动添加脚本,指定表名和key,就可以测试了
这个适用于界面上的静态图片,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; } } }
资源表中也添加想要的字体
最后就可以在TextMeshPro上挂LocalizeTmpFontEvent脚本,指定表名和id
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。