所需工具:
- 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的返回值