Unity3D工程打包成iOS的framework

吐血整理!!!轉載請註明出處!ios

前言

若是不是公司業務非要用這種方案,請不要選這種方案,坑哭你。。。。 參考連接: unity-in-framework unity-ios-frameworkgit

正題

  1. 工具版本

unity版本 2018.2.1f Xcode版本 xcode10(10的小版本沒一個個試應該均可以 我是10.2.1) 很重要這兩個版本對不上 會有不少其餘的錯誤,只能本身在解決了,每一個版本unity導出的c文件都不同,xcode配置也略有不一樣(具體哪裏不一樣別問我,我也不知道,反正各類報錯就對了)。github

  1. 文件打包

在playsetting中other settings中的strip engine code勾去掉,正常導出unity中的iOS工程。這步不會去百度,很簡單,百度也一大堆。xcode

  1. 新建iOS的framework工程

新建一個Unity文件夾,文件夾和App..xcodeproj文件同一目錄,加入工程 把Data Library Classes都拖進新建的工程 把 Data 文件夾加入工程 選擇Create folder refrencesbash

LibraryClasses 加入工程選擇Create groupsapp

去掉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]);
複製代碼

添加系統庫 見下圖

屏幕快照 2019-05-05 下午5.08.53.png

Build Settings中添加User Define Setting 兩項

屏幕快照 2019-05-05 下午3.35.27.png

繼續在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

屏幕快照 2019-05-05 下午5.11.30.png

刪掉 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很是危險,請謹慎使用!

相關文章
相關標籤/搜索