所需工具:
- Unity3d 5.0版本
- Xcode 6.2版本 模擬器的操作系統(tǒng)8.2v
步驟1. 創(chuàng)建一個(gè)unity項(xiàng)目
- 創(chuàng)建一個(gè)Unity項(xiàng)目雨膨,并同時(shí)創(chuàng)建一個(gè)Cube,一個(gè)Text(作為cube的名字)和一個(gè)button按鈕。如何創(chuàng)建這里就不在敘述,稍后附件中會(huì)有源代碼。如下圖:
- 在Unity的Project欄目的Assests中增加一個(gè).mm文件,文件名自定義,我這里是TNAppController.mm,用xcode編寫(xiě)在拖到這里即可,內(nèi)容為:
#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)
說(shuō)明:
a. 導(dǎo)入的部分都為Untiy生成xcode項(xiàng)目中自帶的.h文件据过,以后再導(dǎo)入也可以,HelloViewController.h文件為我們以后要?jiǎng)?chuàng)建的文件妒挎。
b. 這個(gè)文件繼承UnityAppController.h這個(gè)文件蝶俱,這點(diǎn)很重要,UnityAppController中提供了很多我們可以直接調(diào)用的屬性饥漫,以便我們自己寫(xiě)的iOS文件可以使用window榨呆,rootController等屬性,這些屬性要和Untiy導(dǎo)出的iOS項(xiàng)目使用的是同一個(gè)庸队。
c. willStartWithViewController這個(gè)方法好像是只有Unity 5左右的版本才用的是這個(gè)方法积蜻,4.x以前的版本使用的不是這個(gè)方法。這個(gè)方法是將要打開(kāi)視圖控制器的初始化方法彻消。初始化根視圖控制器和根視圖竿拆。
d. IMPL_APP_CONTROLLER_SUBCLASS 這個(gè)很重要,它決定了我們的TNAppController.h 這個(gè)文件將被重載宾尚。UnityAppController.h 文件中有解釋?zhuān)缦聢D:
- 在Assets文件夾中丙笋,創(chuàng)建一個(gè)C# javascript文件谢澈,代碼如下:
說(shuō)明:
a. [DllImport ("__Internal")] 引入一個(gè)dll的內(nèi)部文件.
b. 聲明一個(gè)可調(diào)用的接口, private static extern int ActivateUI_iOS(int index);該方法可以有任意多個(gè)參數(shù)御板,這個(gè)參數(shù)的使用锥忿,到時(shí)候我們?cè)趇OS模擬器上可以看到,并且可以接收到iOS的返回值。
c. Start方法初始化Button怠肋,將button.cs文件與Hierarchy中的創(chuàng)建的Button組件進(jìn)行關(guān)聯(lián)敬鬓。
- 在Assets文件夾中,創(chuàng)建一個(gè)test.js文件笙各,代碼如下圖:
說(shuō)明:
a. SetText(Input)方法钉答,我們將在ios項(xiàng)目中調(diào)用這個(gè)方法,動(dòng)態(tài)修改cube的名字杈抢。returnVal()方法是沒(méi)有用的数尿。
b. 我們將test.js文件和Hierarchy中的cube組件關(guān)聯(lián),在將text組件和cube關(guān)聯(lián)惶楼,text組件作為cube組件的名稱(chēng)右蹦。
- 我們開(kāi)始生成IOS項(xiàng)目,unity->file->build & settings,選中iOS鲫懒,點(diǎn)擊player settings,這里就不詳細(xì)敘述了
步驟2. 配置iOS項(xiàng)目
- 生成iOS項(xiàng)目目錄如下:
我們?cè)趗ntiy中創(chuàng)建的TNAppController.mm文件會(huì)出現(xiàn)在Libraries包中嫩实,創(chuàng)建的test.js刽辙,和button.cs文件會(huì)編譯成dll.s文件中去
我們?cè)赾lasses文件夾中創(chuàng)建如下文件窥岩, MyViewInit.h和MyViewInit.mm文件,mm結(jié)尾的宰缤,是c++識(shí)別的文件颂翼,其中有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調(diào)用了IOS方法" message:[NSString stringWithFormat:@"我是第%i次從Unity過(guò)來(lái)的!",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調(diào)用Unity中的方法朦乏,第一個(gè)參數(shù)為組件名稱(chēng),第二個(gè)參數(shù)為方法名氧骤,第三個(gè)參數(shù)為方法的參數(shù)呻疹,該方法的返回值為void,所有不能接受調(diào)用Unity方法中的返回值
pragma下方的代碼為c++代碼筹陵,為ActivateUI_iOS的實(shí)現(xiàn)刽锤,這樣,點(diǎn)擊Unity中的按鈕就可以調(diào)用這個(gè)方法了朦佩,該方法有一個(gè)返回值并思,Unity中可以接收到
- 創(chuàng)建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:@"這是一個(gè)IOS界面语稠,點(diǎn)我進(jìn)入U(xiǎn)nity界面宋彼。" 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
- 創(chuàng)建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 = @"歡迎進(jìn)入U(xiǎn)nity界面输涕!(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:@"請(qǐng)鍵入文字..."];
tv.tag = 101;
[self.view addSubview:tv];
UIButton *btnNext = [UIButton buttonWithType:UIButtonTypeSystem];
[btnNext setTitle:@"點(diǎn)我可以修改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:@"點(diǎn)我可以進(jìn)入到另外一個(gè)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 = @"還沒(méi)有給我一個(gè)名字哦!";
}
[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中的視圖占贫,把這個(gè)視圖添加到CoolUnitySceneViewController.h視圖上桃熄,并設(shè)置該視圖的框架
- 創(chuàng)建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 = @"我是第二個(gè)IOS界面型奥,歡迎回來(lái)瞳收!";
// lblTheEnd.center = CGPointMake(self.view.bounds.size.height / 2, self.view.bounds.size.width / 2);
[self.view addSubview:lblTheEnd];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
做一個(gè)UILabel,顯示是IOS界面
iOS調(diào)用Unity方法的限制:
- iOS無(wú)法接受到Unity方法中的返回值,因?yàn)閁nitySendMessage是void的類(lèi)型
- iOS調(diào)用Unity方法只能傳一個(gè)參數(shù)厢汹,若有多個(gè)參數(shù)螟深,要拼接成字符串
Unity調(diào)用iOS方法:
- 調(diào)用方法的參數(shù)可以有任意個(gè)
- 可以接受到iOS的返回值