简介
目前项目中有用到过Unity整合到原生项目中,里边涉及很多坑,所以现在记录一下。版本号:unity 5.4.0f3 + Xcode 9.2。
一:Unity导出的工程
image.pngUnity导入原生工程准备工作
-
1、将一些必要的文件导入iOS Native工程中去
-
2、将Classes 和 Libraries 文件夹以 下面的方式导入自己的工程中
image.png -
3、将Data 文件夹以下面的方式导入工程中
image.png -
4、删除Native文件夹下的所有.h文件的引用,清除native中所有的.h引用。为了更快的删除定位到这些.h文件。首先选定Native文件夹
image.png
然后进行这样的操作,注意一定要选择 Remove Reference。千万不要选择 Move to Trash -
5、然后对应删除Libraries-->libil2cpp文件夹。删除方法同Native中的.h文件的移除。
image.png -
6、最后工程目录是这样的:
image.png
二:添加引用库文件
image.png三:工程文件修改
-
1、关闭bitcode,在build settings中bitcode关闭
-
2、在 other Linker Flags 添加 -weak_framework CoreMotion -weak-lSystem
image.png
-
3、在Header Search Path 添加下面这些头文件引用
-
3.1 Header Search Paths
$(PROJECT_DIR)/unity/Libraries
$(PROJECT_DIR)/unity/Libraries/libil2cpp/include
$(PROJECT_DIR)/unity/Classes
$(PROJECT_DIR)/unity/Libraries/Plugins/iOS
$(PROJECT_DIR)/unity/Native
- 3.2 Library Search Paths
$(PROJECT_DIR)/unity/Libraries
$(PROJECT_DIR)/unity/Libraries/Plugins/iOS
$(PROJECT_DIR)/unity
-
4、在 other C Flags 和 other C++ Flags 中添加 -DINIT_SCRIPTING_BACKEND=1(Debug和Release中都添加)
image.png
-
5、在 C Language Dialect 里 将选项改为 C99
image.png
-
6、在 precompile Prefix Header里,改PCH(将两个pch文件合并)
image.png
-
7、在Apple LLVM 9.0 - Warnings - Objective C
- 8、在 User - Defined下添加
GCC_THUMB_SUPPORT NO
GCC_USE_INDIRECT_FUNCTION_CALLS NO
UNITY_RUNTIME_VERSION 5.4.0f3
UNITY_SCRIPTING_BACKEND il2cpp
image.png
因为添加User - Defined的方式不同,所以这块给出添加的方法:
三、项目文件修改
- 3.1 由于swift里边默认没有 main.m 文件,所以还要做一些事情。
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
- 3.2 更改main.m文件为 main.mm 文件。将unity下Classes中的main.mm中的内容合并到原来工程的main.mm工程中。然后 删除 Classes中的main文件。
//main.mm文件里添加下边一行代码
/********************* 创建unity相关 开始 **********************/
custom_unity_init(CommandLine.argc, CommandLine.unsafeArgv)
/********************* 创建unity相关 结束 **********************/
- 3.3 创建 PrefixHeader.pch 文件,在文件里引入 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相关 开始 ************************/
- 3.4 在unityAppController.h中更改GetAppController()方法(OC当然要先引入#import "AppDelegate.h")
//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
- 3.5 APPdelegate里修改
/// 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
}
网友评论