IOS与Unity交互
学习了宣雨松的两篇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交互的更多相关文章
- Android与Unity交互研究
转载请注明出处:http://blog.csdn.net/crazy1235/article/details/46733221 Android与Unity交互研究 unity与android交互的由来 ...
- Cordova - 与iOS原生代码交互2(使用Swift开发Cordova的自定义插件)
在前一篇文章中我介绍了如何通过 js 与原生代码进行交互(Cordova - 与iOS原生代码交互1(通过JS调用Swift方法)),当时是直接对Cordova生成的iOS工程项目进行编辑操作的(添加 ...
- iOS与HTML5交互方法总结(转)
今天小编在找技术文章的时候,发现这样一个标题:iOS与HTML5交互方法总结,怎么看着这么熟悉呢? 还以为是刚哥用了别的文章,点进去一看,原来是刚哥自己写的文章,他们转载的,而且还上了Dev St ...
- iOS与H5交互
H5与App原生交互,一般会是前端页面中的JavaScript与App使用的原生开发语言的交互.技术方案应能达到以下要求: 在js与原生进行交互的时候能保证正常的正向调用逻辑返回,反向可以处理异步回调 ...
- iOS与H5交互遇到的坑
之前的博客写过使用<JavaScriptCore/JavaScriptCore.h>库来实现与H5的交互,但是在项目中还是遇到了一些不得不踩的坑.在这里将我遇到的问题以及参考网上几位大神的 ...
- WebViewJavascriptBridge详细使用 iOS与H5交互的方案
WebViewJavascriptBridge详细使用 源网址: https://www.cnblogs.com/jiang-xiao-yan/p/5345755.html 前言 WebView ...
- iOS与HTML5交互方法总结(修正)
摘要 看了不少别人写的博客或者论坛,关于iOS与HTML5交互方法大概主要有5种方式: 1. 利用WKWebView进行交互(系统API) 2. 利用UIWebView进行交互(系统API) 3. 苹 ...
- cordova与ios native code交互的原理
非常早曾经写了一篇博客,总结cordova插件怎么调用到原生代码:cordova调用过程,只是写得太水.基本没有提到原理.近期加深了一点理解,又一次补充说明一下 js调用native 以下是我们产品中 ...
- unity 和 iOS/Android 信息交互(方法调用)
参考文章均来源于[大神雨松momo]的文章. unity -> iOS // unity 程序 usingSystem.Runtime.InteropServices; usingUnityEn ...
随机推荐
- 在CentOS 6.5上安装NodeJS
CentOS的软件源未包含有最新的nodejs, 需要手动编译安装. 首先安装依赖的库与工具 yum install libtool automake autoconf gcc-c++ openssl ...
- [ difflib] simple1.py
#!/usr/bin/env python # _*_ coding:utf-8 _*_ import difflib text1 = """text1: # 定义字符串 ...
- Spring boot 集成三种拦截方式
三种拦截方式分别为: javax.servlet.Filter org.springframework.web.servlet.HandlerInterceptor org.aspectj.lang. ...
- 散度(Divergence)和旋度(Curl)
原文链接 散度(Divergence) 散度的讨论应从向量和向量场说起.向量是数学中研究多维计算的基本概念.比如,速度可以分解为相互独立的分量,则速度就是一个多维的向量.假如空间中的每一个位置都有一个 ...
- ajax实现无刷新两级联动DropDownList
ajax实现的无刷新三级联动 http://zhangyu028.cnblogs.com/articles/310568.html 本文来自小山blog:http://singlepine.cnblo ...
- Spring Cloud 入门Eureka -Consumer服务消费(Ribbon)(二)
前面一篇介绍了LoadBalancerClient来实现负载均衡, 这里介绍Spring cloud ribbon 1.ribbon Spring Cloud Ribbon 是一个基于Http和TCP ...
- Linux中的代码编辑器vim
Vim的三种工作模式 命令行模式 插入模式 底行模式 Vim 的命令行模式 命令行模式是进入vim后的初始模式,在该模式下主要是使用方向键来移动光标的位置,并通过相应的命令来进行文字的编辑. 切换方法 ...
- 【HTML】placeholder中换行
表示回车 表示换行 案例 <textarea rows="10" placeholder="测试换行 新的一行"></textarea>
- 微信小程序相关
https://www.cnblogs.com/shenzikun1314/p/7805168.html
- tp5简单构造
application 应用目录 网站核心index前台目录 controller 控制器admin 后台目录 model 数据模型view 视图extend 静态类库目录public 静态资源和入口 ...