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

原文地址: http://blog.csdn.net/luxiaoyu_sdc/article/details/23922649
 
实现简单的效果:点击Button,调用IOS AlertView,并显示硬件型号
具体实现:在脚本中定义2个外部方法,一个为弹出AlertView的,另一个则为返回字符串的
GUI中创建一个Button,并在点击时弹出调用外部函数,达到弹框效果
 
在C-Sharp定义了一个外部方法
DllImport(“__Internal”) 和extern是关键点
 
以下是C-Sharp脚本代码
 

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Runtime.InteropServices;
  4. public class Test : MonoBehaviour {
  5.     private static string _buttonTitle = “press!!!!”;
  6.     [DllImport (“__Internal”)]
  7.     private static extern string _getDeviceName();
  8.     [DllImport (“__Internal”)]
  9.     private static extern void _showAlertView(string str);
  10.     // Use this for initialization
  11.     void Start () {
  12.         if(Application.platform==RuntimePlatform.IPhonePlayer)
  13.         {
  14.             print(“Unity:”+_getDeviceName());
  15.         }
  16.     }
  17.     void OnGUI ()
  18.     {
  19.         if (GUI.Button(new Rect (15, 10, 450, 100),_buttonTitle))
  20.         {
  21.             _showAlertView(_getDeviceName());
  22.         }
  23.         GUIStyle labelFont = new GUIStyle();
  24.         labelFont.normal.textColor = Color.white;
  25.         labelFont.alignment = TextAnchor.MiddleCenter;
  26.         labelFont.fontSize = 30;
  27.         GUI.Label(new Rect(15, 150, 100, 100), _getDeviceName(), labelFont);
  28.     }
  29.     // Update is called once per frame
  30.     void Update () {
  31.     }
  32. }

将上述脚本绑定至Main Camera,接着Build&Run一下(PS:记得是IOS的) 
 
这时Unity帮我们打开了XCODE,我们需要做的是在里面添加一个新类,我在这用.mm
并在.mm中通过extern “C”标记接口
 

  1. #import <Foundation/Foundation.h>
  2. @interface CustomMethods : NSObject
  3. @end

 
 
 

  1. //
  2. //  CustomMethods.mm
  3. //  Unity-iPhone
  4. //
  5. //  Created by Dale_Hui on 13-12-13.
  6. //
  7. //
  8. #import “CustomMethods.h”
  9. #import <sys/utsname.h>
  10. static struct utsname systemInfo;
  11. extern “C”
  12. {
  13.     void _showAlertView(const char* str);
  14.     char* _getDeviceName();
  15. }
  16. void _showAlertView(const char* str)
  17. {
  18.     UIAlertView * alertView=[[UIAlertView alloc]initWithTitle:@”Unity交互” message:[NSString stringWithUTF8String:str] delegate:nil cancelButtonTitle:@”确定” otherButtonTitles:nil, nil nil];
  19.     [alertView show];
  20.     [alertView release];
  21. }
  22. char* _getDeviceName()
  23. {
  24.     uname(&systemInfo);
  25.     char* deviceName=(char*)malloc(sizeof(char)*255);
  26.     strcpy(deviceName, systemInfo.machine);
  27.     return deviceName;
  28. }
  29. @implementation CustomMethods
  30. @end

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

  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

reeoo.com - web design inspiration

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注