如何将Unity集成到iOS工程中去
一.配置相关
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 设置为Code/Classes/Prefix.pch;
5.修改Header Search Paths 和 Library Search Paths,修改如下:
6.设置Other Linker Flags,如图所示:
7.Other C Flags 和 Other C++ Flags 设置为 -DINIT_SCRIPTING_BACKEND=1;
8.Enable Bitcode 设置为NO;
二.代码修改:
1.删除原生工程中的main函数,找到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
将以下代码复制进去
#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中添加打开相机权限