1、Localization的安装和使用
1、首先到unity的Package Manager中安装Localization
2、然后在ProjectSetting中找到Localization,然后按照下面图片顺序,添加需要的多语言语种,其中点击Locale Generator时会弹出一个地址,让你选择保存文件位置
3、按照下面图片路径打开LocalizationTable 配置本地化文件表
填好名字后点击Create(此处选择的为String类型,选择Asset可配置资产本地化,比如可以同一key值对应多种图片,可根据同一key加载不同图片等其他资源,此项目只介绍文本本地化)
然后进入下面分页,点击Add New Entry可添加多语言配置
右击窗口标题 可以将配置的多语言 导入和到处为csv,这样直接在外部配置好导入进来,更方便操作
4、下面是静态文本的本地化配置方法
找到静态文本Text,点击Localize会自动生成一个组件,下面就是
然后在图片位置,搜索你的文本表中Key值填入其中,下面就会全部自动填充好这个Key对应的所有语种本地化
5、下面是动态文本的管理
直接上代码
using System.Collections;
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.Localization.Tables;
using System.Collections.Generic;
using System;
using static Unity.VisualScripting.Icons;
public class LanguageManager : Singleton<LanguageManager>
{
AsyncOperationHandle m_InitializeOperation;
private Dictionary<Language, Locale> languageDic = new Dictionary<Language, Locale>();
private StringTable languageTable;
[NonSerialized]
public Language CurLanguage;
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;
}
StartCoroutine(LoadTable());
}
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-CN)")
{
languageDic.Add(Language.ZH_CN, locale);
}
else if (locale.LocaleName == "Chinese (Traditional) (zh-TW)")
{
languageDic.Add(Language.ZH_TW, locale);
}
else if (locale.LocaleName == "English (United States) (en-US)")
{
languageDic.Add(Language.EN, locale);
}
else if (locale.LocaleName == "Japanese (Japan) (ja-JP)")
{
languageDic.Add(Language.JP, locale);
}
else if (locale.LocaleName == "Korean (South Korea) (ko-KR)")
{
languageDic.Add(Language.KR, locale);
}
else if (locale.LocaleName == "French (France) (fr-FR)")
{
languageDic.Add(Language.FR, locale);
}
else if (locale.LocaleName == "German (Germany) (de-DE)")
{
languageDic.Add(Language.DE, locale);
}
else if (locale.LocaleName == "Italian (Italy) (it-IT)")
{
languageDic.Add(Language.IT, locale);
}
else
{
Debug.LogError("语言表名称出错:" + locale.LocaleName);
}
}
}
IEnumerator LoadTable()
{
var loadingOperation = LocalizationSettings.StringDatabase.GetTableAsync("LanguageTable");
yield return loadingOperation;
if (loadingOperation.Status == AsyncOperationStatus.Succeeded)
{
languageTable = loadingOperation.Result;
CurLanguage = (Language)PlayerPrefs.GetInt("CURRENT_LANGUAGE", (int)Language.EN);
LocalizationSettings.Instance.SetSelectedLocale(languageDic[CurLanguage]);
}
else
{
Debug.LogError("Could not load String Table\n" + loadingOperation.OperationException.ToString());
}
}
/// <summary>
/// 获取翻译文本
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public string GetString(string str)
{
if(languageTable != null)
{
return languageTable.GetEntry(str).GetLocalizedString();
}
else
{
return str;
}
}
public void SwitchLanguage(Language language)
{
if(CurLanguage == language) return;
LocalizationSettings.Instance.SetSelectedLocale(languageDic[language]);
StartCoroutine(LoadTable());
CurLanguage = language;
PlayerPrefs.SetInt("CURRENT_LANGUAGE", (int)CurLanguage);
}
}
public enum Language
{
EN,
ZH_CN,
ZH_TW,
JP,
KR,
FR,
DE,
IT,
None
}
动态文本直接调用该脚本中获取文本的方法即可,且其中包括切换语言,保存当前语言功能
Over~
创作不易 记得点赞哦~