Unity调用IOS相关接口获取手机型号(CSharp)

实现简单的效果:点击Button,调用IOS AlertView,并显示硬件型号

具体实现:在脚本中定义2个外部方法,一个为弹出AlertView的,另一个则为返回字符串的

GUI中创建一个Button,并在点击时弹出调用外部函数,达到弹框效果


在C-Sharp定义了一个外部方法

DllImport("__Internal") 和extern是关键点

以下是C-Sharp脚本代码

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Runtime.InteropServices;  
  4.   
  5. public class Test : MonoBehaviour {  
  6.       
  7.     private static string _buttonTitle = "press!!!!";  
  8.   
  9.     [DllImport ("__Internal")]  
  10.     private static extern string _getDeviceName();  
  11.   
  12.     [DllImport ("__Internal")]  
  13.     private static extern void _showAlertView(string str);  
  14.     // Use this for initialization  
  15.     void Start () {  
  16.       
  17.         if(Application.platform==RuntimePlatform.IPhonePlayer)  
  18.         {  
  19.   
  20.             print("Unity:"+_getDeviceName());  
  21.   
  22.   
  23.         }  
  24.     }  
  25.   
  26.   
  27.     void OnGUI ()  
  28.     {  
  29.         if (GUI.Button(new Rect (15, 10, 450, 100),_buttonTitle))  
  30.         {  
  31.             _showAlertView(_getDeviceName());  
  32.         }  
  33.   
  34.         GUIStyle labelFont = new GUIStyle();      
  35.         labelFont.normal.textColor = Color.white;  
  36.         labelFont.alignment = TextAnchor.MiddleCenter;    
  37.         labelFont.fontSize = 30;   
  38.         GUI.Label(new Rect(15, 150, 100, 100), _getDeviceName(), labelFont);  
  39.           
  40.           
  41.     }  
  42.       
  43.       
  44.     // Update is called once per frame  
  45.     void Update () {  
  46.       
  47.   
  48.     }  
  49. }  
将上述脚本绑定至Main Camera,接着Build&Run一下(PS:记得是IOS的)


这时Unity帮我们打开了XCODE,我们需要做的是在里面添加一个新类,我在这用.mm

并在.mm中通过extern "C"标记接口


[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface CustomMethods : NSObject  
  4.   
  5. @end  


[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  CustomMethods.mm  
  3. //  Unity-iPhone  
  4. //  
  5. //  Created by Dale_Hui on 13-12-13.  
  6. //  
  7. //  
  8.   
  9. #import "CustomMethods.h"  
  10. #import <sys/utsname.h>  
  11.   
  12. static struct utsname systemInfo;  
  13.   
  14. extern "C"  
  15. {  
  16.     void _showAlertView(const char* str);  
  17.     char* _getDeviceName();  
  18. }  
  19. void _showAlertView(const char* str)  
  20. {  
  21.       
  22.     UIAlertView * alertView=[[UIAlertView alloc]initWithTitle:@"Unity交互" message:[NSString stringWithUTF8String:str] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil];  
  23.     [alertView show];  
  24.     [alertView release];  
  25.       
  26.       
  27. }  
  28.   
  29. char* _getDeviceName()  
  30.   
  31. {  
  32.       
  33.     uname(&systemInfo);  
  34.       
  35.     char* deviceName=(char*)malloc(sizeof(char)*255);  
  36.       
  37.     strcpy(deviceName, systemInfo.machine);  
  38.       
  39.     return deviceName;  
  40.       
  41. }  
  42.   
  43.   
  44. @implementation CustomMethods  
  45.   
  46. @end  

IOS里有个给Unity发送回调方法(异步方法)

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. UnitySendMessage("GameObjectName1""MethodName1""Message to send");  
三个参数分别为: 对象名,函数名,传递的信息

Unity 调用IOS IAP(内购):   http://blog.chukong-inc.com/index.php/2012/01/05/unity3d-之iap/

有关extern "C"的解释:   http://blog.chinaunix.net/uid-21411227-id-1826909.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值