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

一.配置相關(guān)

1.首先將資源文件和代碼拖入工程;(注意:代碼(Code)拖到工程時選擇Create groups,拖資源文件(Data、QCAR、Vuforia)時選擇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ù)制進去

#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中添加打開相機權(quán)限

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容