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

这篇博客介绍了如何在Unity中通过C#脚本调用iOS系统的接口,实现点击按钮后弹出AlertView显示手机硬件型号。关键点在于使用`DllImport("__Internal")`和`extern`关键字。在Unity中定义外部方法后,在Objective-C++(.mm)文件中使用`extern "C"`进行接口声明,然后在iOS端实现回调方法。
摘要由CSDN通过智能技术生成

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

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

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


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

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

以下是C-Sharp脚本代码

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class Test : MonoBehaviour {
	
	private static string _buttonTitle = "press!!!!";

	[DllImport ("__Internal")]
	private static extern string _getDeviceName();

	[DllImport ("__Internal")]
	private static extern void _showAlertView(string str);
	// Use this for initialization
	void Start () {
	
		if(Application.platform==RuntimePlatform.IPhonePlayer)
		{

			print("Unity:"+_getDeviceName());


		}
	}


	void OnGUI ()
	{
		if (GUI.Button(new Rect (15, 10, 450, 100),_buttonTitle))
		{
			_showAlertView(_getDeviceName());
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值