如何將Unity集成到iOS工程中去

一.配置相關(guān)

1.首先將資源文件和代碼拖入工程屏轰;(注意:代碼(Code)拖到工程時(shí)選擇Create groups,拖資源文件(Data、QCAR吞杭、Vuforia)時(shí)選擇Create folder references)呐籽;

2.添加framework锋勺,如下圖所示:

3.添加Run Script,輸入"$PROJECT_DIR/Code/MapFileParser.sh"

rm -rf"$TARGET_BUILD_DIR/$PRODUCT_NAME.app/Data/Raw/QCAR"

4.修改Prefix Header 設(shè)置為Code/Classes/Prefix.pch狡蝶;

5.修改Header Search Paths 和 Library Search Paths庶橱,修改如下:

6.設(shè)置Other Linker Flags,如圖所示:

7.Other C Flags 和 Other C++ Flags 設(shè)置為?-DINIT_SCRIPTING_BACKEND=1;

8.Enable Bitcode 設(shè)置為NO贪惹;

二.代碼修改:

1.刪除原生工程中的main函數(shù)苏章,找到Code/Classes/main.mm,修改為以下:

#import

#import"AppDelegate.h"

#include"RegisterMonoModules.h"

#include"RegisterFeatures.h"

#include

staticconstintconstsection =0;

voidUnityInitTrampoline();

// WARNING: this MUST be c decl (NSString ctor will be called after +load, so we cant really change its value)

constchar* AppControllerClassName ="AppDelegate";

intmain(intargc,char* argv[])

{

@autoreleasepool

{

UnityInitTrampoline();

UnityParseCommandLine(argc, argv);

RegisterMonoModules();

NSLog(@"-> registered mono modules %p\n", &constsection);

RegisterFeatures();

// iOS terminates open sockets when an application enters background mode.

// The next write to any of such socket causes SIGPIPE signal being raised,

// even if the request has been done from scripting side. This disables the

// signal and allows Mono to throw a proper C# exception.

std::signal(SIGPIPE, SIG_IGN);

// return? UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String:AppControllerClassName]);

returnUIApplicationMain(argc, argv,nil,NSStringFromClass([AppDelegateclass]));

}

//return 0;

}

#if TARGET_IPHONE_SIMULATOR && TARGET_TVOS_SIMULATOR

#include

extern"C"intpthread_cond_init$UNIX2003(pthread_cond_t *cond,constpthread_condattr_t *attr)

{returnpthread_cond_init(cond, attr); }

extern"C"intpthread_cond_destroy$UNIX2003(pthread_cond_t *cond)

{returnpthread_cond_destroy(cond); }

extern"C"intpthread_cond_wait$UNIX2003(pthread_cond_t *cond, pthread_mutex_t *mutex)

{returnpthread_cond_wait(cond, mutex); }

extern"C"intpthread_cond_timedwait$UNIX2003(pthread_cond_t *cond, pthread_mutex_t *mutex,

conststructtimespec *abstime)

{returnpthread_cond_timedwait(cond, mutex, abstime); }

#endif// TARGET_IPHONE_SIMULATOR && TARGET_TVOS_SIMULATOR

2.修改AppDelegate.h

#import

#import"UnityAppController.h"

@interfaceAppDelegate :UIResponder

@property(strong,nonatomic)UIWindow*window;

@property(strong,nonatomic)UIWindow*unityWindow;

@property(strong,nonatomic)UnityAppController*unityController;

- (void)showUnityWindow;

- (void)hideUnityWindow;

- (void)shouldAttachRenderDelegate;

@end

3.將APPDelegate.m修改為AppDelegate.mm

將以下代碼復(fù)制進(jìn)去

#import"AppDelegate.h"

#import"ViewController.h"

@interfaceAppDelegate()

@end

extern"C"voidVuforiaSetGraphicsDevice(void* device,intdeviceType,inteventType);

extern"C"voidVuforiaRenderEvent(intmarker);

@implementationAppDelegate

- (void)shouldAttachRenderDelegate {

UnityRegisterRenderingPlugin(&VuforiaSetGraphicsDevice, &VuforiaRenderEvent);

}

- (UIWindow*)unityWindow {

returnUnityGetMainWindow();

}

- (void)showUnityWindow {

[UIApplicationsharedApplication].statusBarHidden=YES;

[self.unityWindowmakeKeyAndVisible];

UnityPause(NO);

}

- (void)hideUnityWindow {

[UIApplicationsharedApplication].statusBarHidden=NO;

[[UIApplicationsharedApplication]setStatusBarStyle:UIStatusBarStyleLightContentanimated:YES];

[self.windowmakeKeyAndVisible];

UnityPause(NO);

}

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

self.window= [[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];

self.window.backgroundColor= [UIColorwhiteColor];

ViewController*vc = [[ViewControlleralloc]init];

UINavigationController* navc = [[UINavigationControlleralloc]initWithRootViewController:vc];

self.window.rootViewController= navc;

self.unityController= [[UnityAppControlleralloc]init];

[self.unityControllerapplication:applicationdidFinishLaunchingWithOptions:launchOptions];

[self.windowmakeKeyAndVisible];

returnYES;

}

- (void)applicationWillResignActive:(UIApplication*)application {

[self.unityControllerapplicationWillResignActive:application];

}

- (void)applicationDidEnterBackground:(UIApplication*)application {

[self.unityControllerapplicationDidEnterBackground:application];

}

- (void)applicationWillEnterForeground:(UIApplication*)application {

[self.unityControllerapplicationWillEnterForeground:application];

}

- (void)applicationDidBecomeActive:(UIApplication*)application {

[self.unityControllerapplicationDidBecomeActive:application];

}

- (void)applicationWillTerminate:(UIApplication*)application {

[self.unityControllerapplicationWillTerminate:application];

}

@end

4.iOS原生工程可以通過下面方式打開unity界面奏瞬。

- (void)showUnity{

[(AppDelegate*)[UIApplicationsharedApplication].delegateshowUnityWindow];

}

5.修改Code/Classes/UnityAppController.h

找到第83行 改為inlineUnityAppController* GetAppController()

{

return(UnityAppController*)[[UIApplication sharedApplication] valueForKeyPath:@"delegate.unityController"];

}

6.修改 Code/Classes/UnityAppController.m

添加頭文件 AppDelegate.h

找過110行 改為

- (void)shouldAttachRenderDelegate {

AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

[delegate shouldAttachRenderDelegate];

}

7.在info.plist中添加打開相機(jī)權(quán)限

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末枫绅,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子硼端,更是在濱河造成了極大的恐慌并淋,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,496評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件珍昨,死亡現(xiàn)場離奇詭異县耽,居然都是意外死亡句喷,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門兔毙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來唾琼,“玉大人,你說我怎么就攤上這事瞒御「感穑” “怎么了?”我有些...
    開封第一講書人閱讀 162,632評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵肴裙,是天一觀的道長趾唱。 經(jīng)常有香客問我,道長蜻懦,這世上最難降的妖魔是什么甜癞? 我笑而不...
    開封第一講書人閱讀 58,180評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮宛乃,結(jié)果婚禮上悠咱,老公的妹妹穿的比我還像新娘。我一直安慰自己征炼,他們只是感情好析既,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,198評(píng)論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著谆奥,像睡著了一般眼坏。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上酸些,一...
    開封第一講書人閱讀 51,165評(píng)論 1 299
  • 那天宰译,我揣著相機(jī)與錄音,去河邊找鬼魄懂。 笑死沿侈,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的市栗。 我是一名探鬼主播缀拭,決...
    沈念sama閱讀 40,052評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼填帽!你這毒婦竟也來了蛛淋?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,910評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤盲赊,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后敷扫,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體哀蘑,經(jīng)...
    沈念sama閱讀 45,324評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡诚卸,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,542評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了绘迁。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片合溺。...
    茶點(diǎn)故事閱讀 39,711評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖缀台,靈堂內(nèi)的尸體忽然破棺而出棠赛,到底是詐尸還是另有隱情,我是刑警寧澤膛腐,帶...
    沈念sama閱讀 35,424評(píng)論 5 343
  • 正文 年R本政府宣布睛约,位于F島的核電站,受9級(jí)特大地震影響哲身,放射性物質(zhì)發(fā)生泄漏辩涝。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,017評(píng)論 3 326
  • 文/蒙蒙 一勘天、第九天 我趴在偏房一處隱蔽的房頂上張望怔揩。 院中可真熱鬧,春花似錦脯丝、人聲如沸商膊。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽晕拆。三九已至,卻和暖如春砰苍,著一層夾襖步出監(jiān)牢的瞬間潦匈,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評(píng)論 1 269
  • 我被黑心中介騙來泰國打工赚导, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留茬缩,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,722評(píng)論 2 368
  • 正文 我出身青樓吼旧,卻偏偏與公主長得像凰锡,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子圈暗,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,611評(píng)論 2 353

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