位置: 编程技术 - 正文

Unity3d与iOS的交互(unity and unity)

编辑:rootadmin

推荐整理分享Unity3d与iOS的交互(unity and unity),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:unity和u3d,unity io,unity3d vs,unity and unity,unity and unity,unity与ios交互,unity iphone,unity与ios交互,内容如对您有帮助,希望把文章链接给更多的朋友!

Unity3d与iOS的交互(1)

今天我们介绍Unity3d与iOS交互第一部分:iOS传消息到Unity3d中。下面我们开始吧:

1.

首先用Unity3d创建一个Plain,并调整好摄像机的角度以及光源的位置,如下所示:

2.

然后我们创建一个Cube,我们会在iOS中用Objective-C代码来控制它旋转:

3.

然后我们创建一个Rotate.js的脚本并把它关联到Cube上:

[javascript] view plaincopyvar vrotate : Vector3; //Rotate Left function RotateLeft() { print("Rotate Left"); transform.Rotate(Vector3.up * Time.deltaTime * , Space.World); } //Rotate Right function RotateRight() { print("Rotate Right"); transform.Rotate(Vector3.down * Time.deltaTime * , Space.World); } //Rotate Up function RotateUp() { print("Rotate Up"); transform.Rotate(Vector3.right * Time.deltaTime * , Space.World); } //Rotate Down function RotateDown() { print("Rotate Down"); transform.Rotate(Vector3.left * Time.deltaTime * , Space.World); } 上面的四个函数分别朝不同角度选择Cube。我们会在iOS程序中调用这几个Unity3d中的函数。

4.

然后在Unity3d中Build为XCode项目,打开该项目。首先创建一个基于NSObject的类,名为MyViewInit,我们会将我们自己的界面初始化代码都放在其中。修改MyViewInit.h如下:

[cpp] view plaincopy// // MyViewInit.h // Unity-iPhone // // Created by tao hu on 9//. // Copyright (c) __MyCompanyName__. All rights reserved. // #import <UIKit/UIKit.h> @interface MyViewInit : NSObject &#;(void)CreateButton:title rect:(CGRect)rect action:(SEL)action controller:(UIViewController *)controller; &#;(void)CreateTitle:(UIViewController *)controller; &#;(void)LeftButtonPressed; &#;(void)RightButtonPressed; &#;(void)UpButtonPressed; &#;(void)DownButtonPressed; &#;(void)Init:(UIViewController *)controller; @end 其中的方法都是类方法。在Init函数中我们完成了所有我们自定义界面的绘制(添加了四个UIButton和一个UILabel)。

5.

修改MyViewInit.m如下:

[cpp] view plaincopy// // MyViewInit.m // Unity-iPhone // // Created by tao hu on 9//. // Copyright (c) __MyCompanyName__. All rights reserved. // #import "MyViewInit.h" @interface MyViewInit () @end @implementation MyViewInit //创建按钮 &#;(void)CreateButton:title rect:(CGRect)rect action:(SEL)action controller:(UIViewController *)controller { UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //设置按钮范围 btn.frame = rect; //设置按钮显示内容 [btn setTitle:title forState:UIControlStateNormal]; //设置按钮改变后绑定的响应方法 [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside]; //加入View中 [controller.view addSubview:btn]; } //创建标签 &#;(void)CreateTitle:(UIViewController *)controller { //创建label视图 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, , )]; //设置显示内容 label.text = @"旋转控制"; //设置背景颜色 label.backgroundColor = [UIColor blueColor]; //设置文字颜色 label.textColor = [UIColor whiteColor]; //设置显示位置居中 label.textAlignment = UITextAlignmentCenter; //设置字体大小 label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:] size:]; [controller.view addSubview:label]; } //向左按钮 &#;(void)LeftButtonPressed { UnitySendMessage("Cube", "MoveLeft",""); } //向右按钮 &#;(void)RightButtonPressed { UnitySendMessage("Cube", "MoveRight",""); } //向上按钮 &#;(void)UpButtonPressed { UnitySendMessage("Cube", "MoveUp",""); } //向下按钮 &#;(void)DownButtonPressed { UnitySendMessage("Cube", "MoveDown",""); } &#;(void)Init:(UIViewController *)controller { [self CreateTitle:controller]; [self CreateButton:@"左旋转" rect:CGRectMake(0, , , ) action:@selector(LeftButtonPressed) controller:controller]; [self CreateButton:@"右旋转" rect:CGRectMake(0, , , ) action:@selector(RightButtonPressed) controller:controller]; [self CreateButton:@"上旋转" rect:CGRectMake(0, , , ) action:@selector(UpButtonPressed) controller:controller]; [self CreateButton:@"下旋转" rect:CGRectMake(0, , , ) action:@selector(DownButtonPressed) controller:controller]; } @end

其中:

UnitySendMessage函数有三个参数:

第一个是Scene中的模型的名称,第二个是已经绑定在模型上的某个函数,第三个是char *类型的参数,用来将参数传递给这个函数。

Unity3d与iOS的交互(unity and unity)

我们可以用这个方法调用Unity3d中的任意一个模型上的任意一个函数。

6.

最后我们要做的是在程序启动时调用上面的Init函数。找到AppController.mm文件:

[cpp] view plaincopyint OpenEAGL_UnityCallback(UIWindow** window, int* screenWidth, int* screenHeight, int* openglesVersion)

函数在Unity3d程序启动时会被调用,其中的EAGLView 就是Unity3d的背景的那个View。我们在这个函数中调用我们的Init函数。修改OpenEAGL_UnityCallback如下:

[cpp] view plaincopyint OpenEAGL_UnityCallback(UIWindow** window, int* screenWidth, int* screenHeight, int* openglesVersion) { CGRect rect = [[UIScreen mainScreen] bounds]; // Create a full-screen window _window = [[UIWindow alloc] initWithFrame:rect]; EAGLView* view = [[EAGLView alloc] initWithFrame:rect]; UnityViewController *controller = [[UnityViewController alloc] init]; sGLViewController = controller; #if defined(__IPHONE_3_0) if( _iosorNewer ) controller.wantsFullScreenLayout = TRUE; #endif controller.view = view; [_window addSubview:view]; [MyViewInit init:controller]; if( !UnityUseOSAutorotation() ) { _autorotEnableHandling = true; [[NSNotificationCenter defaultCenter] postNotificationName: UIDeviceOrientationDidChangeNotification object: [UIDevice currentDevice]]; } ......

其实我们只加入了一句话:

[cpp] view plaincopy[MyViewInit init:controller];

并在该文件首部加入:

[cpp] view plaincopy#import "MyViewInit.h"

7.

好了,终于要在真机上运行了:

初始界面:

点击下旋转按钮:

旋转Cube:

我们发现在XCode的Console窗口中输出了Unity3d中的pirnt语句。因此,Unity3d中的print函数在真机上运行时是输出到XCode的Console中。

最后代码太大了,无法上传,有需要的可以和我联系或在下面留言。

本文参考了雨松MOMO的文章,在此表示感谢:

Intermediate Unity 3D for iOS: Part 1/3 ThisisatutorialbyJoshuaNewnham,thefounderofWeMakePlay,anindependentstudiocraftingcreativedigitalplayforemergingplatforms.Unityisarguablythemostpopular3DgameengineforiOS–andformanygoodreasons!Rapidde

NGUI血条制作,当人物移出屏幕后不显示血条,优化代码 usingUnityEngine;usingSystem.Collections;///summary///脚本功能:NGUI血条实现///知识要点:NGUI,3D坐标到2D坐标的转换///创建时间:年6月日///添加对象:添加到

Coroutine couldn&#;t be started because the the game object &#;GameController&#; is inactive! 版权声明:本文为博主原创文章,未经博主允许不得转载。

标签: unity and unity

本文链接地址:https://www.jiuchutong.com/biancheng/369224.html 转载请保留说明!

上一篇:[教程] 使用3D Infinite Runner Toolkit打造僵尸跑酷游戏(3dmconfig.ini有什么用)

下一篇:Intermediate Unity 3D for iOS: Part 1/3

  • 车船税征税范围口诀
  • 小规模纳税人已过开票截止日期禁止开票怎么办
  • 资本公积转增资本的限制条件
  • 当年亏损额为什么不能填?
  • 大凭证小凭证
  • 税控减免税额如何做分录
  • 公司汽车的折旧费可以扺税吗
  • 社保局的员工是公务员吗
  • 劳务报酬增值税和个人所得税都要交吗
  • 收到供应商赠送的发票
  • 未经过他人同意贷款怎么处理
  • 营改增以前土地交易应交税费
  • 税费和应交税费一样吗
  • 股权出资登记管理办法已废止
  • 结转税金需要附件吗
  • 政府给的慰问金叫什么
  • 资产减值损失和信用损失的区别
  • 经济纠纷的解决途径包括哪些
  • 增值税进项大于销项,要全部认证吗
  • 怎么用手撕胶带图解
  • 手撕税票去哪可以弄到
  • 涉农贷款损失
  • 不开票收入是怎么回事
  • 资产负债表日后事项是什么意思?
  • 房地产公司需要和哪些部门打交道
  • 广告公司营改增后的会计分录
  • 外籍人员探亲签证
  • 设计服务交文化建设税吗
  • 收上级补助款专用如何做账?
  • 飞机票开电子发票是电子行程单吗
  • 小规模公司减免的增值税怎么做账
  • macbook panic cpu caller
  • vue2.0解决跨域问题
  • Win10 1809 17763.2268 更新发布:附更新修复内容汇总
  • 雨林木风u盘pe装系统教程
  • 自费出版的书籍可以售卖吗
  • joomla组件
  • 补计提去年所得税费用会计分录
  • 划拨建设用地使用权没有使用期限的限制
  • 应付账款零头怎么处理
  • angular 初学者快速上手教程
  • 模式识别与图像处理能做什么
  • php读写xml
  • wordpress all in one
  • 以固定资产抵账什么意思
  • 如何修改php网页内容
  • 端午节补几天工资
  • 函数模拟图
  • mongodb导出数据库数据文件
  • 已经抵扣的发票红字信息表怎么开
  • 比较详细的耐克知识大全
  • 会员退费怎么算
  • 建筑行业普票和专票的税率一样吗
  • 车船税交不交印花税
  • 对公账户原路退回备注怎么写
  • 代收运输费的会计分录
  • 增资扩股影响原股东的利益吗
  • 事业单位非税收入帐务处理
  • 去年多摊销了怎么办
  • 计提工资的会计账务处理
  • 列举2-4种特殊销售方式,其销售额如何确认?
  • 当月支付当月租金需要计提吗
  • 人工费怎么核算
  • 建筑公司直接把钱打到个人账户怎么走账
  • 项目期间费用包括哪些费用
  • 委托收款商业汇票
  • wibdows任务管理器
  • imac 2010 cpu
  • win10系统语言包安装方法
  • 用python的turtle画图代码
  • 纸嫁衣6攻略全文图解
  • javascript的数组可以存放任何类型
  • jquery页面跳转的方法
  • shell忽略大小写
  • 怎样用在js中使用css的内容
  • javascript获取当前文件夹文件数量
  • javascript怎么学好
  • 弥补亏损怎么算
  • 进项税额包括哪些项目
  • 国家税务总局每家公司都可以注册吗
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设