Integrate Unity3D with iOS (oc+unity)

iOS合并unity老是有很多问题,这里我整理了一份操作步骤(按照步骤往下走,就不解释原因了),不同项目成功过好几次,助大家越坑,不过不保证一步到位,之后还有什么问题可以移步到stack overflow查找。

1.文件导入

从unity
项目中copy这几个文件到工程根目录下

2. 添加文件引用(都不需要copy items)

Classes,
Libraries,MapFileParser.sh 这三个文件 设置
Create groups

Data 设置
Create folder

3.删除引用

先删除引用libraries里面的libil2cpp文件夹,然后再删除Classes里面的Native文件夹里面的所有.h文件

4.添加引用库

添加引用库,注意有三个是optional

5.Build Settings

按图上配置

other linker flags 添加: -weak_framework CoreMotion -weak-lSystem

header search path添加:

"$(SRCROOT)/Classes"

"$(SRCROOT)"

$(SRCROOT)/Classes/Native

$(SRCROOT)/Libraries/bdwgc/include

$(SRCROOT)/Libraries/libil2cpp/include

Library Search Path添加:

$(inherited)

"$(SRCROOT)"

"$(SRCROOT)/Libraries"

$(SRCROOT)/Libraries/Plugins/iOS

other C Flags添加:

DINIT_SCRIPTING_BACKEND=1

Mismatched Return Type改成只有Yes

6.设置Prefix Header

设置Precompile
Prefix Header 为Yes,工程里有pch文件的拷贝unity的 Prefix.pch里面代码到自己.pch,加上代码:#import "UnityAppController.h",并且删除Prefix.pch引用

如果没有.pch文件,可直接使用Perfix.pch作为项目pch文件,添加上

#import "UnityAppController.h",配置好项目Prefix Header路径即可

7.修改main.mm文件

将自己项目里main.m文件的相关方法移到main.mm文件里

删除自己的main.m

修改main.mm文件内容:

添加**#import "AppDelegate.h"

更改*const
char
AppControllerClassName = "AppDelegate";
**

更改**UIApplicationMain(argc,
argv, nil, [NSString stringWithUTF8String:AppControllerClassName]);
**

8.更改AppDelegate.h,AppDelegate.m 内容

在AppDelegate.h文件里,#import "UnityAppController.h"

使AppDelegate继承于UnityAppController

在AppDelegate.m里

添加IMPL_APP_CONTROLLER_SUBCLASS(AppDelegate)

注意:

**application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
**

**里面其他方法不需要改动,只需要将
**

**self.window = [[UIWindow
alloc]initWithFrame:[UIScreen mainScreen].bounds];
**

这一句去掉(unity内部会创建一个)

把这两句代码移到startUnity: 方法里面

**self.window.rootViewController
= 你们的rootViewController;
**

**
[self.window makeKeyAndVisible];
**

9.添加run script

添加:"$PROJECT_DIR/MapFileParser.sh"

10.user-Defined设置

在user-Defined 添加如下:

GCC_THUMB_SUPPORT NO

GCC_USE_INDIRECT_FUNCTION_CALLS NO

UNITY_RUNTIME_VERSION 5.3.1f1

UNITY_SCRIPTING_BACKEND il2cpp

11.添加@class

UnityViewControllerBase; UnityAppController.h文件里

编译下试试,没有问题就恭喜啦
swift集成可以参考我的上一篇

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容