博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity3D与iOS消息交互方法(1)--iOS接收Unity3D发出的消息
阅读量:7076 次
发布时间:2019-06-28

本文共 3022 字,大约阅读时间需要 10 分钟。

跨平台这种事情不管多NB, 总要有些与原生系统交互的方法, 比如  Unity3D与iOS消息交互方法.

 

一: 建立一个空的Unity工程.

  File -->  New Project

二: 编写脚本文件 (Test.cs)

     在Project选项卡中, create ->C# script, 命名为Test, 双击, 编写此cs文件.

1 using UnityEngine; 2 using System.Collections; 3  4 public class Main : MonoBehaviour { 5  6 //声明两个Texture变量,图片资源在外面连线赋值 7 public Texture Button0; 8 public Texture Button1; 9 10     // Use this for initialization11     void Start () {12     13     }14     15     // Update is called once per frame16     void Update () {17     18     }19     20     //这个方法用于绘制21     void OnGUI() {22         //绘制两个按钮23         if(GUI.Button(new Rect(0,44,120,120),Button0))24         {25             //返回值为ture说明这个按钮被点击26             SDK.ActivateButton0();27         }    28         29         //绘制两个按钮30         if(GUI.Button(new Rect(200,44,120,120),Button1))31         {32             //返回值为ture说明这个按钮被点击33             SDK.ActivateButton1();34         }    35     }36 }

 

同样的方法, 再生成一个脚本文件. SDK.cs

using UnityEngine;using System.Runtime.InteropServices;public class SDK{          //导出按钮以后将在xcode项目中生成这个按钮的注册,     //这样就可以在xocde代码中实现这个按钮点击后的事件。     [DllImport("__Internal")]     private static extern void _PressButton0 ();          public static void ActivateButton0 ()     {                 if (Application.platform != RuntimePlatform.OSXEditor)         {            //点击按钮后调用xcode中的 _PressButton0 ()方法,            //方法中的内容须要我们自己来添加            _PressButton0 ();        }     }          //和上面一样     [DllImport("__Internal")]     private static extern void _PressButton1 ();          public static void ActivateButton1 ()     {         if (Application.platform != RuntimePlatform.OSXEditor)         {            _PressButton1 ();        }     }}

 

三:绑定相关脚本到Main Camera

  只拖动Test.cs文件到"Main Camera"上, 成功后, 可以在game view中看到效果图, 或者在Main Camera的Inspector中也能看到如: Test(Script)

ps: 在"Project"中添加图片文件作用button的图片, 也是用拖动的方法(选中Test.cs, 在Instpector中可以看到 Button0和1处无图片, 这时拖动图片文件到Button0和1上)

 

四: 导出iOS工程

  Unity3D中的工作此时已经完成, 可以导出iOS工程了

  File--> Build&Run

**:Unity3D中界面如下:

 

 

 

 

五: 在iOS工程中添加代码

添加两个文件 , 以接收Unity3D发来的消息

1 // 2 //  MyView.h 3 //  Unity-iPhone 4 // 5 //  Created by willme on 13-10-15. 6 // 7 // 8  9 #import 
10 11 @interface MyView : NSObject12 13 @end

 

1 // 2 //  MyView.m 3 //  Unity-iPhone 4 // 5 //  Created by willme on 13-10-15. 6 // 7 // 8  9 #import "MyView.h"10 11 @implementation MyView12 13 //接收Unity3D 传递过来的信息14 void _PressButton0()15 {16     UIAlertView *alert = [[UIAlertView alloc] init];17     [alert setTitle:@"雨松MOMO程序世界"];18     [alert setMessage:@"点击了第一个按钮"];19     [alert addButtonWithTitle:@"确定"];20     [alert  show];21     [alert release];22 }23 24 void _PressButton1()25 {26     27     UIAlertView *alert = [[UIAlertView alloc] init];28     [alert setTitle:@"雨松MOMO程序世界"];29     [alert setMessage:@"点击了第二个按钮"];30     [alert addButtonWithTitle:@"确定"];31     [alert  show];32     [alert release];33 }34 35 @end

 

 **界面如下:

 

 

***代码来自 http://blog.csdn.net/xys289187120/article/details/6933456

 

 

 

最后的效果

 

转载于:https://www.cnblogs.com/willbin/p/3370412.html

你可能感兴趣的文章
Linux基础命令:文本处理工具之join , paste
查看>>
我的友情链接
查看>>
Linux LVM 之LV
查看>>
学习GDB(二)
查看>>
自动化Oracle数据库静默安装
查看>>
在Visual Studio中用C++语言创建DLL动态链接库图文教程
查看>>
清除Windows Server Backup 备份副本
查看>>
smarty详解二:从文件读取变量、保留变量、数学计算、内建函数
查看>>
SQLSserver2008安装默认账户介绍
查看>>
面向对象之继承时的关键词
查看>>
我的友情链接
查看>>
JAVA中的类型转换 int和String
查看>>
用g++编程时遇到权限问题
查看>>
MySQL设置UTF8字符
查看>>
自定义标签例子
查看>>
四人过桥、三盏灯 三个开关 的答案
查看>>
【unity】关于时间等常用工具类
查看>>
在论坛中出现的比较难的sql问题:12(递归问题2)
查看>>
第十次课作业(风险管理、项目收尾、知识产权)
查看>>
spring-前置通知
查看>>