天天看点

Unity3d与iOS的交互(1)

Unity3d与iOS的交互(1)

今天我们介绍Unity3d与iOS交互第一部分:iOS传消息到Unity3d中。下面我们开始吧:

1.

首先用Unity3d创建一个Plain,并调整好摄像机的角度以及光源的位置,如下所示:

Unity3d与iOS的交互(1)

2.

然后我们创建一个Cube,我们会在iOS中用Objective-C代码来控制它旋转:

Unity3d与iOS的交互(1)

3.

然后我们创建一个Rotate.js的脚本并把它关联到Cube上:

[javascript] view plain copy

  1. var vrotate : Vector3;    
  2. //Rotate Left  
  3. function RotateLeft()    
  4. {  
  5.     print("Rotate Left");  
  6.     transform.Rotate(Vector3.up * Time.deltaTime * 100, Space.World);  
  7. }    
  8. //Rotate Right  
  9. function RotateRight()    
  10. {  
  11.     print("Rotate Right");  
  12.     transform.Rotate(Vector3.down * Time.deltaTime * 100, Space.World);      
  13. }    
  14. //Rotate Up  
  15. function RotateUp()  
  16. {  
  17.     print("Rotate Up");  
  18.     transform.Rotate(Vector3.right * Time.deltaTime * 100, Space.World);      
  19. }    
  20. //Rotate Down  
  21. function RotateDown()  
  22. {  
  23.     print("Rotate Down");  
  24.     transform.Rotate(Vector3.left * Time.deltaTime * 100, Space.World);      
  25. }  

上面的四个函数分别朝不同角度选择Cube。我们会在iOS程序中调用这几个Unity3d中的函数。

4.

然后在Unity3d中Build为XCode项目,打开该项目。首先创建一个基于NSObject的类,名为MyViewInit,我们会将我们自己的界面初始化代码都放在其中。修改MyViewInit.h如下:

[cpp] view plain copy

  1. //  
  2. //  MyViewInit.h  
  3. //  Unity-iPhone  
  4. //  
  5. //  Created by tao hu on 9/15/12.  
  6. //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.  
  7. //  
  8. #import <UIKit/UIKit.h>  
  9. @interface MyViewInit : NSObject  
  10. +(void)CreateButton:title rect:(CGRect)rect action:(SEL)action controller:(UIViewController *)controller;  
  11. +(void)CreateTitle:(UIViewController *)controller;  
  12. +(void)LeftButtonPressed;  
  13. +(void)RightButtonPressed;    
  14. +(void)UpButtonPressed;  
  15. +(void)DownButtonPressed;  
  16. +(void)Init:(UIViewController *)controller;  
  17. @end  

其中的方法都是类方法。在Init函数中我们完成了所有我们自定义界面的绘制(添加了四个UIButton和一个UILabel)。

5.

修改MyViewInit.m如下:

[cpp] view plain copy

  1. //  
  2. //  MyViewInit.m  
  3. //  Unity-iPhone  
  4. //  
  5. //  Created by tao hu on 9/15/12.  
  6. //  Copyright (c) 2012 __MyCompanyName__. All rights reserved.  
  7. //  
  8. #import "MyViewInit.h"  
  9. @interface MyViewInit ()  
  10. @end  
  11. @implementation MyViewInit  
  12. //创建按钮  
  13. +(void)CreateButton:title rect:(CGRect)rect action:(SEL)action controller:(UIViewController *)controller  
  14. {  
  15.     UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  16.     //设置按钮范围      
  17.     btn.frame = rect;  
  18.     //设置按钮显示内容      
  19.     [btn setTitle:title forState:UIControlStateNormal];  
  20.     //设置按钮改变后绑定的响应方法      
  21.     [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];      
  22.     //加入View中  
  23.     [controller.view addSubview:btn];  
  24. }  
  25. //创建标签  
  26. +(void)CreateTitle:(UIViewController *)controller  
  27. {  
  28.     //创建label视图      
  29.     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];  
  30.     //设置显示内容      
  31.     label.text = @"旋转控制";      
  32.     //设置背景颜色      
  33.     label.backgroundColor = [UIColor blueColor];      
  34.     //设置文字颜色      
  35.     label.textColor = [UIColor whiteColor];      
  36.     //设置显示位置居中      
  37.     label.textAlignment = UITextAlignmentCenter;      
  38.     //设置字体大小      
  39.     label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20];   
  40.     [controller.view addSubview:label];  
  41. }  
  42. //向左按钮    
  43. +(void)LeftButtonPressed  
  44. {    
  45.     UnitySendMessage("Cube", "MoveLeft","");    
  46. }    
  47. //向右按钮    
  48. +(void)RightButtonPressed  
  49. {    
  50.     UnitySendMessage("Cube", "MoveRight","");    
  51. }    
  52. //向上按钮    
  53. +(void)UpButtonPressed  
  54. {    
  55.     UnitySendMessage("Cube", "MoveUp","");    
  56. }    
  57. //向下按钮    
  58. +(void)DownButtonPressed  
  59. {    
  60.     UnitySendMessage("Cube", "MoveDown","");    
  61. }    
  62. +(void)Init:(UIViewController *)controller  
  63. {  
  64.     [self CreateTitle:controller];  
  65.     [self CreateButton:@"左旋转" rect:CGRectMake(0,  50, 100, 40) action:@selector(LeftButtonPressed) controller:controller];  
  66.     [self CreateButton:@"右旋转" rect:CGRectMake(0, 100, 100, 40) action:@selector(RightButtonPressed) controller:controller];  
  67.     [self CreateButton:@"上旋转" rect:CGRectMake(0, 150, 100, 40) action:@selector(UpButtonPressed) controller:controller];  
  68.     [self CreateButton:@"下旋转" rect:CGRectMake(0, 200, 100, 40) action:@selector(DownButtonPressed) controller:controller];  
  69. }  
  70. @end  

其中:

UnitySendMessage函数有三个参数:

第一个是Scene中的模型的名称,第二个是已经绑定在模型上的某个函数,第三个是char *类型的参数,用来将参数传递给这个函数。

我们可以用这个方法调用Unity3d中的任意一个模型上的任意一个函数。

6.

最后我们要做的是在程序启动时调用上面的Init函数。找到AppController.mm文件:

[cpp] view plain copy

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

函数在Unity3d程序启动时会被调用,其中的EAGLView 就是Unity3d的背景的那个View。我们在这个函数中调用我们的Init函数。修改OpenEAGL_UnityCallback如下:

[cpp] view plain copy

  1. int OpenEAGL_UnityCallback(UIWindow** window, int* screenWidth, int* screenHeight,  int* openglesVersion)  
  2. {  
  3.     CGRect rect = [[UIScreen mainScreen] bounds];  
  4.     // Create a full-screen window  
  5.     _window = [[UIWindow alloc] initWithFrame:rect];  
  6.     EAGLView* view = [[EAGLView alloc] initWithFrame:rect];  
  7.     UnityViewController *controller = [[UnityViewController alloc] init];  
  8.     sGLViewController = controller;  
  9. #if defined(__IPHONE_3_0)  
  10.     if( _ios30orNewer )  
  11.         controller.wantsFullScreenLayout = TRUE;  
  12. #endif  
  13.     controller.view = view;  
  14.     [_window addSubview:view];  
  15.     [MyViewInit init:controller];  
  16.     if( !UnityUseOSAutorotation() )  
  17.     {  
  18.         _autorotEnableHandling = true;  
  19.         [[NSNotificationCenter defaultCenter] postNotificationName: UIDeviceOrientationDidChangeNotification object: [UIDevice currentDevice]];  
  20.     }  
  21. ......  

其实我们只加入了一句话:

[cpp] view plain copy

  1. [MyViewInit init:controller];  

并在该文件首部加入:

[cpp] view plain copy

  1. #import "MyViewInit.h"  

7.

好了,终于要在真机上运行了:

初始界面:

Unity3d与iOS的交互(1)

点击下旋转按钮:

Unity3d与iOS的交互(1)

旋转Cube:

Unity3d与iOS的交互(1)

我们发现在XCode的Console窗口中输出了Unity3d中的pirnt语句。因此,Unity3d中的print函数在真机上运行时是输出到XCode的Console中。

最后代码太大了,无法上传,有需要的可以和我联系或在下面留言。

本文参考了雨松MOMO的文章,在此表示感谢:

http://blog.csdn.net/xys289187120/article/details/6926746

继续阅读