吐血整理!!!转载请注明出处!ios
若是不是公司业务非要用这种方案,请不要选这种方案,坑哭你。。。。 参考连接: unity-in-framework unity-ios-frameworkgit
unity版本 2018.2.1f Xcode版本 xcode10(10的小版本没一个个试应该均可以 我是10.2.1) 很重要这两个版本对不上 会有不少其余的错误,只能本身在解决了,每一个版本unity导出的c文件都不同,xcode配置也略有不一样(具体哪里不一样别问我,我也不知道,反正各类报错就对了)。github
在playsetting中other settings中的strip engine code
勾去掉,正常导出unity中的iOS工程。这步不会去百度,很简单,百度也一大堆。xcode
新建一个Unity文件夹,文件夹和App..xcodeproj
文件同一目录,加入工程 把Data Library Classes
都拖进新建的工程 把 Data
文件夹加入工程 选择Create folder refrences
bash
把Library
和Classes
加入工程选择Create groups
app
去掉Library
中的libil2cpp
文件夹Remove Refreence
框架
把MapFileParser.sh
加入到根目录下,不用拖到项目中也就是和App..xcodeproj
文件同一目录。工具
在Build phases
中添加Run Script
脚本"$PROJECT_DIR/MapFileParser.sh"
测试
加入几个预先写好的类主要是用来调用u3d中界面类的下载地址ui
重要 SpaceAppController.mm
中的sharedController
类方法,Unity使用_NSGetExecutablePath
来查找可执行文件的路径,因此用facebook的fishhook hook住动态连接。
接下来,咱们须要覆盖UnityAppController以防止Unity在加载资源时接管应用程序UI并使用框架路径而不是主程序包。覆盖didFinishLaunchingWithOptions并执行如下更改:
// we will replace this:
// UnityInitApplicationNoGraphics([[[NSBundle mainBundle] bundlePath] UTF8String]);
// with this:
UnityInitApplicationNoGraphics([[[NSBundle bundleForClass:[self class]] bundlePath] UTF8String]);
复制代码
添加系统库 见下图
在Build Settings
中添加User Define Setting
两项
继续在Other Lunker Flags
中添加-weak_framework CoreMotion -weak-lSystem
Header Search Paths
中添加$(PROJECT_DIR)/Unity/Classes $(PROJECT_DIR)/Unity/Classes/Native $(PROJECT_DIR)/Unity/Libraries/bdwgc/include $(PROJECT_DIR)/Unity/Libraries/libil2cpp/include
Other C Flags
添加 -DINIT_SCRIPTING_BACKEND=1 -fno-strict-overflow -DRUNTIME_IL2CPP=1
Prefix Header
添加Unity/Classes/Prefix.pch
Mismatched Return Type
添加YES
不要用YES(Error)
编译经过
把编译出的framework拖入测试工程
在Build phases
中添加Run Script
脚本"$PROJECT_DIR/MapFileParser.sh"
MapFileParser.sh
别忘了拖过来
添加copy file parse
删掉 Build Setting
中的Library Search Paths
中的内容
Other C Flags
添加$(inherited) -weak_framework CoreMotion -weak-lSystem
viewController.m
代码
//
// ViewController.m
// testu3d-app
//
// Created by King on 2019/4/25.
// Copyright © 2019 King. All rights reserved.
//
#import "ViewController.h"
#import <testliboc/testliboc.h>
@interface ViewController ()
@property (nonatomic, weak) IBOutlet UIView* unityContainerView;
@property (nonatomic, strong) UIView* unityView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[[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
复制代码
全部的例子:地址我放百度网盘了 别问我为啥不传GitHub 打开你就知道了。网盘连接 打包编译的时候别忘了 把我例子里的iOS版本改为你要的
ps:其实我公司业务要求,把这个framework和公司原来的framework再次融合,再给APP集成,中间又有几个小坑。我就不说了。有须要的同窗能够给我留言。
感谢大大大大大萝卜指出的两个注意点: 1.framework主要须要dynamic,不然ipa体积会很大。 2.fishhook很是危险,请谨慎使用!