Unity3D和iOS的对接及界面跳转
界面1 界面2 界面3 界面4
所需工具:
- Unity3d 5.0版本
- Xcode 6.2版本 模拟器的操作系统8.2v
步骤1. 创建一个unity项目
- 创建一个Unity项目,并同时创建一个Cube,一个Text(作为cube的名字)和一个button按钮。如何创建这里就不在叙述,稍后附件中会有源代码。如下图:
- 在Unity的Project栏目的Assests中增加一个.mm文件,文件名自定义,我这里是TNAppController.mm,用xcode编写在拖到这里即可,内容为:
#import <UIKit/UIKit.h>
#import "UnityAppController.h"
#import "UI/UnityView.h"
#import "UI/UnityViewControllerBase.h"
#import "HelloViewController.h"
@interface TNAppController : UnityAppController
@property (nonatomic, strong) UINavigationController *navController;
- (void)willStartWithViewController:(UIViewController*)controller;
@end
@implementation TNAppController
- (void)willStartWithViewController:(UIViewController*)controller
{
_rootController = [[UIViewController alloc] init];
_rootView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_rootController.view = _rootView;
HelloViewController *helloVC = [[HelloViewController alloc] initWithNibName:nil bundle:nil];
self.navController = [[UINavigationController alloc] initWithRootViewController:helloVC];
[_rootView addSubview:self.navController.view];
}
@end
IMPL_APP_CONTROLLER_SUBCLASS(TNAppController)
说明:
a. 导入的部分都为Untiy生成xcode项目中自带的.h文件,以后再导入也可以,HelloViewController.h文件为我们以后要创建的文件。
b. 这个文件继承UnityAppController.h这个文件,这点很重要,UnityAppController中提供了很多我们可以直接调用的属性,以便我们自己写的iOS文件可以使用window,rootController等属性,这些属性要和Untiy导出的iOS项目使用的是同一个。
c. willStartWithViewController这个方法好像是只有Unity 5左右的版本才用的是这个方法,4.x以前的版本使用的不是这个方法。这个方法是将要打开视图控制器的初始化方法。初始化根视图控制器和根视图。
d. IMPL_APP_CONTROLLER_SUBCLASS 这个很重要,它决定了我们的TNAppController.h 这个文件将被重载。UnityAppController.h 文件中有解释,如下图:
- 在Assets文件夹中,创建一个C# javascript文件,代码如下:
说明:
a. [DllImport ("__Internal")] 引入一个dll的内部文件.
b. 声明一个可调用的接口, private static extern int ActivateUI_iOS(int index);该方法可以有任意多个参数,这个参数的使用,到时候我们在iOS模拟器上可以看到,并且可以接收到iOS的返回值。
c. Start方法初始化Button,将button.cs文件与Hierarchy中的创建的Button组件进行关联。
- 在Assets文件夹中,创建一个test.js文件,代码如下图:
说明:
a. SetText(Input)方法,我们将在ios项目中调用这个方法,动态修改cube的名字。returnVal()方法是没有用的。
b. 我们将test.js文件和Hierarchy中的cube组件关联,在将text组件和cube关联,text组件作为cube组件的名称。
- 我们开始生成IOS项目,unity->file->build & settings,选中iOS,点击player settings,这里就不详细叙述了
步骤2. 配置iOS项目
- 生成iOS项目目录如下:
我们在untiy中创建的TNAppController.mm文件会出现在Libraries包中,创建的test.js,和button.cs文件会编译成dll.s文件中去
我们在classes文件夹中创建如下文件, MyViewInit.h和MyViewInit.mm文件,mm结尾的,是c++识别的文件,其中有c++代码
- MyViewInit.h:
//
// MyViewInit.h
// Unity-iPhone
//
// Created by wencun on 4/3/15.
//
//
#import <Foundation/Foundation.h>
@interface MyViewInit : NSObject
+ (void) enabled;
+ (void) disabled;
+ (void) setEnabled:(NSString *)foo;
+ (int) value;
@end
#pragma mark cplusplus code
#ifdef __cplusplus
extern "C" {
#endif
int ActivateUI_iOS(int index);
#ifdef __cplusplus
} // extern "C"
#endif
- MyViewInit.mm:
//
// MyViewInit.m
// Unity-iPhone
//
// Created by wencun on 4/3/15.
//
//
#import "MyViewInit.h"
#import "UnityInterface.h"
@implementation MyViewInit
+ (void) enabled{
UnitySendMessage("Cube", "Enabled", "");
}
+ (void) disabled{
UnitySendMessage("Cube", "Disabled", "");
}
+ (void) setEnabled:(NSString *)foo{
UnitySendMessage("Cube", "SetText", [foo UTF8String]);
}
+ (int) value{
// return UnitySendMessage("Cube", "returnVal", "");
return 11;
}
@end
#pragma mark cplusplus code
extern "C"{
int ActivateUI_iOS(int index){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unity调用了IOS方法" message:[NSString stringWithFormat:@"我是第%i次从Unity过来的!",index] delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
[alert show];
// NSString *s = [NSString stringWithCharacters:index length:99];
// NSNumber *num = [NSNumber numberWithChar:index];
return ++index;
}
}
其中,UnitySendMessage为iOS调用Unity中的方法,第一个参数为组件名称,第二个参数为方法名,第三个参数为方法的参数,该方法的返回值为void,所有不能接受调用Unity方法中的返回值
pragma下方的代码为c++代码,为ActivateUI_iOS的实现,这样,点击Unity中的按钮就可以调用这个方法了,该方法有一个返回值,Unity中可以接收到
- 创建HelloViewController.h文件,继承UIViewController
//
// HelloViewController.m
// Unity-iPhone
//
// Created by wencun on 4/3/15.
//
//
#import "HelloViewController.h"
#import "CoolUnitySceneviewController.h"
@interface HelloViewController ()
@end
@implementation HelloViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
UIButton *btnNext = [UIButton buttonWithType:UIButtonTypeSystem];
[btnNext setTitle:@"这是一个IOS界面,点我进入Unity界面。" forState:UIControlStateNormal];
btnNext.frame = CGRectMake(40, 200, 300, 44);
btnNext.layer.borderWidth = 1;
// btnNext.layer.borderColor = [UIColor grayColor];
btnNext.layer.cornerRadius = 3;
btnNext.backgroundColor = [UIColor greenColor];
// btnNext.center = CGPointMake(self.view.bounds.size.height / 2, 200);
[self.view addSubview:btnNext];
self.view.backgroundColor = [UIColor grayColor];
[btnNext addTarget:self action:@selector(gotoUnityScene:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - Private methods
- (void)gotoUnityScene:(id)sender {
NSLog(@"[HelloVC] Go to unity scene");
CoolUnitySceneViewController *coolUnityVC = [[CoolUnitySceneViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:coolUnityVC animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
- 创建CoolUnitySceneViewController.h文件,继承UIViewController,如下图:
//
// CoolUnitySceneviewController.m
// Unity-iPhone
//
// Created by wencun on 4/3/15.
//
//
#import "CoolUnitySceneViewController.h"
#import "GoodByeViewController.h"
#include "UI/UnityViewControllerBase.h"
#include "UnityAppController+ViewHandling.h"
#import "MyViewInit.h"
@interface CoolUnitySceneViewController ()
@end
@implementation CoolUnitySceneViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view addSubview:GetAppController().unityView];
GetAppController().unityView.frame = self.view.frame;
UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(40, 50, 300, 40)];
text.text = @"欢迎进入Unity界面!(IOS)";
text.textAlignment = NSTextAlignmentCenter;
text.backgroundColor = [UIColor greenColor];
[self.view addSubview:text];
UITextField *tv = [[UITextField alloc] initWithFrame:CGRectMake(40, 100, 300, 40)];
tv.textAlignment = NSTextAlignmentCenter;
tv.layer.borderWidth = 1;
tv.layer.cornerRadius = 3;
[tv setPlaceholder:@"请键入文字..."];
tv.tag = 101;
[self.view addSubview:tv];
UIButton *btnNext = [UIButton buttonWithType:UIButtonTypeSystem];
[btnNext setTitle:@"点我可以修改CUBE中的文字(IOS)" forState:UIControlStateNormal];
btnNext.frame = CGRectMake(40, 150, 300, 44);
btnNext.backgroundColor = [UIColor whiteColor];
btnNext.layer.borderWidth = 1;
btnNext.layer.cornerRadius = 3;
[self.view addSubview:btnNext];
[btnNext addTarget:self action:@selector(goToLastScene:) forControlEvents:UIControlEventTouchUpInside];
UIButton *recevied = [UIButton buttonWithType:UIButtonTypeSystem];
[recevied setTitle:@"点我可以进入到另外一个IOS界面哦!(IOS)" forState:UIControlStateNormal];
recevied.frame = CGRectMake(40, 450, 300, 44);
recevied.backgroundColor = [UIColor whiteColor];
recevied.layer.borderWidth = 1;
recevied.layer.cornerRadius = 3;
// btnNext.center = CGPointMake(self.view.bounds.size.height / 2, self.view.bounds.size.width / 2);
[self.view addSubview:recevied];
[recevied addTarget:self action:@selector(recevied:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - Private methods
- (void)goToLastScene:(id)sender {
// NSLog(@"[CoolUnitySceneVC] Go to the last scene");
NSString *text = ((UITextField *)[self.view viewWithTag:101]).text;
if ([text isEqualToString:@""]) {
text = @"还没有给我一个名字哦!";
}
[MyViewInit setEnabled:text];
}
- (void)show:(id)sender{
[MyViewInit setEnabled:@"show"];
}
- (void)recevied:(id)sender{
GoodByeViewController *goodByeVC = [[GoodByeViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:goodByeVC animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
GetAppController().unityView,获取Unity中的视图,把这个视图添加到CoolUnitySceneViewController.h视图上,并设置该视图的框架
- 创建GoodByeViewController.h文件,代码如下图:
//
// GoodByeViewController.m
// Unity-iPhone
//
// Created by wencun on 4/3/15.
//
//
#import "GoodByeViewController.h"
#import "CoolUnitySceneviewController.h"
@interface GoodByeViewController ()
@end
@implementation GoodByeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor grayColor];
UILabel *lblTheEnd = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 300, 44)];
lblTheEnd.text = @"我是第二个IOS界面,欢迎回来!";
// lblTheEnd.center = CGPointMake(self.view.bounds.size.height / 2, self.view.bounds.size.width / 2);
[self.view addSubview:lblTheEnd];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
做一个UILabel,显示是IOS界面
iOS调用Unity方法的限制:
- iOS无法接受到Unity方法中的返回值,因为UnitySendMessage是void的类型
- iOS调用Unity方法只能传一个参数,若有多个参数,要拼接成字符串
Unity调用iOS方法:
- 调用方法的参数可以有任意个
- 可以接受到iOS的返回值