学习了宣雨松的两篇Unity和IOS交互的文章,自己动手做了下,遇到了些问题,在此记录

先说IOS发送消息给Unity:(文章地址:http://www.xuanyusong.com/archives/517)

先在unity写好脚本,并拖到cube上,代码是给IOS来调用的,代码如下:

var vrotate : Vector3;

//向左旋转
function MoveLeft()
{
var rotate : float = Time.deltaTime * 100;
vrotate = Vector3.up * rotate;
transform.Rotate(vrotate, Space.World);
} //向右旋转
function MoveRight()
{
var rotate : float = Time.deltaTime * 100;
vrotate = Vector3.down* rotate;
transform.Rotate(vrotate, Space.World);
} //向上旋转
function MoveUp(){
var rotate : float = Time.deltaTime * 100;
vrotate = Vector3.right* rotate;
transform.Rotate(vrotate, Space.World);
} //向下旋转
function MoveDown(){
var rotate : float = Time.deltaTime * 100;
vrotate = Vector3.left* rotate;
transform.Rotate(vrotate, Space.World);
}

接下来将这个Unity工程导出成Xcode项目,双击xcodeproj后缀的工程文件,默认是xcode打开此文件,

右键Classws文件夹 → New File... → Objective-C File 取名MyView,那么会添加MyView.m的文件,此时把后缀m改成mm

同样在新建个Header File,名也为MyView,那么会添加MyView.h的文件

MyView.h的代码如下:

#ifndef Unity_iPhone_MyView_h
#define Uni #import <UIKit/UIKit.h> @interface MyView : UIViewController @end #endif

MyView.mm的代码如下:

#import "MyView.h"
@implementation MyView // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
-(void)viewDidLoad{
[super viewDidLoad];
//创建label视图
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
//设置显示内容
label.text = @"IOS调用Unity";
//设置背景颜色
label.backgroundColor = [UIColor blueColor];
//设置文字颜色
label.textColor = [UIColor whiteColor];
//设置显示位置居中
label.textAlignment = UITextAlignmentCenter;
//设置字体大小
label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:] size:]; //创建按钮
UIButton *button0 = [UIButton buttonWithType:];
//设置按钮范围
button0.frame = CGRectMake(, , , );
//设置按钮显示内容
[button0 setTitle:@"矩形左旋转" forState:UIControlStateNormal];
//设置按钮改变后 绑定响应方法
[button0 addTarget:self action:@selector(LeftButtonPressed) forControlEvents:UIControlEventTouchUpInside]; //创建按钮
UIButton *button1 = [UIButton buttonWithType:];
//设置按钮范围
button1.frame = CGRectMake(, , , );
//设置按钮显示内容
[button1 setTitle:@"矩形右旋转" forState:UIControlStateNormal];
//设置按钮改变后 绑定响应方法
[button1 addTarget:self action:@selector(RightButtonPressed) forControlEvents:UIControlEventTouchUpInside]; //创建按钮
UIButton *button2 = [UIButton buttonWithType:];
//设置按钮范围
button2.frame = CGRectMake(, , , );
//设置按钮显示内容
[button2 setTitle:@"矩形上旋转" forState:UIControlStateNormal];
//设置按钮改变后 绑定响应方法
[button2 addTarget:self action:@selector(UpButtonPressed) forControlEvents:UIControlEventTouchUpInside]; //创建按钮
UIButton *button3 = [UIButton buttonWithType:];
//设置按钮范围
button3.frame = CGRectMake(, , , );
//设置按钮显示内容
[button3 setTitle:@"矩形下旋转" forState:UIControlStateNormal];
//设置按钮改变后 绑定响应方法
[button3 addTarget:self action:@selector(DownButtonPressed) forControlEvents:UIControlEventTouchUpInside]; //向view添加
[self.view addSubview:label];
[self.view addSubview:button0];
[self.view addSubview:button1];
[self.view addSubview:button2];
[self.view addSubview:button3];
} //向左按钮
-(void)LeftButtonPressed{
UnitySendMessage("Cube","MoveLeft","");
} //向右按钮
-(void)RightButtonPressed{
UnitySendMessage("Cube","MoveRight","");
}
//向上按钮
-(void)UpButtonPressed{
UnitySendMessage("Cube","MoveUp","");
} //向下按钮
-(void)DownButtonPressed{
UnitySendMessage("Cube","MoveDown","");
} - (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning]; // Release any cached data, images, etc. that aren't in use.
} - (void)viewDidUnload {
[super viewDidUnload];
} - (void)dealloc {
[super dealloc];
} @end

找到UnityAppController.mm此文件:
导入#include “MyView.h”

在UnityAppController.mm文件下的这个方法(didFinishLaunchingWithOptions)里面
加入如下代码:

MyView * myView = [[MyView alloc] init];
[UnityGetGLView() addSubview:myView.view];
return YES;

大功告成!

Unity发送消息给IOS:(文章地址:http://www.xuanyusong.com/archives/521)

新建一个Unity脚本,名为SDK

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.cs

using UnityEngine;
using System.Collections; public class Main : MonoBehaviour { //声明两个Texture变量,图片资源在外面连线赋值
public Texture Button0;
public Texture Button1; // Use this for initialization
void Start () { } // Update is called once per frame
void Update () { } //这个方法用于绘制
void OnGUI() {
//绘制两个按钮
if(GUI.Button(new Rect(,,,),Button0))
{
//返回值为ture说明这个按钮被点击
SDK.ActivateButton0();
} //绘制两个按钮
if(GUI.Button(new Rect(,,,),Button1))
{
//返回值为ture说明这个按钮被点击
SDK.ActivateButton1();
}
}
}

将Untiy3D项目导出成Xcode项目,我们用Xcode打开它。添加Unit3D中GUI按钮点击后的响应事件。创建一个类命名为MyView.h 、MyView.mm,用它来接收Unity3D 回馈回来的消息,_PressButton0 与 _PressButton1 这两个方法在Unity3D中已经注册过,所以在这个类中我们须要对它进行Xcode中的实现。

MyView.h代码如下:

#ifndef Unity_iPhone_MyView_h
#define Unity_iPhone_MyView_h @interface MyView : UIViewController void _PressButton0(); void _PressButton1(); @end
#endif

MyView.mm代码如下:

关键点:unity调用Xcode封装的函数,声明时需要在自己写的MyView类的头部引用extern "C"

extern "C"
#import "MyView.h" @implementation MyView //接收Unity3D 传递过来的信息 void _PressButton0()
{
UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTitle:@"测试Unity向IOS发送消息"];
[alert setMessage:@"点击了第一个按钮"];
[alert addButtonWithTitle:@"确定"];
[alert show];
[alert release];
} void _PressButton1()
{ UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTitle:@"测试Unity向IOS发送消息"];
[alert setMessage:@"点击了第二个按钮"];
[alert addButtonWithTitle:@"确定"];
[alert show];
[alert release];
} @end

IOS与Unity交互的更多相关文章

  1. Android与Unity交互研究

    转载请注明出处:http://blog.csdn.net/crazy1235/article/details/46733221 Android与Unity交互研究 unity与android交互的由来 ...

  2. Cordova - 与iOS原生代码交互2(使用Swift开发Cordova的自定义插件)

    在前一篇文章中我介绍了如何通过 js 与原生代码进行交互(Cordova - 与iOS原生代码交互1(通过JS调用Swift方法)),当时是直接对Cordova生成的iOS工程项目进行编辑操作的(添加 ...

  3. iOS与HTML5交互方法总结(转)

    今天小编在找技术文章的时候,发现这样一个标题:iOS与HTML5交互方法总结,怎么看着这么熟悉呢?   还以为是刚哥用了别的文章,点进去一看,原来是刚哥自己写的文章,他们转载的,而且还上了Dev St ...

  4. iOS与H5交互

    H5与App原生交互,一般会是前端页面中的JavaScript与App使用的原生开发语言的交互.技术方案应能达到以下要求: 在js与原生进行交互的时候能保证正常的正向调用逻辑返回,反向可以处理异步回调 ...

  5. iOS与H5交互遇到的坑

    之前的博客写过使用<JavaScriptCore/JavaScriptCore.h>库来实现与H5的交互,但是在项目中还是遇到了一些不得不踩的坑.在这里将我遇到的问题以及参考网上几位大神的 ...

  6. WebViewJavascriptBridge详细使用 iOS与H5交互的方案

    WebViewJavascriptBridge详细使用 源网址: https://www.cnblogs.com/jiang-xiao-yan/p/5345755.html    前言 WebView ...

  7. iOS与HTML5交互方法总结(修正)

    摘要 看了不少别人写的博客或者论坛,关于iOS与HTML5交互方法大概主要有5种方式: 1. 利用WKWebView进行交互(系统API) 2. 利用UIWebView进行交互(系统API) 3. 苹 ...

  8. cordova与ios native code交互的原理

    非常早曾经写了一篇博客,总结cordova插件怎么调用到原生代码:cordova调用过程,只是写得太水.基本没有提到原理.近期加深了一点理解,又一次补充说明一下 js调用native 以下是我们产品中 ...

  9. unity 和 iOS/Android 信息交互(方法调用)

    参考文章均来源于[大神雨松momo]的文章. unity -> iOS // unity 程序 usingSystem.Runtime.InteropServices; usingUnityEn ...

随机推荐

  1. POJ-2155 Matrix---二维树状数组+区域更新单点查询

    题目链接: https://vjudge.net/problem/POJ-2155 题目大意: 给一个n*n的01矩阵,然后有两种操作(m次)C x1 y1 x2 y2是把这个小矩形内所有数字异或一遍 ...

  2. 2017.11.4 JavaWeb-----基于JavaBean+JSP求任意两数代数和(改进的在JSP页面中无JSP脚本代码的)+网页计数器JavaBean的设计与使用

    修改后的JSP中不含有JSP脚本代码这使得JSP程序的清晰性.简单 1.设计JavaBean 的Add.java 类 package beans; public class Add { private ...

  3. ZooKeeper 完全分布式集群环境搭建

    1. 搭建前准备 示例共三台主机,主机IP映射信息如下: 192.168.32.101 s1 192.168.32.102 s2 192.168.32.103 s3 2.下载ZooKeeper, 以  ...

  4. redux详解

    redux介绍 学习文档:英文文档,中文文档,Github redux是什么 redux是一个独立专门用于做状态管理的JS库(不是react插件库),它可以用在react, angular, vue等 ...

  5. P1903 数颜色

    题目 带修莫队题. 在询问上多加一个变量,记录是在那次修改之后的. 然后暴力修改. 就没了. 不过有一些修改的小技巧 #include<cstdio> #include<algori ...

  6. MySQL常见错误分析与解决方法总结

    MySQL常见错误分析与解决方法总结 一.Can't connect to MySQL server on 'localhost' (10061)翻译:不能连接到 localhost 上的mysql分 ...

  7. 在Linux文件清空的几种方法

    在Linux文件清空的几种方法 1.使用重定向的方法 [root@centos7 ~]# du -h test.txt 4.0K test.txt [root@centos7 ~]# > tes ...

  8. docker快速安装jenkins

    用过docker的人,可能真的很难忍受再一步步二进制安装了,好了话不多说,感慨一下jenkins实现自动化发布构建真的很方便. 推荐一个学习的好地方https://m.w3cschool.cn/jen ...

  9. cordova创建工程添加插件

    创建工程 phonegap创建工程 代码 用以创建自己需要的  工程名   ; 报名  ;类名 ; 应用名 cordova create hello com.example.hello HelloWo ...

  10. Spring Cloud 入门Eureka -Consumer服务消费(Ribbon)(二)

    前面一篇介绍了LoadBalancerClient来实现负载均衡, 这里介绍Spring cloud ribbon 1.ribbon Spring Cloud Ribbon 是一个基于Http和TCP ...