Unity3D 游戏引擎之iOS高级界面消息发送与接收

移动开发 iOS 游戏开发
本例实现游戏背景是Unity3D 的游戏世界,前面添加4个IOS的高级界面的按钮,并且点击这些按钮可以将消息传递给背景的Unity3D ,让它做一些事情。

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

本例实现游戏背景是Unity3D 的游戏世界,前面添加4个IOS的高级界面的按钮,并且点击这些按钮可以将消息传递给背景的Unity3D ,让它做一些事情。
上一章介绍了触摸IOS屏幕 移动摄像机的位置,下面有盆友问我说他不想移动摄像机的位置,就想移动物体的位置,我在这里补充一下,可以把脚本绑定在箱子上,参照物选择为主摄像机,这样子更新箱子的脚本就OK啦。今天例子,我就将脚本绑定在箱子上,如下图所示,把Move脚本绑定在这个 Cube中。
先把Move脚本的代码贴出来,这里面我写了4个方法分别处理这个箱子的旋转,这4个方法是由IOS上的代码向Unity发送消息后调用的,下面我会介绍具体操作的方法。

[代码]js代码:

01 var vrotate : Vector3; 
02    
03 //向左旋转 
04 function MoveLeft() 
05
06     var rotate : float = Time.deltaTime * 100;  
07     vrotate = Vector3.up * rotate; 
08     transform.Rotate(vrotate, Space.World);   
09
10    
11 //向右旋转 
12 function MoveRight() 
13
14     var rotate : float = Time.deltaTime * 100;  
15     vrotate = Vector3.down* rotate; 
16     transform.Rotate(vrotate, Space.World);   
17
18    
19 //向上旋转 
20 function MoveUp(){ 
21     var rotate : float = Time.deltaTime * 100;  
22     vrotate = Vector3.right* rotate; 
23     transform.Rotate(vrotate, Space.World);   
24
25    
26 //向下旋转 
27 function MoveDown(){ 
28     var rotate : float = Time.deltaTime * 100;  
29     vrotate = Vector3.left* rotate; 
30     transform.Rotate(vrotate, Space.World);   
31 }
到这里盆友们可以将这个Unity工程导出成Xcode项目,不会的盆友请看我之前的文章哈,Xcode项目导出成功后,我们先添加4个高级界面的按钮用来点击响应上面脚本的这4个旋转箱子的方法。
 
创建一个类继承UIViewController,用来添加我们的高级界面的视图,我暂且命名为MyView.
 
打开Unity3D导出的AppController.mm这个类,头文件处先导入我们的这个类 #import "MyView"

找到下面这个方法,来添加view

int OpenEAGL_UnityCallback(UIWindow** window, int* screenWidth, int* screenHeight,  int* openglesVersion)

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

[代码]c#/cpp/oc代码:

1 // Create a full-screen window 
2 _window = [[UIWindow alloc] initWithFrame:rect]; 
3 EAGLView* view = [[EAGLView alloc] initWithFrame:rect]; 
4 [_window addSubview:view]; 
5    
6 MyView * myView =  [[MyView alloc] init]; 
7 [_window addSubview:myView.view];
贴出MyView的代码,写完发现忘释放内存了,呵呵,懒得改了,本章主要的介绍的不是这个哦。

[代码]c#/cpp/oc代码:

001 // 
002 //  MyView.m 
003 //  Unity-iPhone 
004 // 
005 //  Created by 雨松MOMO on 11-11-1. 
006 //  Copyright 2011 __MyCompanyName__. All rights reserved. 
007 // 
008    
009 #import "MyView.h" 
010    
011    
012 @implementation MyView 
013    
014    
015 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
016 - (void)viewDidLoad { 
017     [super viewDidLoad]; 
018     //创建label视图   
019     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];   
020     //设置显示内容   
021     label.text = @"雨松MOMO的程序世界";   
022     //设置背景颜色   
023     label.backgroundColor = [UIColor blueColor];   
024     //设置文字颜色   
025     label.textColor = [UIColor whiteColor];   
026     //设置显示位置居中   
027     label.textAlignment = UITextAlignmentCenter;   
028     //设置字体大小   
029     label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20]; 
030        
031     //创建按钮   
032     UIButton *button0 = [UIButton buttonWithType:1];   
033     //设置按钮范围   
034     button0.frame = CGRectMake(0, 40, 100, 30);   
035     //设置按钮显示内容   
036     [button0 setTitle:@"矩形左旋转" forState:UIControlStateNormal];   
037     //设置按钮改变后 绑定响应方法   
038     [button0 addTarget:self action:@selector(LeftButtonPressed) forControlEvents:UIControlEventTouchUpInside];   
039        
040     //创建按钮   
041     UIButton *button1 = [UIButton buttonWithType:1];   
042     //设置按钮范围   
043     button1.frame = CGRectMake(0, 100, 100, 30);   
044     //设置按钮显示内容   
045     [button1 setTitle:@"矩形右旋转" forState:UIControlStateNormal];   
046     //设置按钮改变后 绑定响应方法   
047     [button1 addTarget:self action:@selector(RightButtonPressed) forControlEvents:UIControlEventTouchUpInside];   
048    
049     //创建按钮   
050     UIButton *button2 = [UIButton buttonWithType:1];   
051     //设置按钮范围   
052     button2.frame = CGRectMake(0, 160, 100, 30);   
053     //设置按钮显示内容   
054     [button2 setTitle:@"矩形上旋转" forState:UIControlStateNormal];   
055     //设置按钮改变后 绑定响应方法   
056     [button2 addTarget:self action:@selector(UpButtonPressed) forControlEvents:UIControlEventTouchUpInside];   
057        
058     //创建按钮   
059     UIButton *button3 = [UIButton buttonWithType:1];   
060     //设置按钮范围   
061     button3.frame = CGRectMake(0, 220, 100, 30);   
062     //设置按钮显示内容   
063     [button3 setTitle:@"矩形下旋转" forState:UIControlStateNormal];   
064     //设置按钮改变后 绑定响应方法   
065     [button3 addTarget:self action:@selector(DownButtonPressed) forControlEvents:UIControlEventTouchUpInside];   
066        
067        
068     //向view添加 
069     [self.view addSubview:label]; 
070     [self.view addSubview:button0]; 
071     [self.view addSubview:button1]; 
072     [self.view addSubview:button2]; 
073     [self.view addSubview:button3]; 
074
075    
076 //向左按钮 
077 -(void)LeftButtonPressed{ 
078     UnitySendMessage("Cube","MoveLeft",""); 
079
080    
081 //向右按钮 
082 -(void)RightButtonPressed{ 
083     UnitySendMessage("Cube","MoveRight",""); 
084
085 //向上按钮 
086 -(void)UpButtonPressed{ 
087     UnitySendMessage("Cube","MoveUp",""); 
088
089    
090 //向下按钮 
091 -(void)DownButtonPressed{ 
092     UnitySendMessage("Cube","MoveDown",""); 
093
094    
095    
096    
097 - (void)didReceiveMemoryWarning { 
098     // Releases the view if it doesn't have a superview. 
099     [super didReceiveMemoryWarning]; 
100        
101     // Release any cached data, images, etc. that aren't in use. 
102
103    
104 - (void)viewDidUnload { 
105     [super viewDidUnload]; 
106
107    
108    
109 - (void)dealloc { 
110     [super dealloc]; 
111
112    
113    
114 @end
这里我主要说一下下面这个方法,它是Unity底层帮我们写好的一个方法,意思iPhone向向Unity发送消息,

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

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

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

UnitySendMessage("Cube","MoveLeft","");

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

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

Move.unitypackage.zip

责任编辑:冰凝儿
相关推荐

2012-12-24 08:57:35

iOSUnity3D

2012-12-24 08:40:12

2012-12-24 08:46:50

iOSUnity3D

2012-12-24 09:01:41

iOSUnity3D

2012-12-24 08:48:25

iOSUnity3D

2012-12-24 09:04:04

iOSUnity3D

2012-12-24 08:50:21

iOSUnity3D

2012-12-24 08:59:13

iOSUnity3D

2012-12-24 08:52:44

iOSUnity3D

2012-12-24 08:54:47

iOSUnity3D

2012-12-24 08:45:19

iOSUnity3D

2012-12-24 08:51:23

iOSUnity3D

2012-12-24 09:06:14

iOSUnity3D

2013-04-25 00:06:06

unity3D手机游戏引擎

2012-12-24 09:07:09

iOSUnity3D

2012-12-24 09:11:58

iOSUnity3D

2012-12-24 09:00:31

iOSUnity3D

2012-12-24 09:09:27

AndoidUnity3D

2012-12-24 09:02:48

iOSUnity3D

2013-04-25 09:56:24

unity3D手机游戏引擎
点赞
收藏

51CTO技术栈公众号