iOS开发

Unity导出工程整合进iOS原生工程(swift)

2018-07-10  本文已影响28人  jeckHao

简介

目前项目中有用到过Unity整合到原生项目中,里边涉及很多坑,所以现在记录一下。版本号:unity 5.4.0f3 + Xcode 9.2。

一:Unity导出的工程

image.png

Unity导入原生工程准备工作

二:添加引用库文件

image.png

三:工程文件修改

$(PROJECT_DIR)/unity/Libraries

$(PROJECT_DIR)/unity/Libraries/libil2cpp/include

$(PROJECT_DIR)/unity/Classes

$(PROJECT_DIR)/unity/Libraries/Plugins/iOS

$(PROJECT_DIR)/unity/Native

$(PROJECT_DIR)/unity/Libraries

$(PROJECT_DIR)/unity/Libraries/Plugins/iOS

$(PROJECT_DIR)/unity

image.png
GCC_THUMB_SUPPORT                   NO
GCC_USE_INDIRECT_FUNCTION_CALLS     NO
UNITY_RUNTIME_VERSION               5.4.0f3
UNITY_SCRIPTING_BACKEND             il2cpp
image.png

因为添加User - Defined的方式不同,所以这块给出添加的方法:

image.png

三、项目文件修改

3.1.1、创建main.m文件

import Foundation
import UIKit

let newUnsafeArgv = UnsafeMutableRawPointer( CommandLine.unsafeArgv ).bindMemory( to: UnsafeMutablePointer<Int8>.self, capacity: Int( CommandLine.argc ) )
UIApplicationMain(CommandLine.argc, newUnsafeArgv, NSStringFromClass( UIApplication.self ), NSStringFromClass( AppDelegate.self ))

3.1.2、去掉APPdelegate文件中的 @UIApplicationMain

//main.mm文件里添加下边一行代码
/********************* 创建unity相关 开始 **********************/
custom_unity_init(CommandLine.argc, CommandLine.unsafeArgv)
/********************* 创建unity相关 结束 **********************/
#ifndef PrefixHeader_pch
#define PrefixHeader_pch

#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#endif

//unity相关
#include "Preprocessor.h"
#include "UnityTrampolineConfigure.h"
#include "UnityInterface.h"

#ifndef __OBJC__
#if USE_IL2CPP_PCH
#include "il2cpp_precompiled_header.h"
#endif
#endif

#endif

然后修改APPdelegate文件,添加 unityController 变量

/*********************** unity相关 开始 ************************/
    /// unity的控制器
    var unityController: UnityAppController!
/*********************** unity相关 开始 ************************/
//inline UnityAppController*    GetAppController()
//{
//  return (UnityAppController*)[UIApplication sharedApplication].delegate;
//}
NS_INLINE UnityAppController* GetAppController()
{
    NSObject<UIApplicationDelegate>* delegate = [UIApplication sharedApplication].delegate;
    UnityAppController* currentUnityController = (UnityAppController *)[delegate valueForKey:@"unityController"];
    return currentUnityController;
}
image.png
/// unity的keywindow
lazy var unityWindow: UIWindow = {
     let window = UnityGetMainWindow()
     window?.backgroundColor = UIColor.white
     return window!
 }()
    
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.backgroundColor = UIColor.white
        self.window?.windowLevel = UIWindowLevelNormal+1
        rootVC = TabBarController()
        self.window?.rootViewController = rootVC
        //初始化 unityController
         unityController = UnityAppController()
         unityController?.application(application,didFinishLaunchingWithOptions: launchOptions)
         self.window?.makeKeyAndVisible()
         return true
}

func applicationWillResignActive(_ application: UIApplication) {
        //程序挂起:接听电话,锁屏
        print("程序挂起:接听电话,锁屏")    
        //unity相关
        unityController.applicationWillResignActive(application)
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        //程序进入后台
        print("程序进入后台")  
        //unity相关
        unityController.applicationDidEnterBackground(application)
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        //程序即将进入后台开始活跃
        print("程序即将进入后台开始活跃")
        //unity相关
        unityController.applicationWillEnterForeground(application)
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        //程序复原或进入活跃状态
        print("程序复原或进入活跃状态")
        
        //unity相关
        unityController.applicationDidBecomeActive(application)

    }

    func applicationWillTerminate(_ application: UIApplication) {
        //程序终止
        print("程序终止")
        //unity相关
        unityController.applicationWillTerminate(application)
    }
    
    func shouldAttachRenderDelegate() {
        unityController?.renderDelegate = VuforiaRenderDelegate()
        //        UnityRegisterRenderingPlugin(nil, &)
    }
    
    func showUnityWindow() {
        if fristLaunchUnity {
            //初始化返回按钮等等操作
            configuration()
        }
        unityisshow = true
        unityWindow.makeKeyAndVisible()
        //设置window的显示层序
        let window = AppDelegate.shareInstance().window
        window?.windowLevel = UIWindowLevelNormal
        unityWindow.windowLevel = UIWindowLevelNormal+1
    }
    
    func hiddenUnityWindow() {
        unityisshow = false
        let window = AppDelegate.shareInstance().window
        window?.makeKeyAndVisible()
        recoType = .RECOTYPE_UNKNOWN
        //设置window的显示层序
        window?.windowLevel = UIWindowLevelNormal+1
        unityWindow.windowLevel = UIWindowLevelNormal
    }

最终:这样就把unity相关代码结合到iOS原生里了。普及一个事情就是,unity的window一旦开启,就会一直在后台运行。

上一篇下一篇

猜你喜欢

热点阅读