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






上一章介绍了触摸IOS屏幕 移动摄像机的位置,下面有盆友问我说他不想移动摄像机的位置,就想移动物体的位置,我在这里补充一下,可以把脚本绑定在箱子上,参照物选择为主摄像机,这样子更新箱子的脚本就OK啦。今天例子,我就将脚本绑定在箱子上,如下图所示,把Move脚本绑定在这个 Cube中。












unity游戏ios平台接入Apple支付sdk ios unity_发送消息










先把Move脚本的代码贴出来,这里面我写了4个方法分别处理这个箱子的旋转,这4个方法是由IOS上的代码向Unity发送消息后调用的,下面我会介绍具体操作的方法。




 

1. var
2.    
3. //向左旋转  
4. function
5. {   
6. var rotate : float
7.     vrotate = Vector3.up * rotate;   
8.     transform.Rotate(vrotate, Space.World);     
9. }   
10.    
11. //向右旋转  
12. function
13. {   
14. var rotate : float
15.     vrotate = Vector3.down* rotate;   
16.     transform.Rotate(vrotate, Space.World);     
17. }   
18.    
19. //向上旋转  
20. function
21. var rotate : float
22.     vrotate = Vector3.right* rotate;   
23.     transform.Rotate(vrotate, Space.World);     
24. }   
25.    
26. //向下旋转  
27. function
28. var rotate : float
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 覆盖在它上面。


 


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的代码,写完发现忘释放内存了,呵呵,懒得改了,本章主要的介绍的不是这个哦。

 

 


1. //  
2. //  MyView.m  
3. //  Unity-iPhone  
4. //  
5. //  Created by 雨松MOMO on 11-11-1.  
6. //  Copyright 2011 __MyCompanyName__. All rights reserved.  
7. //  
8.    
9. #import "MyView.h"  
10.    
11.    
12. @implementation MyView   
13.    
14.    
15. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.  
16. - (void)viewDidLoad {   
17.     [super viewDidLoad];   
18. //创建label视图    
19.     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];     
20. //设置显示内容    
21. "雨松MOMO的程序世界";     
22. //设置背景颜色    
23.     label.backgroundColor = [UIColor blueColor];     
24. //设置文字颜色    
25.     label.textColor = [UIColor whiteColor];     
26. //设置显示位置居中    
27.     label.textAlignment = UITextAlignmentCenter;     
28. //设置字体大小    
29.     label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20];   
30.        
31. //创建按钮    
32.     UIButton *button0 = [UIButton buttonWithType:1];     
33. //设置按钮范围    
34.     button0.frame = CGRectMake(0, 40, 100, 30);     
35. //设置按钮显示内容    
36. "矩形左旋转"
37. //设置按钮改变后 绑定响应方法    
38.     [button0 addTarget:self action:@selector(LeftButtonPressed) forControlEvents:UIControlEventTouchUpInside];     
39.        
40. //创建按钮    
41.     UIButton *button1 = [UIButton buttonWithType:1];     
42. //设置按钮范围    
43.     button1.frame = CGRectMake(0, 100, 100, 30);     
44. //设置按钮显示内容    
45. "矩形右旋转"
46. //设置按钮改变后 绑定响应方法    
47.     [button1 addTarget:self action:@selector(RightButtonPressed) forControlEvents:UIControlEventTouchUpInside];     
48.    
49. //创建按钮    
50.     UIButton *button2 = [UIButton buttonWithType:1];     
51. //设置按钮范围    
52.     button2.frame = CGRectMake(0, 160, 100, 30);     
53. //设置按钮显示内容    
54. "矩形上旋转"
55. //设置按钮改变后 绑定响应方法    
56.     [button2 addTarget:self action:@selector(UpButtonPressed) forControlEvents:UIControlEventTouchUpInside];     
57.        
58. //创建按钮    
59.     UIButton *button3 = [UIButton buttonWithType:1];     
60. //设置按钮范围    
61.     button3.frame = CGRectMake(0, 220, 100, 30);     
62. //设置按钮显示内容    
63. "矩形下旋转"
64. //设置按钮改变后 绑定响应方法    
65.     [button3 addTarget:self action:@selector(DownButtonPressed) forControlEvents:UIControlEventTouchUpInside];     
66.        
67.        
68. //向view添加  
69.     [self.view addSubview:label];   
70.     [self.view addSubview:button0];   
71.     [self.view addSubview:button1];   
72.     [self.view addSubview:button2];   
73.     [self.view addSubview:button3];   
74. }   
75.    
76. //向左按钮  
77. -(void)LeftButtonPressed{   
78. "Cube","MoveLeft","");   
79. }   
80.    
81. //向右按钮  
82. -(void)RightButtonPressed{   
83. "Cube","MoveRight","");   
84. }   
85. //向上按钮  
86. -(void)UpButtonPressed{   
87. "Cube","MoveUp","");   
88. }   
89.    
90. //向下按钮  
91. -(void)DownButtonPressed{   
92. "Cube","MoveDown","");   
93. }   
94.    
95.    
96.    
97. - (void)didReceiveMemoryWarning {   
98. // Releases the view if it doesn't have a superview.  
99.     [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中任意模型发送消息调用它绑定的脚本中的方法,当前前提是模型名称、方法名称、 参数都填写正确。