吐血整理!!!转载请注明出处!
前言
如果不是公司业务非要用这种方案,请不要选这种方案,坑哭你。。。。
参考链接:
unity-in-framework
unity-ios-framework
正题
- #####工具版本
unity版本 2018.2.1f
Xcode版本 xcode10(10的小版本没一个个试应该都可以 我是10.2.1)
很重要这两个版本对不上 会有很多其他的错误,只能自己在解决了,每个版本unity导出的c文件都不一样,xcode配置也略有不同(具体哪里不同别问我,我也不知道,反正各种报错就对了)。
- #####文件打包
在playsetting中other settings中的strip engine code勾去掉,正常导出unity中的iOS工程。这步不会去百度,很简单,百度也一大堆。
- #####新建iOS的framework工程
新建一个Unity文件夹,文件夹和App..xcodeproj文件同一目录,加入工程 把Data Library Classes都拖进新建的工程
把 Data 文件夹加入工程 选择Create folder refrences
把Library和Classes 加入工程选择Create groups
去掉Library中的libil2cpp文件夹Remove Refreence
把MapFileParser.sh加入到根目录下,不用拖到项目中也就是和App..xcodeproj文件同一目录。
在Build phases中添加Run Script脚本"$PROJECT_DIR/MapFileParser.sh"
加入几个预先写好的类主要是用来调用u3d中界面类的下载地址
重要
SpaceAppController.mm中的sharedController类方法,Unity使用_NSGetExecutablePath来查找可执行文件的路径,所以用facebook的fishhook hook住动态链接。
接下来,我们需要覆盖UnityAppController以防止Unity在加载资源时接管应用程序UI并使用框架路径而不是主程序包。覆盖didFinishLaunchingWithOptions并执行以下更改:
1 2 3 4
| // 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)
编译通过
- #####新建测试APP工程
把编译出的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代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| // // 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非常危险,请谨慎使用!