位置: 编程技术 - 正文

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

  • 缴纳印花税的会计凭证
  • 土地增值税常见问题及解答
  • 划转国有划拨土地要交契税吗?
  • 企业所得税弥补亏损明细表怎么看
  • 个人所得税的会计科目
  • 小微企业减免所得税额是怎么算出来的
  • 小规模免增值税的账务处理
  • 股东借给公司的钱怎么归还
  • 公司卖车怎么开公司发票
  • 企业购买的商业保险赔偿多少
  • 出口样品账务处理
  • 其他业务支出借方表示什么成本数额
  • 出售房产税收
  • 小微企业增值税减免账务处理
  • 季度营业额超过6万需要补税
  • 附加税费计提表
  • 残保金属于职工薪酬吗
  • 分支机构增值税汇总纳税如何申报?
  • 行政事业单位会计科目一览表
  • 什么情况下要交消费税
  • 往来款和应收账款
  • 电脑怎么设置色盲模式
  • win10电源模式最佳性能多耗好多电
  • 王者荣耀中李白第二次离开
  • 企业租赁汽车交什么税
  • win10应用图标怎么调出来
  • 中秋购物
  • 退回多收款项,提现金,如何做账
  • 收到专利补助费的会计处理
  • vue每一列内容过多自动换行
  • 公司员工个人所得税交多少
  • 付检测费会计分录
  • 收到进项发票不认证可以吗
  • 小规模自开专票办理流程
  • 终止合约取得的合约
  • 办公桌椅入固定资产计提折旧几年
  • 零税率与免税有关系吗
  • 收据可以当发票吗?
  • 本期数值与去年同期数值之差称为什么
  • 转让不动产与销售不符
  • PostgreSql新手必学入门命令小结
  • 福利费专票必须抵扣再转出吗
  • 拍卖行业收取手续费多少
  • 应收账款收不回来
  • 公司支付员工工资方式有几种
  • 业务招待费礼品要扣个税吗
  • 购入无形资产属于资产吗
  • 员工借款可以直接转账吗
  • 资产负债表期初余额和期末余额
  • 高速etc发票如何打印
  • 电子秤计入什么费用
  • windowsandbox
  • linux系统转换为win系统脚本
  • win7修改sid
  • win10开始无法打开
  • win10系统如何关闭窗口特效
  • 电脑ios怎么安装
  • 随机产生10个数
  • Ubuntu14.04 的 SSH 无密码登录的设置方法
  • linux 详解
  • win10专业版怎么安装
  • register.exe - register进程有什么用.是什么意思
  • sentstrt.exe - sentstrt进程是什么文件 有什么用
  • nilaunch.exe - nilaunch是什么进程 有什么用
  • win8.1中文版下载
  • Win10系统怎么使用经典事件查看器?
  • win7系统无法创建分区也无法定位
  • 如何修改win7电脑用户密码
  • window10光驱不能用了
  • js转义字符串
  • unity gui教程
  • 聊天界面的新消息通知设置 找不到
  • mongodb python
  • flappy bird攻略
  • [置顶] [Android Studio 权威教程]AS添加第三方库的6种方式(Jar,module,so等)
  • 注册会计师和注册建造师哪个难考
  • 收到海关进口增值税专用缴款书怎么确定库存商品的金额
  • 吉林省地税局电话号码
  • 贵州省微企补助政策
  • 城市维护建设税,教育费附加,地方教育费附加
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设