iOS中集成unity

- 说明

0.png

- 导入unity模块

  1. unity中导出的iOS工程是这样子的:
1.png
  1. 将Classes文件夹、Data文件夹、Libraries、MapFileParser、MapFileParser.sh这五个复制到自己的iOS工程中
2.png
  1. 将Data、QCAR(QCAR文件在Data->Raw里面)文件夹以以下方式添加
3.png
  1. 将Classes、Libraries以以下方式添加
4.png
  1. 添加引用库文件
    打开从unity中导出的iOS工程,查看其引用的库文件,如图所示
5.png

将上面的库文件,添加到自己的工程中

  1. 关闭bitcode
    在build settings中关闭bitcode
  2. 在 other Linker Flags 添加
    -weak_framework CoreMotion -weak-lSystem


    14.png
  3. 在Other C Flags中添加 -DINIT_SCRIPTING_BACKEND=1,如图所示
6.png
  1. 添加pch文件
7.png

如果项目中有多个pch文件,请将其合并成一个pch文件,再添加

  1. 在Header Search Paths 添加头文件引用
    打开从unity导出的工程,查看Header Search Paths中添加的头文件,如图所示
8.png

将其添加到自己工程的Header Search Paths中

  1. 在Library Search Path 中添加库引用
    打开从unity导出的工程,查看ibrary Search Path中添加的头文件,如图所示
9.png

将其添加到自己工程的ibrary Search Path中

  1. 在user-Defined 添加
    打开从unity导出的工程, 在build settings中查看user-Defined,如图所示
10.png

并将其添加到自己项目的 user-Defined中

11.png
  1. 添加Run Script
    打开从unity导出的工程, 在build phases中查看Run Script,如图所示:
12.png
  1. 更改main.m文件为main.mm文件。将Classes中的main.mm中的内容复制,粘贴到原来工程的main.mm中。然后删除Classes中的main文件。
 
#include "RegisterMonoModules.h"
#include "RegisterFeatures.h"
#import "AppDelegate.h"
#include <mach/mach_time.h>
#include <csignal>

// Hack to work around iOS SDK 4.3 linker problem
// we need at least one __TEXT, __const section entry in main application .o files
// to get this section emitted at right time and so avoid LC_ENCRYPTION_INFO size miscalculation
static const int constsection = 0;

void UnityInitTrampoline();

// WARNING: this MUST be c decl (NSString ctor will be called after +load, so we cant really change its  value)
const char* AppControllerClassName = "UnityAppController";

int main(int argc, char* argv[])
{
  signed long long startTime = mach_absolute_time();
  @autoreleasepool
  {
      UnitySetStartupTime(startTime);
      UnityInitTrampoline();
      UnityInitRuntime(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);
      
      //UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String:AppControllerClassName]);
      UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
  }
  
  return 0;
}

#if TARGET_IPHONE_SIMULATOR && TARGET_TVOS_SIMULATOR

#include <pthread.h>

extern "C" int pthread_cond_init$UNIX2003(pthread_cond_t *cond, const pthread_condattr_t *attr)
{ return pthread_cond_init(cond, attr); }
extern "C" int pthread_cond_destroy$UNIX2003(pthread_cond_t *cond)
{ return pthread_cond_destroy(cond); }
extern "C" int pthread_cond_wait$UNIX2003(pthread_cond_t *cond, pthread_mutex_t *mutex)
{ return pthread_cond_wait(cond, mutex); }
extern "C" int pthread_cond_timedwait$UNIX2003(pthread_cond_t *cond, pthread_mutex_t *mutex,
                                             const struct timespec *abstime)
{ return pthread_cond_timedwait(cond, mutex, abstime); }

#endif // TARGET_IPHONE_SIMULATOR && TARGET_TVOS_SIMULATOR

并将main函数中的 UIApplicationMain方法修改为UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

  1. 在Classes的UnityAppController.mm中的- (void)applicationDidBecomeActive:(UIApplication*)application方法中,注释掉[self performSelector: @selector(startUnity:) withObject: application afterDelay: 0]方法,这样做的目的,是为防止unity的内容随着应用的启动,而启动;
- (void)applicationDidBecomeActive:(UIApplication*)application
{
   ::printf("-> applicationDidBecomeActive()\n");

   [self removeSnapshotView];

   if (_unityAppReady)
   {
       if (UnityIsPaused() && _wasPausedExternal == false)
       {
           UnityWillResume();
           UnityPause(0);
       }
       UnitySetPlayerFocus(1);
   }
   else if (!_startUnityScheduled)
   {
       _startUnityScheduled = true;
      // [self performSelector: @selector(startUnity:) withObject: application afterDelay: 0];
   }

   _didResignActive = false;
}
  1. 修改appDelegate.h
#import <UIKit/UIKit.h>
@class UnityAppController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property(strong,nonatomic)UINavigationController *navi;
@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic)UnityAppController* unityAppController;
- (void)shouldAttachRenderDelegate;
@end
  1. 将appDelegate.m修改为appDelegate.mm,并对其内容进行修改
   #import "AppDelegate.h"
#import "ViewController.h"
#import "UnityAppController.h"
@interface AppDelegate ()

@end

extern "C" void VuforiaSetGraphicsDevice(void* device, int deviceType, int eventType);
extern "C" void VuforiaRenderEvent(int marker);

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Override point for customization after application launch.
  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  BOOL returnBool;
  if (_unityAppController == nil) {
      
      _unityAppController = [[UnityAppController alloc] init];
  }
  [_unityAppController application:application didFinishLaunchingWithOptions:launchOptions];
  
  ViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"viewVC"];
  self.navi = [[UINavigationController alloc] initWithRootViewController:vc];
  self.window.rootViewController=self.navi;
  [self.window makeKeyAndVisible];
  return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
  [_unityAppController applicationWillResignActive:application];
  // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
  // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
  
  [_unityAppController applicationDidEnterBackground:application];
  // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
  // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
  [_unityAppController applicationWillEnterForeground:application];
  // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
  [_unityAppController applicationDidBecomeActive:application];
  // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
  [_unityAppController applicationWillTerminate:application];
  // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
- (void)shouldAttachRenderDelegate
{
  UnityRegisterRenderingPlugin(&VuforiaSetGraphicsDevice, &VuforiaRenderEvent);
}
@end
  1. 在Classes的UnityAppController.h中,更改GetAppController()方法(当然要先引入#import "AppDelegate.h"
inline UnityAppController*  GetAppController()
{
  AppDelegate *dele = (AppDelegate *)[UIApplication sharedApplication].delegate;
  return (UnityAppController *)dele.unityAppController;
}
  1. 在UnityAppController.mm中重写- (void)shouldAttachRenderDelegate方法
- (void)shouldAttachRenderDelegate  {
 AppDelegate *deleg = [UIApplication sharedApplication].delegate;
 [deleg shouldAttachRenderDelegate];
}
  1. ok,通过上面的步骤,我们就已经将unity的功能导入自己的项目中,接下来做的,就是点击按钮,启动unity模块

  2. Main.storyBoard的ViewController创建一个UIButton,并将Storyboard ID设为“viewVC”

13.png
  1. 在ViewController.m实现其点击事件(记得倒入#import "UnityAppController.h",#import "AppDelegate.h")
 #pragma mark----打开AR-----
- (IBAction)doOpenAR:(id)sender {
   
   static bool flg=NO;
   if (flg) {//如果不是第一次启动,则之间切换window
       [GetAppController() restartUnity]; 
   }
   else//如果是第一次点击按钮,则需要启动unity
   {
       [GetAppController() preStartUnity];
       [GetAppController() startUnity:[UIApplication sharedApplication]];

[UnityGetMainWindow() makeKeyAndVisible];
       flg=YES;
   }
   
}
  1. 在UnityAppController.h中申明-(void) restartUnity,在UnityAppController.mm中实现
-(void) restartUnity
{
 _window.rootViewController=_rootController;
 [_window makeKeyAndVisible];
 [UnityGetMainWindow() makeKeyAndVisible];
 if (_didResignActive) {
     UnityPause(false);
     
     _didResignActive=NO;
 }
}
  1. 这样,我们就能启动unity的模块了,但是,关闭或隐藏unity模块,也需要我们操作。
  2. 我们需要在UnityAppController.mm中的- (void)startUnity:(UIApplication*)application方法里,添加一个按钮
- (void)startUnity:(UIApplication*)application
{
 NSAssert(_unityAppReady == NO, @"[UnityAppController startUnity:] called after Unity has been initialized");

 UnityInitApplicationGraphics(UNITY_FORCE_DIRECT_RENDERING);

 // we make sure that first level gets correct display list and orientation
 [[DisplayManager Instance] updateDisplayListInUnity];

 UnityLoadApplication();
 Profiler_InitProfiler();

 [self showGameUI];
 
 
 UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
 [button addTarget:self action:@selector(doHideenUnity) forControlEvents:UIControlEventTouchUpInside];
 button.frame=CGRectMake(0, 0, 50, 50) ;
 [button setTitle:@"退出" forState:UIControlStateNormal];
 //[button setBackgroundColor:[UIColor purpleColor]];
 [_rootView addSubview:button];
 
 [self createDisplayLink];

 UnitySetPlayerFocus(1);
}
  1. 并实现按钮的点击事件
-(void)doHideenUnity
{
 UnityPause(true);
 _didResignActive=YES;
 Profiler_UninitProfiler();
 AppDelegate *delet=[UIApplication sharedApplication].delegate;
     
     [delet.window makeKeyAndVisible];
 
}
  1. ok,完成

可能遇到了问题

  • 重复的main.mm,记得删除Classes文件的main.mm
  • 如果有多个pch文件,记得进行合并
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容