Unity3D和iOS的對(duì)接及界面跳轉(zhuǎn)


界面1
界面2
界面3
界面4

所需工具:
  1. Unity3d 5.0版本
  2. 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ì)有源代碼。如下圖:
創(chuàng)建unity
  • 在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:

unityAppController.h
  • 在Assets文件夾中丙笋,創(chuàng)建一個(gè)C# javascript文件谢澈,代碼如下:
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文件笙各,代碼如下圖:
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)右蹦。

cube組件
  • 我們開(kāi)始生成IOS項(xiàng)目,unity->file->build & settings,選中iOS鲫懒,點(diǎn)擊player settings,這里就不詳細(xì)敘述了


步驟2. 配置iOS項(xiàng)目
  • 生成iOS項(xiàng)目目錄如下:
生成的iOS目錄

我們?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方法的限制:
  1. iOS無(wú)法接受到Unity方法中的返回值,因?yàn)閁nitySendMessage是void的類(lèi)型
  2. iOS調(diào)用Unity方法只能傳一個(gè)參數(shù)厢汹,若有多個(gè)參數(shù)螟深,要拼接成字符串
Unity調(diào)用iOS方法:
  1. 調(diào)用方法的參數(shù)可以有任意個(gè)
  2. 可以接受到iOS的返回值
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市烫葬,隨后出現(xiàn)的幾起案子界弧,更是在濱河造成了極大的恐慌,老刑警劉巖搭综,帶你破解...
    沈念sama閱讀 217,277評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件垢箕,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡兑巾,警方通過(guò)查閱死者的電腦和手機(jī)条获,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)蒋歌,“玉大人帅掘,你說(shuō)我怎么就攤上這事√糜停” “怎么了修档?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,624評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)府框。 經(jīng)常有香客問(wèn)我吱窝,道長(zhǎng),這世上最難降的妖魔是什么迫靖? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,356評(píng)論 1 293
  • 正文 為了忘掉前任院峡,我火速辦了婚禮,結(jié)果婚禮上袜香,老公的妹妹穿的比我還像新娘撕予。我一直安慰自己,他們只是感情好蜈首,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布实抡。 她就那樣靜靜地躺著欠母,像睡著了一般。 火紅的嫁衣襯著肌膚如雪吆寨。 梳的紋絲不亂的頭發(fā)上赏淌,一...
    開(kāi)封第一講書(shū)人閱讀 51,292評(píng)論 1 301
  • 那天,我揣著相機(jī)與錄音啄清,去河邊找鬼六水。 笑死,一個(gè)胖子當(dāng)著我的面吹牛辣卒,可吹牛的內(nèi)容都是我干的掷贾。 我是一名探鬼主播,決...
    沈念sama閱讀 40,135評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼荣茫,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼想帅!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起啡莉,我...
    開(kāi)封第一講書(shū)人閱讀 38,992評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤港准,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后咧欣,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體浅缸,經(jīng)...
    沈念sama閱讀 45,429評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評(píng)論 3 334
  • 正文 我和宋清朗相戀三年魄咕,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了衩椒。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,785評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蚕礼,死狀恐怖烟具,靈堂內(nèi)的尸體忽然破棺而出梢什,到底是詐尸還是另有隱情奠蹬,我是刑警寧澤,帶...
    沈念sama閱讀 35,492評(píng)論 5 345
  • 正文 年R本政府宣布嗡午,位于F島的核電站囤躁,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏荔睹。R本人自食惡果不足惜狸演,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望僻他。 院中可真熱鬧宵距,春花似錦、人聲如沸吨拗。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,723評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至哨鸭,卻和暖如春民宿,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背像鸡。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,858評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工活鹰, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人志群。 一個(gè)月前我還...
    沈念sama閱讀 47,891評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像赖舟,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子宾抓,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容