一直想抽空整理一下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 = ; 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 ;
} #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];
//unity
self.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];
}

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

unity导出工程导入到iOS原生工程中详细步骤的更多相关文章

  1. iOS原生项目中集成React Native

    1.本文的前提条件是,电脑上已经安装了CocoaPods,React Native相关环境. 2.使用Xcode新建一个工程.EmbedRNMeituan [图1] 3.使用CocoaPods安装Re ...

  2. unitty导出工程嵌入iOS原生工程中出现黑屏,但是模型还是可以扫。

    一般上出现这个问题,其实就是因为两个注意点没有搞清楚.我们分析一下,如果我们的模型或者视屏能够出来但是屏幕还是黑屏的.说明我们的unity的组件其实已经加载出来了.但是供我们交互的那个Layer类并没 ...

  3. Eclipse项目工程导入到IDEA继续开发-超详细

    现在Java开发的主流工具是IDEA,不是说Eclipse,各有各的特色.不过我现在深深的爱上了idea这个工具. 但是之前很多项目都是用eclipse开发的,现在就转入到idea中进行继续开发. 1 ...

  4. ios真机调试详细步骤

    •真机调试的主要步骤 1.登录开发者主页 2.生成cer证书:cer是一个跟电脑相关联的证书文件,让电脑具备真机调试的功能 3.添加App ID:调试哪些app? 4.注册真机设备:哪台设备需要做真机 ...

  5. ios原生项目内嵌u3d工程

    本文一反常态,目标是把u3d工程以framewWork形式 内嵌原生IOS项目 1.xcode中新建Cocoa Touch FrameWork.取名u3dFrameWork 2.把u3d导出的xcod ...

  6. 如何在原生工程中引入Cordova工程-for iOS 【转】

    http://blog.csdn.net/e20914053/article/details/50170487 如今混合开发方兴未艾,有的项目可能一开始是原生开发的,后期需要加入混合开发,如将Cord ...

  7. iOS原生项目集成React Native模块

    今天周末,弄弄Native和React Native之间的交互.首先,先在iOS原生项目中集成React Native模块: 注意事项: 1.因为react native的版本问题,部分细节可能有所不 ...

  8. iOS原生 和 react native视图混编

    在iOS原生功能中加入RN,请看之前 写的 RN与iOS交互系列文章.本篇只讲下视图混编. 关键点只有二: 1.通过 RCTRootView 加载RN视图. 2.RN中,只需要AppRegistry. ...

  9. React Native项目集成iOS原生模块

    今天学习一下怎么在React Native项目中集成iOS原生模块,道理和在iOS原生项目中集成React Native模块类似.他们的界面跳转靠的都是iOS原生的UINavigationContro ...

随机推荐

  1. 动画在webapp中的现状

    webapp的一大优势便是在view切换时候可以拥有媲美与native的动画效果,但是很多时候那只是一种想法,真正的情况却不是这样 产生此问题的原因有: ① 手机CPU烂! ② 手机显卡烂!就算四核其 ...

  2. Mysql(Mariadb) 基础操作语句 (持续更新)

    基础SQL语句,记录以备查阅.(在HeiDiSql中执行) # 创建数据库 Create Database If Not Exists VerifyIdear Character Set UTF8; ...

  3. C#通过WMI的wind32 的API函数实现msinfo32的本地和远程计算机的系统日志查看功能

    先不说如何实现,先来看看效果图: 读取远程的需要提供下远程的计算用户名和密码即可. 如何实现这个代码功能,请看如下代码部分: 实体类: using System; using System.Colle ...

  4. IOS开发基础知识--碎片32

    1:动画属性UIViewAnimationOptions说明 a:常规动画属性设置(可以同时选择多个进行设置) UIViewAnimationOptionLayoutSubviews:动画过程中保证子 ...

  5. 带交互的 iOS 产品原型可以用什么软件制作?

    摘自知乎http://www.zhihu.com/question/20326729 来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 首先如果你小团队或者个人开发,当然 ...

  6. [sql查询] 重复数据只取一条

    SELECT * FROM tab_init WHERE id IN ( --根据Data分类获取数据最小ID列表 select max(id) from tab_init group by a,b ...

  7. 我是这么给娃娃取名的(使用 node.js )

    依据: 81 命理,需要让五格都为大吉(吉).五格命理请自行谷歌. 我的是单姓复名.姓是固定的. 废话不说,上代码: Array.prototype.contains = function (k) { ...

  8. -bash: ulimit: pipe size: cannot modify limit: Invalid argument

    从root账号切换到oracle账号时,出现了"-bash: ulimit: pipe size: cannot modify limit: Invalid argument"提示 ...

  9. Ignite安装配置——上篇

    Ignite介绍 Ignite 是SolarWinds公司开发的一款数据库性能监控.性能分析并提供优化解决方案的性能检测分析工具,Ignite配置简单.方便:它会收集实时会话数据.服务器资源使用情况, ...

  10. 安装Ubuntu的那些事儿(续)

    由于我的第一篇Blog并没有给出完全解决进Ubuntu系统时显卡所造成的问题,至于那个装显卡驱动的方法本人也没有去做,感兴趣的朋友可以在网上教程试一试. 至于我的那个在高系选项中进行配置也不是好的方法 ...