Unity3D开发之“获取IOS设备所在的国家代码"
2014-07-07 10:05
1086 查看
原地址:http://dong2008hong.blog.163.com/blog/static/469688272014021025578/
在前一段时间游戏开发中需要实现获取IOS设备所在的国家代码,方便服务端推送合适的语言的功能,在网上找了一些资料实现了上述功能,现在分享给大家。后面会给大家分享网络编程、数据库的应用、服务器的配置等的一些知识和注意事项。 首先,这个功能Unity是没有提供现成的API,所以必须先制作一个IOS插件,然后才再Unity里面来调用Object-c实现了的功能。 MyTool.h文件代码: #import <Foundation/Foundation.h> @interface MyTools : NSObject @end MyTool.m文件代码: #import "MyTools.h" @implementation MyTools char *_getCountryCode(){ NSLocale *locale = [NSLocalecurrentLocale]; NSString *countrycode = [localelocaleIdentifier]; NSLog(@"国家代码:%@",countrycode); const char *country = [countrycodeUTF8String]; char *back =malloc(countrycode.length + 1); char *back2 = back; for (int i = 0;i<countrycode.length; i++) { *back2 = country; back2++; } *back2 = '\0'; return back; } @end 插件制作好以后,放到Plugins/IOS文件夹下,然后在Unity中创建MyTools.cs脚本 代码如下: using UnityEngine; using System.Collections; using System.Runtime.InteropServices; public class MyTools : MonoBehaviour { [DllImport("__Internal")] private static extern string _getCountryCode(); public static string getCountryCode() { return _getCountryCode(); } } 在制作插件的时候一定要注意的问题就是,Object-c是不能直接传NSString数据类型给Unity的,你必须把NSString类型转化成char[]传给Unity。
相关文章推荐
- 用代码获取ios设备的分辨率
- 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!【2012-12-11日更新获取"产品付费数量等于0的问题"】
- 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!【2012-12-11日更新获取"产品付费数量等于0的问题"】
- 获取ios设备的开发id
- ios开发——NSString如何从标准输入设备获取字符串
- 获取ios设备设置的国家和语言
- 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!【2012-12-11日更新获取"产品付费数量等于0的问题"】
- IOS开发 获取当前视图所在的类名
- 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!【2012-12-11日更新获取"产品付费数量等于0的问题"】
- iOS获取设备剩余存储空间代码片段
- 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!【2012-12-11日更新获取"产品付费数量等于0的问题"】
- 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!【2012-12-11日更新获取"产品付费数量等于0的问题"】
- iOS开发:分享一个获取设备型号的方法
- IOS开发之--获取设备类型和系统版本号
- ios助手开发系列(三):打开设备连接,获取设备基本信息
- iOS获取设备剩余存储空间代码片段
- 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!【2012-12-11日更新获取"产品付费数量等于0的问题"】
- Javascript中获取出错代码所在文件及行数 "转载"
- php开发中如何获取所在全局变量的代码实例
- iOS开发小技巧 获取当前设备/应用程序相关信息