位置: 编程技术 - 正文

Unity3d与iOS的交互(unity and unity)

发布时间:2024-02-27

推荐整理分享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

  • 完税证明是可以抵扣吗
  • 小企业一定要买五险吗
  • 利润表的调整
  • 股东溢价转让股份交易市场会计分录
  • 当期免抵税额有什么用
  • 电商返佣平台有风险吗
  • 事业单位开展活动的意义
  • 计提的利息汇算清缴前没发放,需要交企业所得税吗
  • 行政单位在建工程入账
  • 收到海外商务退税如何做账?
  • 制造行业运输费包括哪些
  • 营改增后非增值税应税项目包括哪些
  • 企业房产税如何申报缴纳
  • 租车服务的税率
  • 预缴两个点的税是什么意思
  • 酒店工作车工作间标准
  • 进料加工贸易是什么意思
  • 不具有法人资格的企业形式
  • 记账凭证填制的内容
  • 代缴社保工资怎么做账
  • 核销以前年度的其他应付款
  • 工业企业出售厂房需要预缴税款吗
  • 加装固态后如何分盘
  • win10 21h1正式版怎么样
  • 支付宝付款凭证可以当发票吗
  • nod32kui.exe - nod32kui是什么进程 作用是什么
  • 个人独资企业公账转私账
  • 应收账款和预收账款有什么区别
  • 为什么建筑企业简易计税可以扣分包款
  • php imagecopymerge
  • 未注销的坏账可以转出吗
  • 建筑设备出租并配备人员
  • 超期未认证的发票怎么处理
  • 债权投资 科目
  • 为什么我的命令提示符里显示user
  • 万科金域华府二手房房源
  • Navicat for MySQL 15 v15.0.27 中文企业正式版(附安装教程) 32/64位 破解版
  • mysql如何上锁
  • 税收收入退还书有时间限制吗
  • 一般纳税人企业所得税5%还是25%
  • 累计折旧是当月提还是下月提
  • python操作yaml文件
  • 食堂购买固定资产会计处理
  • 一般纳税人招待费扣除标准
  • 个人劳务费用
  • 合并报表调整分录理解
  • 企业所得税退回应交税费会计分录
  • sqlserversa用户登录失败
  • 免税申报表里的免税销售额是不含税
  • 买下土地
  • 企业如何在没有税务登记
  • 技术服务费属于什么科目
  • 检测费可以抵扣吗
  • 公司成立前购买的固定资产
  • 销项负数发票给对方哪一联
  • 国家法定滞纳金规定
  • 水泥销售技巧
  • 技术服务费发票怎么开
  • 企业向员工收取费用
  • 新成立公司年初余额
  • Win10怎么显示我的电脑
  • Win10 Mobile Build 10549正式推送:须回滚到WP8.1升级
  • itunes无法更新app
  • linux使用cp
  • cygwin下载教程
  • win7磁盘c满了怎么办却又分不清哪些是该删除
  • 移动硬盘做win7系统盘
  • win10如何更换
  • js日期选择框
  • Extjs grid panel自带滚动条失效的解决方法
  • 按住鼠标右键拖动文件
  • <Unity3D>Unity3D GUI控件
  • bootstrap快速入门
  • android自定义属性详解
  • 自定义右键属性是什么
  • android颜色值
  • NGUI之UICamera
  • 深入理解新发展理念,推进供给侧结构性改革心得体会
  • 关于怀孕在线咨询
  • 国家税务总局财政部公告2023年第11号
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号