由于项目有此需求,所以不得不爬此坑,爬了两天终于出坑
参考
参考使用的Unity版本是2018.2.1f1 我的是2018.4.0f1 所以配置上有不同
1.首先Unity导出Xcode工程 这里注意一下 strip engine code 去勾
image.png
2.生成的iOS工程取MapFileParser.sh、Classes、Data、Libraries四个文件
image.png3.将文件导入framework工程中(iOS创建framework这里不讲解) 工程结构如下
image.png
Unity和testlibocSDK文件是同一目录 Data文件的引用选择Create folder refrences,其他选择Create groups,然后将Libraries下的libil2cpp文件夹Remove Refreence
Build phases配置
4.在Build phases中添加Run Script脚本"$PROJECT_DIR/MapFileParser.sh"
image.png5.添加几个类文件 类的下载地址
将SpaceAppController.h设置为Public
image.png
Build Settings设置
6.因为原生引用游戏 所以此SDK应该是动态库 静态库的话下面会说导致的问题 而且静态库比较占用资源 Mach-O Type 不用修改 (默认就是动态库 - Dynamic Library)
7.Other Linker Flags 添加$(inherited)、-weak_framework、CoreMotion、-weak-lSystem
image.png
8.Header Search Paths 添加
- $(inherited)、
- "$(SRCROOT)"、
- $(PROJECT_DIR)/Unity/Classes、
- $(PROJECT_DIR)/Unity/Classes/Native、
- $(PROJECT_DIR)/Unity/Libraries、
- $(PROJECT_DIR)/Unity/Libraries/bdwgc/include、
-
$(PROJECT_DIR)/Unity/Libraries/libil2cpp/include
image.png
9.Library Search Paths 不用设置 自己生成
image.png
10.Other C Flags 添加$(inherited)、-DINIT_SCRIPTING_BACKEND=1、-fno-strict-overflow、-DNET_4_0、-DRUNTIME_IL2CPP=1
image.png
11.Other C++ Flags
image.png
12.Prefix Header 添加Unity/Classes/Prefix.pch
image.png
13.Mismatched Return Type添加YES 不要用YES(Error)
image.png
14.User-Defined 添加UNITY_RUNTIME_VERSION -> Unity 版本号、UNITY_SCRIPTING_BACKEND -> il2cpp
image.png
爬坑过程!!
1.WWWConnection.h:4:41: error: cannot find interface declaration for 'NSObject', superclass of 'UnityWWWConnectionDelegate'
image.png
解决:系统库添加少了
2.'Classes/Unity/UnitySharedDecls.h' file not found
image.png
解决:找到文件 删除前面路径 改成 #include "UnitySharedDecls.h"
3.'Classes/iPhone_Sensors.h' file not found
解决同上
解决:删除对DynamicLibEngineAPI-functions.h和DynamicLibEngineAPI.mm的引用
5.Undefined symbol: __mh_execute_header
_UnityGetAppLoadAddress in CrashReporter.o
解决:找到CrashReporter.mm文件
注释代码UnityGetAppLoadAddress、UnityGetAppLoadCommandAddress、UnityGetAppLoadCommandCount
image.png
6.重复引用
image.png
解决:Unity的main.mm文件和导入的UnityMain.mm重复了 删除对main.mm的引用即可
7.这个问题刚开始就出现了 我反复删除添加libil2cpp文件夹貌似自己就好了 (主要是我也不记得怎么修复的了...)
image.png
当然你添加libil2cpp文件后会报另一个问题(libil2cpp文件里有些include引用会报错 排查非常麻烦,感觉加上libil2cpp文件就是不对的操作)
新项目引用SDK
1.引入framework 文件目录添加MapFileParser文件
image.png image.png
2.Build Phrase配置
添加Run Script "$PROJECT_DIR/MapFileParser.sh"
image.png
3.添加Copy Files
image.png
代码使用
ViewController.m文件 只需加上如下代码即可
#import "ViewController.h"
//#import <testliboc/testliboc.h>
#import <testlibocSDK/testlibocSDK.h>
@interface ViewController ()
//Main.storyboard上添加一个view 关联上即可
@property (nonatomic, weak) IBOutlet UIView* unityContainerView;
@property (nonatomic, strong) UIView* unityView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100, 50);
[btn setBackgroundColor:[UIColor redColor]];
[btn addTarget:self action:@selector(aaa) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)aaa{
[[SpaceAppController sharedController] application:[UIApplication sharedApplication] didFinishLaunchingWithOptions:[NSDictionary dictionary]];
[[SpaceAppController sharedController] applicationDidBecomeActive:[UIApplication sharedApplication]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
self.unityView = [SpaceAppController sharedController].unityView;
[self.unityContainerView addSubview:self.unityView];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.unityView.frame = self.unityContainerView.bounds;
}
- (void)applicationWillResignActive {
[[SpaceAppController sharedController] applicationWillResignActive:[UIApplication sharedApplication]];
}
- (void)applicationDidBecomeActive {
[[SpaceAppController sharedController] applicationDidBecomeActive:[UIApplication sharedApplication]];
}
- (void)applicationWillEnterForeground {
[[SpaceAppController sharedController] applicationWillEnterForeground:[UIApplication sharedApplication]];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[SpaceAppController sharedController] applicationWillResignActive:[UIApplication sharedApplication]];
}
@end
这里补充一下 之前打包的静态库framework的问题
静态库打包参考了上一篇Unity文件放到iOS里的配置及上面的参考链接还有静态库打包的配置(参考我之前写的framework打包)
打包成功后,项目引入时会报错(libil2cpp文件找不到),必须把Libraries文件也引入才行,然后运行没问题,但是当加载Unity时会报错 (crash occurred >>> il2cpp::vm:MetadataCache::Initialize())
image.png对于这个问题应该是Unity版本不同导致的 他应该是引用不到il2cpp的原因
关于此问题的讨论
网友评论