【Unity3D游戏开发】Application.systemLanguage无法区分IOS简体中文和繁体中文

本文转载于:http://blog.csdn.net/teng_ontheway/article/details/50277169

游戏发布,语言本地化需要繁体中文和简体中文

iOS8版本之前没问题,iOS9上无法正常识别这两种语言

原因是在iOS9上,Unity通过Application.systemLanguage返回的简体中文和繁体中文都是SystemLanguage.Chinese,真尼玛坑爹啊,摔手机砸电脑~

亲测Unity 5.2.1、Unity 5.3都有这个问题,Unity更新日志中都没有见这方面消息,不知是没人重视还是没人提~


原因分析:

[plain]  view plain  copy
  1. 语言          iOS返回语言         Application.systemLanguage  
  2.   
  3. ios 7  
  4. 简体中文            zh-Hans             ChineseSimplified  
  5. 繁体中文            zh-Hans             ChineseSimplified  
  6.   
  7. ios 8.1  
  8. 简体中文            zh-Hans             ChineseSimplified  
  9. 繁体中文(香港)        zh-HK               ChineseTraditional  
  10. 繁体中文(台湾)        zh-Hant             ChineseTraditional  
  11.   
  12. ios 9.1  
  13. 繁体中文            zh-Hant-CN          Chinese           
  14. 简体中文            zh-Hans-CN          Chinese  
  15. 繁体中文(香港)        zh-HK               ChineseTraditional  
  16. 繁体中文(台湾)        zh-TW               Chinese  


可见苹果在iOS9上都做了什么鬼处理......


解决方法:

如果Application.systemLanguage返回的是chinese,直接通过C#调用ObjC函数获得当前iOS系统语言进行纠正

[csharp]  view plain  copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Runtime.InteropServices;  
  4.   
  5. public class ComManager  
  6. {  
  7.     [DllImport("__Internal")]  
  8.     // ios手机的当前语言 "en"、“zh"、“zh-Hans"、"zh-Hant"  
  9.     private static extern string CurIOSLang();  
  10.   
  11.     /** 
  12.         获得校正后的系统语言 
  13.         因为ios9调整了系统语言,简体中文和繁体中文在ios9上Application.systemLanguage获取的值都是Chinese 
  14.         无法区分简体中文和繁体中文 
  15.  
  16.         ios 7 
  17.         简体              zh-Hans 
  18.         繁体              zh-Hant 
  19.  
  20.         ios 8.1 
  21.         简体中文            zh-Hans             ChineseSimplified 
  22.         繁体中文(香港)        zh-HK               ChineseTraditional 
  23.         繁体中文(台湾)        zh-Hant             ChineseTraditional 
  24.  
  25.         ios 9.1 
  26.         简体中文            zh-Hans-CN          Chinese 
  27.         繁体中文(香港)        zh-HK               ChineseTraditional 
  28.         繁体中文(台湾)        zh-TW               Chinese 
  29.     **/  
  30.     public static SystemLanguage GetSystemLanguage()  
  31.     {  
  32.         SystemLanguage lang = Application.systemLanguage;  
  33.         if (Application.platform == RuntimePlatform.IPhonePlayer)  
  34.         {  
  35.             if (lang == SystemLanguage.Chinese) {  
  36.                 string name = CurIOSLang();  
  37.                 if (name.StartsWith("zh-Hans")) {  
  38.                     return SystemLanguage.ChineseSimplified;  
  39.                 }  
  40.   
  41.                 return SystemLanguage.ChineseTraditional;  
  42.             }  
  43.         }  
  44.   
  45.         return lang;  
  46.     }  
  47. }  


有朋友问上面生命的的CurIOSLang函数怎么来的,是在Objective-C中定义好的,C#中声明引用,具体可以看文章最后的Unity的C#和ObjC数据交互

定义一个.mm文件,内容如下:

[objc]  view plain  copy
  1. extern char* cStringCopy(const char* string);  
  2.   
  3. extern "C"  
  4. {  
  5.     // ios手机的当前语言 "en"、“zh"、“zh-Hans"、"zh-Hant"  
  6.    const char* CurIOSLang()  
  7.    {  
  8.         NSArray *languages = [NSLocale preferredLanguages];    
  9.         NSString *currentLanguage = [languages objectAtIndex:0];   
  10.         return cStringCopy([currentLanguage UTF8String]);  
  11.    }  
  12. }  


相关资料:

Unity的C#和ObjC数据交互  http://blog.csdn.net/teng_ontheway/article/details/50182217

iOS获得当前系统语言 http://blog.csdn.net/teng_ontheway/article/details/50276987

【Unity语言国际化】多语言缩写对应表 http://blog.csdn.net/teng_ontheway/article/details/48293617


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
var target1 : Transform; var target1C : Transform; var target2 : Transform; var target2C : Transform; var mousePos1 : Vector3; var mousePos2 : Vector3; var cursorImage : Texture; var Mouse : GUISkin; private var MouseImg : boolean = false; function Update() { if(Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) { if(Input.touchCount == 1) { mousePos1 = Input.touches[0].position; } else if(Input.touchCount == 2) { mousePos1 = Input.touches[0].position; mousePos2 = Input.touches[1].position; } } else { mousePos1 = Input.mousePosition; } target1.position = camera.ScreenToWorldPoint (Vector3(mousePos1.x,mousePos1.y,1)); target2.position = camera.ScreenToWorldPoint (Vector3(mousePos2.x,mousePos2.y,1)); } function LateUpdate() { if(Input.GetKey(KeyCode.Escape)) { Application.Quit(); } } function OnGUI() { if(MouseImg) { GUI.skin = Mouse; var windowRect : Rect = Rect (mousePos1.x - cursorImage.width/2, Screen.height - mousePos1.y - cursorImage.height/2, cursorImage.width, cursorImage.height); windowRect = GUI.Window (0, windowRect, DoMyWindow, "My Window"); } if(GUILayout.Button("PlanA")) { Screen.showCursor = !Screen.showCursor; target1.gameObject.active = !target1.gameObject.active; target1C.gameObject.active = target1.gameObject.active; if(Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) { target2.gameObject.active = !target2.gameObject.active; target2C.gameObject.active = target2.gameObject.active; } } else if(GUILayout.Button("PlanB")) { Screen.showCursor = !Screen.showCursor; MouseImg = !MouseImg; } else if(GUILayout.Button("Restart")) { Application.LoadLevel(0); } if(GUI.Button(new Rect(Screen.width-120,Screen.height-40,120,30),"Click to YUHUA!")) {

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值