[英]How can i get locale info in Unity C#?
我正在使用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。
問題來自Unity使用的Mono。
我在這個論壇帖子中找到了一些解決方案。
如果您正在使用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());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.