首页 >Unity3D频道 >【Unity3D研究院之游戏开发】 > Unity3D 研究院之IOS高级界面发送消息与Unity3D消息的接收(九)
2012
05-01

Unity3D 研究院之IOS高级界面发送消息与Unity3D消息的接收(九)

        刚搬家了新家超累,大家久等了。哇咔咔~ 今天和盆友们讨论IOS的高级界面与Unity3D游戏引擎的交互,这个在开发中是非常重要的,Unity3D 毕竟是一个面向多平台的一个游戏引擎,它不可能全部为IOS 考虑的面面俱到,引擎中也不存在针对IOS的高级界面的控件的使用。

         本例实现游戏背景是Unity3D 的游戏世界,前面添加4个IOS的高级界面的按钮,并且点击这些按钮可以将消息传递给背景的Unity3D ,让它做一些事情。
上一章介绍了触摸IOS屏幕 移动摄像机的位置,下面有盆友问我说他不想移动摄像机的位置,就想移动物体的位置,我在这里补充一下,可以把脚本绑定在箱子上,参照物选择为主摄像机,这样子更新箱子的脚本就OK啦。今天例子,我就将脚本绑定在箱子上,如下图所示,把Move脚本绑定在这个 Cube中。
Unity3D 研究院之IOS高级界面发送消息与Unity3D消息的接收(九) - 雨松MOMO程序研究院 - 1
先把Move脚本的代码贴出来,这里面我写了4个方法分别处理这个箱子的旋转,这4个方法是由IOS上的代码向Unity发送消息后调用的,下面我会介绍具体操作的方法。
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项目,不会的盆友请看我之前的文章哈,Xcode项目导出成功后,我们先添加4个高级界面的按钮用来点击响应上面脚本的这4个旋转箱子的方法。

创建一个类继承UIViewController,用来添加我们的高级界面的视图,我暂且命名为MyView.

打开Unity3D导出的AppController.mm这个类,头文件处先导入我们的这个类 #import “MyView”找到下面这个方法,来添加viewint OpenEAGL_UnityCallback(UIWindow** window, int* screenWidth, int* screenHeight,  int* openglesVersion)


EAGLView 是Unity3D 背景的那个View, 下面我们添加一个我们自己写的View 覆盖在它上面。

 

	// Create a full-screen window
	_window = [[UIWindow alloc] initWithFrame:rect];
	EAGLView* view = [[EAGLView alloc] initWithFrame:rect];
	[_window addSubview:view];

	MyView * myView =  [[MyView alloc] init];
	[_window addSubview:myView.view];

贴出MyView的代码,写完发现忘释放内存了,呵呵,懒得改了,本章主要的介绍的不是这个哦。

 

//
//  MyView.m
//  Unity-iPhone
//
//  Created by 雨松MOMO on 11-11-1.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#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(0, 0, 320, 40)];
	//设置显示内容
	label.text = @"雨松MOMO的程序世界";
	//设置背景颜色
	label.backgroundColor = [UIColor blueColor];
	//设置文字颜色
	label.textColor = [UIColor whiteColor];
	//设置显示位置居中
	label.textAlignment = UITextAlignmentCenter;
	//设置字体大小
	label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20];

	//创建按钮
	UIButton *button0 = [UIButton buttonWithType:1];
	//设置按钮范围
	button0.frame = CGRectMake(0, 40, 100, 30);
	//设置按钮显示内容
	[button0 setTitle:@"矩形左旋转" forState:UIControlStateNormal];
	//设置按钮改变后 绑定响应方法
	[button0 addTarget:self action:@selector(LeftButtonPressed) forControlEvents:UIControlEventTouchUpInside];  

	//创建按钮
	UIButton *button1 = [UIButton buttonWithType:1];
	//设置按钮范围
	button1.frame = CGRectMake(0, 100, 100, 30);
	//设置按钮显示内容
	[button1 setTitle:@"矩形右旋转" forState:UIControlStateNormal];
	//设置按钮改变后 绑定响应方法
	[button1 addTarget:self action:@selector(RightButtonPressed) forControlEvents:UIControlEventTouchUpInside];  

	//创建按钮
	UIButton *button2 = [UIButton buttonWithType:1];
	//设置按钮范围
	button2.frame = CGRectMake(0, 160, 100, 30);
	//设置按钮显示内容
	[button2 setTitle:@"矩形上旋转" forState:UIControlStateNormal];
	//设置按钮改变后 绑定响应方法
	[button2 addTarget:self action:@selector(UpButtonPressed) forControlEvents:UIControlEventTouchUpInside];  

	//创建按钮
	UIButton *button3 = [UIButton buttonWithType:1];
	//设置按钮范围
	button3.frame = CGRectMake(0, 220, 100, 30);
	//设置按钮显示内容
	[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

这里我主要说一下下面这个方法,它是Unity底层帮我们写好的一个方法,意思iPhone向向Unity发送消息,

 

参数1:场景中的模型名称,Cube就是我们定义的一个箱子。

参数2:脚本方法名称MoveLeft就是上面脚本中的方法,

参数3:为一个char *类型的 可以向Unity中传递数据。

 

UnitySendMessage(“Cube”,”MoveLeft”,””);

我们可以向Unity3D中任意模型发送消息调用它绑定的脚本中的方法,当前前提是模型名称、方法名称、 参数都填写正确。

这里4个按钮都是以这种方式传递消息,下面是iPhone 真机的效果图,我们触摸点击4个高级界面的按钮可以实现Unity3D世界中的模型旋转,  所以大家一定要切记这个方法,很重要噢,哇咔咔~

 

Unity3D 研究院之IOS高级界面发送消息与Unity3D消息的接收(九) - 雨松MOMO程序研究院 - 2

 

最后欢迎各位盆友可以和MOMO一起讨论Unity3D游戏开发,哇咔咔~~~ 附上Unity3D工程的下载地址,Xcode项目我就不上传了,不早了,大家晚安,哇咔咔~~
补充: 由于Unity3.5在渲染3D的时候添加了 sGLViewController,所以按照以前的方法添加的视图是无法接收旋转事件的。对应3.5的版本大家需要修改一下代码。
还是在 OpenEAGL_UnityCallback方法中,在此方法的末尾添加代码:
MyViewController * myView =  [[MyViewController alloc] init];

[sGLViewController.view addSubview:myView.view];

 

 

MyViewController是我们新定义的,

也就是说把我们的写的视图添加至 

sGLViewController当中,这样就完事OK啦。  Unity每次升级都会小改动一下,所以我们也需要小改动一下,哈哈!

 

2013年2月25日补充

在Unity4中 把之前的sGLViewController 替代成了 UnityGetGLViewController() 那么在Unity4中简单的修改一下上面的代码即可。

   MyViewController * myView =  [[MyViewController alloc] init];

    [UnityGetGLViewController().view addSubview:myView.view];

 

最后编辑:
作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!

Unity3D 研究院之IOS高级界面发送消息与Unity3D消息的接收(九)》有 89 条评论

  1. 刘拉拉说:

    你好、我想从ios里面向Unity传一个数组,可以做到吗、我看你说只能传一个char*、是这样的、我想在ios里得到itunes里面的所有歌曲名称的数组、然后传到Unity里面,作为一个list列表、点击之后再把点中的那个歌曲的名称传到ios里、进行播放

  2. 看教程博客点广告是美德。

  3. 郁闷了。。一直看着momo的博客在android上开发,这篇竟然。。。好吧我导入到eclipse中去重写代码试试。。能不能用

  4. 欧宇龙说:

    三件非常重要的事1:关于你2:关于我3:关于我们一:我点了你这个3次广告本次二:我电脑被偷了三:请问,我能在U3D前面放一个 摄像头的画面么??

  5. 章星宇说:

    最后 3.5 和 4 的修订,应该是 MyView 而不是 MyViewController 吧。前面新建的类名字是 MyView。十分误导。

  6. sevenloo说:

    首先 要谢谢你! MoMo我现在想做这么一个demo :“通过鼠标来实现立方体的旋转” 。也就是通过“鼠标”在屏幕上的滑动“手势”让Cube按照“鼠标”的“手势”方向旋转!我现在做的是通过判断Input.GetMouseButtonDown()和Input.GetMouseButtonUp()起始位置,但是在旋转的时候很不流畅,请MoMo指点迷津!

  7. 士心说:

    雨松能稍微说下如何移除吗?对obj-c刚入门,多谢了。

  8. sevenloo说:

    MOMO下载Eclipse是那个版本?是eclipse-jee-juno-SR2-win32-x86_64.zip还是eclipse-java-juno-SR2-win32-x86_64.zip ?我是学习.net。java还没有玩过.吼吼……

  9. 延澈左说:

    打开Unity3D导出的AppController.mm这个类是什么意思???AppController.mm这个类到底是什么样的?viewint OpenEAGL_UnityCallback(UIWindow** window, int* screenWidth, int* screenHeight, int* openglesVersion)是谁的方法??

  10. 崔彬0说:

    MOMO我想问一下。如果我从Unity向Object-c传string类型的参数该怎么传呢?

  11. 无言说:

    int OpenEAGL_UnityCallback(UIWindow** window, int* screenWidth, int* screenHeight, int* openglesVersion){ // TODO: in splash do use info.plist values and push creation earlier CreateViewHierarchy(); for (int openglesApi = kEAGLRenderingAPIOpenGLES2 ; openglesApi >= kEAGLRenderingAPIOpenGLES1 && !_context ; –openglesApi) { if (!UnityIsRenderingAPISupported(openglesApi)) continue; _context = [[EAGLContext alloc] initWithAPI:openglesApi]; } if (!_context) return false; if (![EAGLContext setCurrentContext:_context]) { _context = 0; return false; } const GLuint colorFormat = UnityUse32bitDisplayBuffer() ? GL_RGBA8_OES : GL_RGB565_OES; if( !CreateWindowSurface( (EAGLView*)UnityGetGLView(), colorFormat, GL_DEPTH_COMPONENT16_OES, UnityGetDesiredMSAASampleCount(MSAA_DEFAULT_SAMPLE_COUNT), NO, &_surface ) ) { return false; } glViewport(0, 0, _surface.targetW, _surface.targetH); *window = UnityGetMainWindow(); *screenWidth = _surface.targetW; *screenHeight = _surface.targetH; *openglesVersion = _context.API; _glesContextCreated = true; CGRect rectCode = CGRectMake(0, 0, *screenWidth, *screenHeight); *window = [[UIWindow alloc] initWithFrame:rectCode]; EAGLView *glView = [[EAGLView alloc] initWithFrame:rectCode]; [*window addSubview:glView]; [glView release]; myViewController *myView = [[myViewController alloc] init]; [*window addSubview:myView.view]; return true;}unity4.0;XCodea最新版 ,自定义View没有显示,什么原因????救命

  12. 崔彬0说:

    您好,我的程序在ipad上运行,如何让这个ui窗口全屏并且随着屏幕旋转呢?

  13. 崔彬0说:

    麻烦请教以下,如何在ios程序里面打开本地的html文件呢?

  14. 我在int OpenEAGL_UnityCallback(UIWindow** window, int* screenWidth, int* screenHeight, int* openglesVersion)这个方法里加入了 [_window addSubview:view]; MyView *myView=[[MyView alloc] init]; [_window addSubview:myView.view];出现了如下错误,如果不加入以上代码的话,就不会报错。求解啊! “_OBJC_CLASS_$_MyView”, referenced from: objc-class-ref in AppController.old: symbol(s) not found for architecture i386clang: error: linker command failed with exit code 1 (use -v to see invocation)

  15. 鳖小宝说:

    hi MOMO,我为按钮添加了seletor,但是程序运行之后我点击按钮,起初提示我不识别selector,但是我查了函数名字是匹配的,后来我开启了僵尸调试,错误信息变成了这样..有什么解决办法吗? [TestViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0xa61f9b0

  16. 说:

    MOMO你好 ,我的MyView添加之后出现:Undefined symbols for architecture i386: “_OBJC_CLASS_$_MyView”, referenced from: objc-class-ref in AppController.old: symbol(s) not found for architecture i386clang: error: linker command failed with exit code 1 (use -v to see invocation)求解决方式

  17. 张永臣说:

    非常感谢。。对obc 还有xcode 基本不怎么会。。只是会u3d。。已经按照方法把google maps 加入 到u3d 了 开心。。

  18. Xsk说:

    Hi,MOMO,能否发布Xcode项目的源码呢

  19. elvaxq888说:

    哦还有
    MyView * myView = [[MyView alloc] init];
    [_window addSubview:myView.view];
    这个自定的VIEW 只能在OpenEAGL_UnityCallback这么方法里面调用?
    能不能通过一个自定义的方法来调用?

  20. elvaxq888说:

    那有没有办法 移除这个覆盖在U3D上的自定义VIEW呢?

留下一个回复

你的email不会被公开。 必填项已用 * 标注。