Unity3D工程打包成iOS的framework

如果不是公司业务非要用这种方案,请不要选这种方案,坑哭你。。。。 参考链接: unity-in-framework unity-ios-framework

正题

  1. 工具版本

unity版本 2018.2.1f Xcode版本 xcode10(10的小版本没一个个试应该都可以 我是10.2.1) 很重要这两个版本对不上 会有很多其他的错误,只能自己在解决了,每个版本unity导出的c文件都不一样,xcode配置也略有不同(具体哪里不同别问我,我也不知道,反正各种报错就对了)。

  1. 文件打包

在playsetting中other settings中的strip engine code勾去掉,正常导出unity中的iOS工程。这步不会去百度,很简单,百度也一大堆。

  1. 新建iOS的framework工程

新建一个Unity文件夹,文件夹和App..xcodeproj文件同一目录,加入工程 把Data Library Classes都拖进新建的工程 把 Data 文件夹加入工程 选择Create folder refrences

LibraryClasses 加入工程选择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并执行以下更改:

// 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)

编译通过

  1. 新建测试APP工程

把编译出的framework拖入测试工程

Build phases中添加Run Script脚本"$PROJECT_DIR/MapFileParser.sh" MapFileParser.sh别忘了拖过来

添加copy file parse

 

 

<figcaption style="display: block; text-align: center; font-size: 1rem; line-height: 1.6; color: rgb(144, 144, 144); margin-top: 2px;"></figcaption>

删掉 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

posted @ 2021-01-26 17:43  FakeCoder  阅读(784)  评论(0编辑  收藏  举报