Unity3d与iOS的交互

Unity3d与iOS的交互(1)



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



1.

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





2.

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






3.

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

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

上面的四个函数分别朝不同角度选择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.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface MyViewInit : NSObject  
  12.   
  13.   
  14. +(void)CreateButton:title rect:(CGRect)rect action:(SEL)action controller:(UIViewController *)controller;  
  15. +(void)CreateTitle:(UIViewController *)controller;  
  16. +(void)LeftButtonPressed;  
  17. +(void)RightButtonPressed;    
  18. +(void)UpButtonPressed;  
  19. +(void)DownButtonPressed;  
  20. +(void)Init:(UIViewController *)controller;  
  21.   
  22.   
  23. @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.   
  9. #import "MyViewInit.h"  
  10.   
  11. @interface MyViewInit ()  
  12.   
  13. @end  
  14.   
  15. @implementation MyViewInit  
  16.   
  17.   
  18. //创建按钮  
  19. +(void)CreateButton:title rect:(CGRect)rect action:(SEL)action controller:(UIViewController *)controller  
  20. {  
  21.     UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  22.   
  23.     //设置按钮范围      
  24.     btn.frame = rect;  
  25.   
  26.     //设置按钮显示内容      
  27.     [btn setTitle:title forState:UIControlStateNormal];  
  28.   
  29.     //设置按钮改变后绑定的响应方法      
  30.     [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];      
  31.       
  32.     //加入View中  
  33.     [controller.view addSubview:btn];  
  34. }  
  35.   
  36.   
  37. //创建标签  
  38. +(void)CreateTitle:(UIViewController *)controller  
  39. {  
  40.     //创建label视图      
  41.     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];  
  42.     //设置显示内容      
  43.     label.text = @"旋转控制";      
  44.     //设置背景颜色      
  45.     label.backgroundColor = [UIColor blueColor];      
  46.     //设置文字颜色      
  47.     label.textColor = [UIColor whiteColor];      
  48.     //设置显示位置居中      
  49.     label.textAlignment = UITextAlignmentCenter;      
  50.     //设置字体大小      
  51.     label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20];   
  52.       
  53.     [controller.view addSubview:label];  
  54. }  
  55.   
  56.   
  57. //向左按钮    
  58. +(void)LeftButtonPressed  
  59. {    
  60.     UnitySendMessage("Cube""MoveLeft","");    
  61. }    
  62.   
  63. //向右按钮    
  64. +(void)RightButtonPressed  
  65. {    
  66.     UnitySendMessage("Cube""MoveRight","");    
  67. }    
  68. //向上按钮    
  69. +(void)UpButtonPressed  
  70. {    
  71.     UnitySendMessage("Cube""MoveUp","");    
  72. }    
  73.   
  74. //向下按钮    
  75. +(void)DownButtonPressed  
  76. {    
  77.     UnitySendMessage("Cube""MoveDown","");    
  78. }    
  79.   
  80.   
  81. +(void)Init:(UIViewController *)controller  
  82. {  
  83.     [self CreateTitle:controller];  
  84.       
  85.     [self CreateButton:@"左旋转" rect:CGRectMake(0,  50, 100, 40) action:@selector(LeftButtonPressed) controller:controller];  
  86.     [self CreateButton:@"右旋转" rect:CGRectMake(0, 100, 100, 40) action:@selector(RightButtonPressed) controller:controller];  
  87.     [self CreateButton:@"上旋转" rect:CGRectMake(0, 150, 100, 40) action:@selector(UpButtonPressed) controller:controller];  
  88.     [self CreateButton:@"下旋转" rect:CGRectMake(0, 200, 100, 40) action:@selector(DownButtonPressed) controller:controller];  
  89. }  
  90.   
  91.   
  92.   
  93. @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.   
  5.     // Create a full-screen window  
  6.     _window = [[UIWindow alloc] initWithFrame:rect];  
  7.       
  8.       
  9.     EAGLView* view = [[EAGLView alloc] initWithFrame:rect];  
  10.     UnityViewController *controller = [[UnityViewController alloc] init];  
  11.       
  12.       
  13.     sGLViewController = controller;  
  14.   
  15. #if defined(__IPHONE_3_0)  
  16.     if( _ios30orNewer )  
  17.         controller.wantsFullScreenLayout = TRUE;  
  18. #endif  
  19.   
  20.     controller.view = view;  
  21.     [_window addSubview:view];  
  22.       
  23.       
  24.     [MyViewInit init:controller];  
  25.   
  26.   
  27.     if( !UnityUseOSAutorotation() )  
  28.     {  
  29.         _autorotEnableHandling = true;  
  30.         [[NSNotificationCenter defaultCenter] postNotificationName: UIDeviceOrientationDidChangeNotification object: [UIDevice currentDevice]];  
  31.     }  
  32.   
  33. ......  


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

[cpp]  view plain copy
  1. [MyViewInit init:controller];  


并在该文件首部加入:

[cpp]  view plain copy
  1. #import "MyViewInit.h"  




7.

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

初始界面:





点击下旋转按钮:




旋转Cube:




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





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





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

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



From: http://blog.csdn.net/htttw/article/details/7982134



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值