【问题标题】:How can i get locale info in Unity C#?如何在 Unity C# 中获取语言环境信息?
【发布时间】:2023-03-08 07:28:02
【问题描述】:

我正在使用 Unity 2019.2b 开发一个项目,它预计适用于所有平台(android、ios、windows、macos),我正在尝试获取设备区域设置信息(例如 en-us、en-au、en -bz)但我能找到的只是 Application.systemLanguage 定义。即使我决定使用这些信息,我仍然需要地区信息。是否有任何示例或方法可以在 Unity 中获取语言环境信息或获取设备区域信息? (如果仅针对区域信息的解决方案;我不想使用设备位置或不想使用用户 IP 地址,我想从设置中获取)

我尝试使用 RegionInfo、CultureInfo、System.Threading.Thread.CurrentThread.CurrentUICulture 和 System.Threading.Thread.CurrentThread.CurrentCulture 定义获取语言环境或至少设备区域信息,但没有成功。

string regionName = System.Globalization.RegionInfo.CurrentRegion.Name;
string cultureName = System.Globalization.CultureInfo.CurrentCulture.Name;
string cname = System.Threading.Thread.CurrentThread.CurrentCulture.Name;
string uiname = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;

取决于平台(ios、android、windows、macos),有时结果是 InvariantCulture,有时总是 en-US,即使设备区域设置不是 en-US。

【问题讨论】:

标签: c# unity3d locale region


【解决方案1】:

问题来自 Unity 使用的 Mono。

我在这个forum thread 中找到了一些解决方案。

如果您使用的是 Windows,一种解决方法可能是使用 PInvoke:

 [System.Runtime.InteropServices.DllImport("KERNEL32.DLL")]
 private static extern int GetSystemDefaultLCID();

然后像这样检查全球文化:

var currentCulture = new CultureInfo(GetSystemDefaultLCID());

这可能用于设置文化:

 Thread.CurrentThread.CurrentCulture = new CultureInfo(GetSystemDefaultLCID());
 Thread.CurrentThread.CurrentUICulture = new CultureInfo(GetSystemDefaultLCID());

【讨论】:

  • 从未尝试过,但您可能还想在 Edit/ProjectSettings/PlayerSettins/OtherSettings/ScriptingBackend 下切换到 ILCPP 而不是 Mono
猜你喜欢
  • 2010-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-12
  • 2010-11-08
  • 2012-01-15
  • 1970-01-01
相关资源
最近更新 更多