iOS原生集成easyAR unity工程

最近遇到一個(gè)識(shí)別圖像后顯示3D模型的需求,權(quán)衡再三選擇了國(guó)內(nèi)的easyAR SDK。Unity建模完成后,導(dǎo)出相應(yīng)的iOS工程,由于Unity模塊在我的工程中是作為一個(gè)功能模塊存在的,因此需要將導(dǎo)出的Unity iOS工程集成到原生工程中。集成過(guò)程稍顯復(fù)雜,也躺過(guò)一些坑,此處做一個(gè)記錄以銘心志。另外關(guān)于Unity建模不是本文要探討的內(nèi)容,本人也純屬Unity小白,瞎折騰一番后模型勉強(qiáng)符合預(yù)期,慚愧~

【Unity版本】2017.1.1f1 【Xcode版本】8.3.3

導(dǎo)出Unity iOS工程

Unity模型搭建好之后,需要導(dǎo)出iOS工程。相關(guān)配置如下:

0

由于是easyAR unity工程,務(wù)必保證Bundle ID與easyAR中注冊(cè)的一致。配置完成后點(diǎn)擊build,即可導(dǎo)出unity iOS工程。本例中工程文件結(jié)構(gòu)如下,其中 unity_general 為unity導(dǎo)出的iOS工程,zhb_general 為目標(biāo)iOS工程。

unity文件引用

在目標(biāo)iOS工程里引用unity iOS工程中的文件,主要就是三個(gè)目錄,Classes,Libraries,Data。這里需要注意的是,Classes和Libraries目錄作為Group引用,切記不要勾選copy,而Data目錄不需要參與編譯,作為folder引用進(jìn)來(lái)即可。本例中統(tǒng)一引用到Unity group下,文件結(jié)構(gòu)如下:

工程配置

  • 關(guān)閉bitcode。新版的Unity已經(jīng)支持Bitcode但EasyAR并不支持,不關(guān)閉無(wú)法正常編譯。


  • 修改Linking -> Other Linker Flags選項(xiàng),添加參數(shù) -weak_framework CoreMotion -weak-lSystem。

  • 修改頭文件搜索目錄
  • 修改庫(kù)搜索目錄
  • 修改LLVM - Custom Complier Flags -> Other C Flags選項(xiàng),添加兩個(gè)參數(shù): -DINIT_SCRIPTING_BACKEND=1-DRUNTIME_IL2CPP=1。
  • 修改LLVM - Language -> C Language Dialect選項(xiàng),選擇C99。
7
  • 修改LLVM - Language - C++ -> C++ Language Dialect選項(xiàng),選擇 C++11。
  • 添加三項(xiàng)自定義設(shè)置
    • MTL_ENABLE_DEBUG_INFO -> NO
    • UNITY_RUNTIME_VERSION -> 2017.1.1f1(當(dāng)前你的Unity3d版本號(hào),請(qǐng)自行替換)
    • UNITY_SCRIPTING_BACKEND -> il2cpp
  • 新建一個(gè)PCH文件,并修改Precompile Prefix Header為YES,關(guān)聯(lián)pch文件路徑。此處新建文件名為 PrefixHeader.pch。

添加工程依賴

修改代碼

  1. 找到unity工程的pch文件,完整復(fù)制內(nèi)容到之前建好的PrefixHeader.pch文件中。

  2. 找到unity工程的main.m文件,復(fù)制其內(nèi)容到新建工程的main.m文件中,將main.m修改為main.mm允許C++混編,并修改AppDelegate為工程代理類。

    #include "RegisterMonoModules.h"
    #include "RegisterFeatures.h"
    #include <csignal>
    
    // Hack to work around iOS SDK 4.3 linker problem
    // we need at least one __TEXT, __const section entry in main application .o files
    // to get this section emitted at right time and so avoid LC_ENCRYPTION_INFO size miscalculation
    static const int constsection = 0;
    
    void UnityInitTrampoline();
    
    // WARNING: this MUST be c decl (NSString ctor will be called after +load, so we cant really change its value)
    const char* AppControllerClassName = "UnityAppController";
    
    int main(int argc, char* argv[])
    {
        UnityInitStartupTime();
        @autoreleasepool
        {
            UnityInitTrampoline();
            UnityInitRuntime(argc, argv);
            
            RegisterMonoModules();
            NSLog(@"-> registered mono modules %p\n", &constsection);
            RegisterFeatures();
            
            // iOS terminates open sockets when an application enters background mode.
            // The next write to any of such socket causes SIGPIPE signal being raised,
            // even if the request has been done from scripting side. This disables the
            // signal and allows Mono to throw a proper C# exception.
            std::signal(SIGPIPE, SIG_IGN);
            UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String:"AppDelegate"]);
        }
        return 0;
    }
    
    #if TARGET_IPHONE_SIMULATOR && TARGET_TVOS_SIMULATOR
    
    #include <pthread.h>
    
    extern "C" int pthread_cond_init$UNIX2003(pthread_cond_t *cond, const pthread_condattr_t *attr)
    { return pthread_cond_init(cond, attr); }
    extern "C" int pthread_cond_destroy$UNIX2003(pthread_cond_t *cond)
    { return pthread_cond_destroy(cond); }
    extern "C" int pthread_cond_wait$UNIX2003(pthread_cond_t *cond, pthread_mutex_t *mutex)
    { return pthread_cond_wait(cond, mutex); }
    extern "C" int pthread_cond_timedwait$UNIX2003(pthread_cond_t *cond, pthread_mutex_t *mutex,
                                                   const struct timespec *abstime)
    { return pthread_cond_timedwait(cond, mutex, abstime); }
    
    #endif // TARGET_IPHONE_SIMULATOR && TARGET_TVOS_SIMULATOR
    

    同時(shí)在Build Phases -> Compier Sources 中刪掉unity工程中的main.mm,只編譯目標(biāo)工程的main.mm。

  1. 修改UnityAppController.h,將其中的內(nèi)聯(lián)函數(shù)GetAppController()修改如下:

    #import "AppDelegate.h"
    inline UnityAppController* GetAppController()
    {
        AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        return delegate.unityController;
    //    return (UnityAppController*)[UIApplication sharedApplication].delegate;
    }
    
  2. 新建EasyARAppController.h,因?yàn)樵镜腅asyARAppController是沒(méi)有頭文件的,是通過(guò)OC運(yùn)行時(shí)初始化的,為了方便在代碼中初始化和使用,單獨(dú)建立它的頭文件,并調(diào)整一下EasyARAppController.mm文件。

    //EasyARAppController.h 
    #import "UnityAppController.h"
    @interface EasyARAppController : UnityAppController
    @end
    
    //EasyARAppController.mm
    #import <UIKit/UIKit.h>
    #import "EasyARAppController.h"
    
    extern "C" void ezarUnitySetGraphicsDevice(void* device, int deviceType, int eventType);
    extern "C" void ezarUnityRenderEvent(int marker);
    
    @interface EasyARAppController ()
    - (void)shouldAttachRenderDelegate;
    @end
    
    @implementation EasyARAppController
    - (void)shouldAttachRenderDelegate;
    {
        UnityRegisterRenderingPlugin(&ezarUnitySetGraphicsDevice, &ezarUnityRenderEvent);
    }
    @end
    
    IMPL_APP_CONTROLLER_SUBCLASS(EasyARAppController)
    
  3. 創(chuàng)建MyARAppController,繼承自EasyARAppController,作為后續(xù)開(kāi)發(fā)的交互controller。

    //MyARAppController.h
    #import "EasyARAppController.h"
    @interface MyARAppController : EasyARAppController
    @end
    
    //MyARAppController.mm
    #import "MyARAppController.h"
    #import "AppDelegate.h"
    #import "UnityViewControllerListener.h"
    
    #define BackButtonTag 10000
    
    extern "C" {
        void backToNative(){
            AppDelegate* delegate =  (AppDelegate *)[UIApplication sharedApplication].delegate;
            [delegate hideUnityWindow];
        }
    }
    
    @interface MyARAppController ()
    @property (nonatomic, strong) UIButton *backButton;
    @end
    
    @implementation MyARAppController
    
    - (instancetype)init{
        self = [super init];
        if (self) {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewDidLoad) name:kUnityViewWillAppear object:self.rootViewController];
        }
        return self;
    }
    
    -(void)viewDidLoad{
        if (![self.rootViewController.view viewWithTag:BackButtonTag]) {
            [self.rootViewController.view addSubview:self.backButton];
            [self.rootViewController.view bringSubviewToFront:self.backButton];
        }
    }
    
    - (void)actionBack{
        backToNative();
    }
    
    - (UIButton *)backButton{
        if (!_backButton) {
            _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
            _backButton.frame = CGRectMake(10, 10, 60, 44);
            _backButton.backgroundColor = [UIColor clearColor];
            [_backButton setTitle:@"退出" forState:UIControlStateNormal];
            [_backButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            [_backButton addTarget:self action:@selector(actionBack) forControlEvents:UIControlEventTouchUpInside];
            _backButton.tag = BackButtonTag;
        }
        return _backButton;
    }
    
    - (void)dealloc{
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    
    @end
    
  4. 修改AppDelegate,將AppDelegate.m修改為AppDelegate.mm,并創(chuàng)建新的Unity3d入口。

    //AppDelegate.h
    #import <UIKit/UIKit.h>
    @class UnityAppController;
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    @property (strong, nonatomic) UIWindow *window;
    @property (strong, nonatomic) UIWindow *unityWindow;
    @property (strong, nonatomic) UnityAppController *unityController;
    -(void)showUnityWindow;
    -(void)hideUnityWindow;
    @end
    
    //AppDelegate.mm
    #import "AppDelegate.h"
    #import <IQKeyboardManager.h>
    #import "MyARAppController.h"
    
    @interface AppDelegate ()
    @property (nonatomic,strong) UINavigationController *navVC;
    @end
    
    @implementation AppDelegate
    
    -(UIWindow *)unityWindow{
        return UnityGetMainWindow();
    }
    
    -(void)showUnityWindow{
        self.unityWindow.hidden = NO;
        UnitySendMessage("CameraDevice", "StartCapture", "");
    }
    
    -(void)hideUnityWindow{
        self.unityWindow.hidden = YES;
        UnitySendMessage("CameraDevice", "StopCapture", "");
    }
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
        self.window.backgroundColor = [UIColor whiteColor];
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        self.navVC = [sb instantiateInitialViewController];
        self.window.rootViewController = self.navVC;
        [self.window makeKeyAndVisible];
        
        self.unityController = [[MyARAppController alloc]init];
        [self.unityController application:application didFinishLaunchingWithOptions:launchOptions];
        [self hideUnityWindow];
      
        return YES;
    }
    
    - (void)applicationWillResignActive:(UIApplication *)application {
        [self.unityController applicationWillResignActive:application];
    }
    - (void)applicationDidEnterBackground:(UIApplication *)application {
        [self.unityController applicationDidEnterBackground:application];
    }
    - (void)applicationWillEnterForeground:(UIApplication *)application {
        [self.unityController applicationWillEnterForeground:application];
    }
    - (void)applicationDidBecomeActive:(UIApplication *)application {
        [self.unityController applicationDidBecomeActive:application];
    }
    - (void)applicationWillTerminate:(UIApplication *)application {
        [self.unityController applicationWillTerminate:application];
    }
    
    @end
    
  5. 設(shè)置入口,此處以ViewController作為入口控制器。

    #import "ViewController.h"
    #import "AppDelegate.h"
    @implementation ViewController
    - (void)viewDidLoad {
        [super viewDidLoad];
        UIButton * enterBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        enterBtn.frame = CGRectMake(0, 0, 100, 100);
        enterBtn.center = self.view.center;
        [enterBtn setTitle:@"進(jìn)入" forState:UIControlStateNormal];
        [enterBtn addTarget:self action:@selector(didClickEnter:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:enterBtn];
    }
    
    -(void)didClickEnter:(UIButton*)btn{
         //喚起unity界面
        AppDelegate * delegate = [UIApplication sharedApplication].delegate;
        [delegate showUnityWindow];
    }
    @end
    

躺過(guò)的坑

  1. 編譯成功后,APP啟動(dòng)時(shí)crash:il2cpp::vm::MetadataCache::Initialize{}

解決辦法是在 Other C Flags選項(xiàng)中添加 -DRUNTIME_IL2CPP=1 參數(shù)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容