一.配置相關(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)限