一直想抽空整理一下unity原生工程导入iOS原生工程中的详细步骤。做iOS+vuforia+unity开发这么长时间了。从最初的小小白到现在的小白。中间趟过了好多的坑。也有一些的小小收货。做一个喜欢总结的人。好了废话不多说了。上干货。本人用的是unity 5.4.0f3 + Xcode 7.2。个人认为这个版本的影响不是很大的。以下的导入资源过程大家根据实际需要去选择是否拷贝资源到原生工程中。(copy if need)

一、unity导出工程一开始是这样子的:

二、unity导入原生工程的准备工作。

1.将一些必要的文件导入iOS Native工程中去

1>将Classes 和 Libraries 文件夹以 下面的方式导入自己的工程中

2>将Data 文件夹以下面的方式导入工程中

3>删除Native文件夹下的所有.h文件的引用。并将cpp文件导入工程引入。

清除native中所有的.h引用。为了更快的删除定位到这些.h文件。首先选定Native文件夹

然后进行这样的操作,注意一定要选择Remove Reference。千万不要选择Move to Trash(下面截图只是为了演示一下删除框。类名忽略不要对号删除,并不是Native中的头文件)然后再将Native中的cpp文件拖入到工程。勾选1、2、4方框。

4>.然后对应删除Libraries-->libil2cpp文件夹。删除方法同Native中的.h文件的移除。

三、添加引用库文件

其中因为我用了pod的缘故。libPods库可以忽略。

四、关闭bitcode

在build settings中bitcode关闭。

五、在 other Linker Flags 添加

-weak_framework CoreMotion -weak-lSystem

六、在Header Search Path 添加下面这些头文件引用~

SRCROOT/../../iOS/unity2iOSSRCROOT/../../iOS/unity2iOS{SRCROOT}/../../iOS/unity2iOS/Classes
SRCROOT/../../iOS/unity2iOS/Classes/NativeSRCROOT/../../iOS/unity2iOS/Classes/Native{SRCROOT}/../../iOS/unity2iOS/Libraries
SRCROOT/../../iOS/unity2iOS/Libraries/libil2cpp/includeSRCROOT/../../iOS/unity2iOS/Libraries/libil2cpp/include{SRCROOT}/../../iOS/unity2iOS/Libraries/Plugins/iOS

七、在Library Search Path 中添加

SRCROOT/../../iOS/unity2iOSSRCROOT/../../iOS/unity2iOS{SRCROOT}/../../iOS/unity2iOS/Libraries
${SRCROOT}/../../iOS/unity2iOS/Libraries/Plugins/iOS

八、在other C Flags 中添加 -DINIT_SCRIPTING_BACKEND=1 同是在 other C++ Flags中出现

改为C99

改PCH(将两个pch文件合并)

跟着下面的图片做设置更改

在user-Defined 添加如下

GCC_THUMB_SUPPORT NO
GCC_USE_INDIRECT_FUNCTION_CALLS NO
UNITY_RUNTIME_VERSION 5.4.0f3
UNITY_SCRIPTING_BACKEND il2cpp

以上都要根据自己工程的具体情况添加。大家切记。

九、添加配置脚本RUN SCRIPT(注意按照自己工程中的脚本位置配置)

十、更改main.m文件为main.mm文件。将Classes中的main.mm中的内容合并到原来工程的main.mm工程中。然后删除Classes中的main文件。

//
//  main.m
////
//  Created by Aaron on 16/11/17.
//  Copyright © 2016年 Aaron. All rights reserved.
//

#import  <UIKit/UIKit.h>
#import  "AppDelegate.h"
#include "RegisterMonoModules.h"
#include "RegisterFeatures.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)
/***  VuforiaNativeRendererController*/
const char* AppControllerClassName = "AppDelegate";int main(int argc, char* argv[])
{@autoreleasepool{UnityInitTrampoline();UnityParseCommandLine(argc, argv);RegisterMonoModules();NSLog(@"-> registered mono modules %p\n", &constsection);RegisterFeatures();std::signal(SIGPIPE, SIG_IGN);NSString *ss = NSStringFromClass([AppDelegate class]);UIApplicationMain(argc, argv, nil,ss);}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

十一、在pch文件中添加 #import "UnityAppController.h"。然后更改AppDelegate

//
//  AppDelegate.h
//  //
//  Created by Aaron on 16/11/17.
//  Copyright © 2016年 Aaron. All rights reserved.
//#import <UIKit/UIKit.h>
@class UnityAppController;@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;
@property (nonatomic , strong)UIWindow *unityWindow;
@property (nonatomic , strong)UnityAppController *unityVC;- (void)showUnityWindow;
- (void)hideUnityWindow;
- (void)shouldAttachRenderDelegate;@end

在AppDelegate.mm(因为有混编,本来变一个main.mm文件就可以了。这里也变过来双保险)

首先添加:

//
//  AppDelegate.m
//  //
//  Created by Aaron on 16/11/17.
//  Copyright © 2016年 Aaron. All rights reserved.
//#import "AppDelegate.h"
#import "HomeViewController.h"
#import "UnityAppController.h"
#import "VuforiaRenderDelegate.h"extern "C" void VuforiaSetGraphicsDevice(void* device, int deviceType, int eventType);
extern "C" void VuforiaRenderEvent(int marker);@interface AppDelegate ()
{UIButton *_backBtn;
}@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];// Override point for customization after application launch.self.window.backgroundColor = [UIColor whiteColor];[application setStatusBarStyle:UIStatusBarStyleLightContent];//unityself.unityVC = [[UnityAppController alloc] init];[self.unityVC application:application didFinishLaunchingWithOptions:launchOptions];HomeViewController *homeVC = [[HomeViewController alloc] init];BaseNavViewController *mainNav = [[BaseNavViewController alloc] initWithRootViewController:homeVC];self.window.rootViewController = mainNav;[self.window makeKeyAndVisible];return YES;
}- (void)applicationWillResignActive:(UIApplication *)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 throttle down OpenGL ES frame rates. Games should use this method to pause the game.[self.unityVC applicationWillResignActive:application];
}- (void)applicationDidEnterBackground:(UIApplication *)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.[self.unityVC applicationDidEnterBackground:application];
}- (void)applicationWillEnterForeground:(UIApplication *)application
{// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.[self.unityVC applicationWillEnterForeground:application];
}- (void)applicationDidBecomeActive:(UIApplication *)application
{[self.unityVC applicationDidBecomeActive:application];
}- (void)applicationWillTerminate:(UIApplication *)application
{// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.[self.unityVC applicationWillTerminate:application];
}#pragma mark -
#pragma mark ---------------unity开启与隐藏
- (UIWindow *)unityWindow
{if (!_unityWindow) {_unityWindow = UnityGetMainWindow();}return _unityWindow;
}
- (void)showUnityWindow
{[self.unityWindow makeKeyAndVisible];_backBtn = [UIButton buttonWithType:UIButtonTypeCustom];[_backBtn setImage:[UIImage imageNamed:@"返回icon"] forState:UIControlStateNormal];[self.unityWindow addSubview:_backBtn];[_backBtn addTarget:self action:@selector(hideUnityWindow) forControlEvents:UIControlEventTouchUpInside];[_backBtn mas_makeConstraints:^(MASConstraintMaker *make){make.top.equalTo(_unityWindow).offset(30);make.left.equalTo(_unityWindow).offset(15);make.width.mas_equalTo(50);make.height.mas_equalTo(50);}];
}
- (void)hideUnityWindow
{[self.window makeKeyAndVisible];
}
- (void)shouldAttachRenderDelegate
{UnityRegisterRenderingPlugin(&VuforiaSetGraphicsDevice, &VuforiaRenderEvent);
}@end

十二、在unityappcontroller中需要做的配置就是,首先在unityAppController.h中更改GetAppController()方法(当然要先引入#import "AppDelegate.h")

NS_INLINE UnityAppController*    GetAppController()
{AppDelegate *dele = (AppDelegate *)[UIApplication sharedApplication].delegate;return (UnityAppController *)dele.unityVC;
}

在.mm文件中重写shouldAttachRenderDelegate

方法

- (void)shouldAttachRenderDelegate
{AppDelegate *deleg = [UIApplication sharedApplication].delegate;[deleg shouldAttachRenderDelegate];
}

至此,导入工作已经做完了。愉快地去玩耍吧。

转载于:https://www.cnblogs.com/fuunnyy/p/6227740.html

unity导出工程导入到iOS原生工程中详细步骤相关推荐

  1. android pod 组件化_Flutter组件化导入至iOS现有工程中(CocoaPods篇)

    Flutter项目实战:方案有三 纯Flutter项目,需要投入大量人力进入到Flutter编程中,且现有原生项目需要完全摒弃 Flutter项目混编,暂且不说Flutter所支持的框架,与原生交互的 ...

  2. ios html5上架,iOS原生集成H5+详细流程

    iOS原生集成H5+ 集成方式 独立应用方式集成 Widget方式集成 WebView方式集成 可以打开官方链接: 选择 5+SDK -> 5+SDK集成 -> 平台 下查看集成方式 独立 ...

  3. flutter不支持热更新_在iOS原生项目中使用Flutter,热更新

    前言: Flutter 因其自建的渲染引擎,背靠谷歌的支持,近来俘获了不少的开发小伙伴,越来越多的开发者尝试使用Flutter进行开发,在原生项目中嵌入Flutter来完成复杂度不高的页面成为了一个不 ...

  4. iOS原生WebView中JavaScript和OC交互

    在iOS开发中很多时候我们会和UIWebView打交道,目前国内的很多应用都采用了UIWebView的混合编程技术,最常见的是微信公众号的内容页面.前段时间在做微信公众平台相关的开发,发现很多应用场景 ...

  5. iOS真机测试详细步骤及图解

    0.首先使用开发者账号登录该网站 https://developer.apple.com 关于开发者账号分为三种: 个人.公司.企业,具体如何成为公司或者企业账号,请百度 为什么要真机调试?模拟器调试 ...

  6. ios手机fiddler代理详细步骤

    索大镇楼,废话不多说 1.安装fildder,可以在你电脑的软件管家中下载,这里就不贴下载地址啦 2.fildder设置 Tools-Otions-HTTPS配置 connetions配置 3.安装证 ...

  7. iOS苹果内购详细步骤

    一.设置协议等相关 1.点击协议.税务和银行业务. 点击协议.税务和银行业务.png 2.点击 Request Contracts 下面的 Request,一直点击直到主协议界面. Request.p ...

  8. iOS 苹果内购详细步骤

    一.设置协议等相关 1.点击协议.税务和银行业务. 2.点击 Request Contracts 下面的 Request,一直点击直到主协议界面. 主协议界面 Contact info :联系人信息 ...

  9. iOS App抓取图片详细步骤图解

    如何获取其他App中的图片 应用场景 对于想仿写别人的App的时候,例如很多初学者都喜欢仿写微博,仿写App需要对应的图片素材,此时可以直接抓取即可 具体操作步骤 1.下载抓取图片的Mac工具: iO ...

最新文章

  1. A Fully Featured Windows HTTP Wrapper in C++
  2. 健康管理-健康的概念和健康管理目标特点
  3. c语言中的数组覆盖,[求助] 怎么得到被覆盖的数组?
  4. 算法笔记_163:算法提高 最大乘积(Java)
  5. Huntor中国CRM评估报告连载(一)
  6. mysql语句1=1_mysql - “where 1 = 1”语句
  7. 贪心整理一本通1431:钓鱼题解
  8. R语言ETL系列:过滤(filter)
  9. 计算机等级考试网络数据,全国计算机等级考试三级信息、网络、数据库上机编程题15道...
  10. socket编程常用函数
  11. fileman命令的帮助+?
  12. [Java教程 00] 计算机基础
  13. Rsyslog Properties and the Property Replacer
  14. Javascript:简易天数计算器
  15. 酶促反应动力学_酶促反应动力学中,米氏方程怎么推导出来的?具体怎么应用?...
  16. java利用poi导出excel功能-附带图片导出
  17. 用苹果手机计算机程序二,两台iPhone怎么互传软件 苹果手机互传应用的3个小技巧...
  18. 网络游戏引入人工智能:游戏玩家并非真人
  19. java sshd实现连接ssh操作
  20. 基金 thread.php,这个时候,我们可以为基金“上会通过”做点什么?(附方案) - 基金申请 - 小木虫 - 学术 科研 互动社区...

热门文章

  1. 输入有序数组返回下标
  2. Python学习之迭代器协议
  3. stm32串口学习(二)
  4. stm32中断优先级快速入门
  5. Darknet_Yolov4实战(二)_安装OpenCV
  6. 本博客正式开通 Chat快问 功能
  7. Android存储系统之架构篇
  8. layui 刷新页面_layuimini简洁、清爽、易用的layui后台框架模板
  9. JZOJ 6030. 【GDOI2019模拟2019.2.25】白白的
  10. JZOJ 5354. 【NOIP2017提高A组模拟9.9】导弹拦截