swift3.0融合Unity
前言
公司目前的项目是基于OC,融合了Unity做AR功能,目前已经比较稳定。swift现在已经趋向稳定,大家都在慢慢的向swift上面转,所以我就尝试了用swift融合一下看看,我直接用了swift3.0,会比之前的人少些坑吧...不过我没找到关于3.0融合的教程,就参考了一些2.3的和自己之前融合的经验搞了一下,结果还融成功了哈哈,由于Unity部分是公司的AR团队做的,我并不是很了解,估计会有一些Unity的坑我解决不了。
一些参考
大部分的步骤是参考童冀同学的,不过他用了cocoapods、swift2.3以及步骤个人感觉不是很通畅(哈哈哈😂),所以我就做了一遍,然后整理了一下步骤。
准备工作
- swift3.0工程,直接创建个空工程就可以
- Unity导出的Xcode工程
- ios-unity5-master,就是上边git上的下载下来的文件
GO
1.、 在swift工程文件夹下新建一个UnityFix文件放融合进来的Unity文件用,然后拉近项目里。


2、把刚才下载的文件夹中的Unity.xcconfig、UnityBridge.h、UnityUtils.h、UnityUtils.mm这4个文件拉进刚才创建的文件夹UnityFix中,拉的时候会提示创建桥接文件,选择不创建


3、修改配置、使用Unity.xcconfig、如图所示

修改buildSetting配置,路径和你unity的版本号



buildPhases中添加运行脚本,内容为最开始下载的UnityProjectRefresh.sh中的内容

4、修改UnityUtils.mm文件内容,改完发现报错不用管,因为还有一些文件没导进项目里。
//更改整个extern "C" int custom_unity_init 里面代码为:
extern "C" int custom_unity_init(int argc, char* argv[])
{
@autoreleasepool
{
UnityInitTrampoline();
UnityParseCommandLine(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:"AppControllerClassName"]);
}
return 0;
}
如图:

5、把unity导出的工程里Classes、Data、Libraries三个文件夹在finder里拉进UnityFix文件夹

6、把Classes、Libraries两个文件夹拉进工程里,不选copy,下边选create groups。这里时间会有点长,你的Xcode会变成卡住的状态,稍等。

然后把Data文件夹拉近工程里,一样的不选copy,但是folders选create folder references。

最终如图:

8、删除引用,先删除引用libraries里面的libil2cpp文件夹,然后再删除Classes里面的Native文件夹里面的所有.h文件(这个.h太多了我就没删,后来运行时不影响的,想删的话可以删了。)这里都是删除引用,不要move to trash。

9、修改unity里的方法,引用。
找到main.mm,替换这个方法
//int main(int argc, char* argv[])
//{
// @autoreleasepool
// {
// UnityInitTrampoline();
// UnityParseCommandLine(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:AppControllerClassName]);
// }
//
// return 0;
//}
int main_unity_default(int argc, char* argv[])
{
@autoreleasepool
{
UnityInitTrampoline();
UnityParseCommandLine(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:AppControllerClassName]);
// UIApplicationMain(argc, argv, nil, NSStringFromClass([UnitySubAppDelegate class]));
UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String:AppControllerClassName]);
}
return 0;
}
找到UnityAppController.h
添加:#import <UIKit/UIKit.h>、@class UnityViewControllerBase;

//注释此方法
//inline UnityAppController* GetAppController()
//{
// return (UnityAppController*)[UIApplication sharedApplication].delegate;
//}
//替换为此方法
NS_INLINE UnityAppController* GetAppController()
{
NSObject<UIApplicationDelegate>* delegate = [UIApplication sharedApplication].delegate;
UnityAppController* currentUnityController = (UnityAppController *)[delegate valueForKey:@"currentUnityController"];
return currentUnityController;
}

10、开始修改swift的工程。
找到appdelegate.swift,注释掉@UIApplicationMain,按图添加代码

新建一个main.swift文件,添加如下代码
//添加一个新的main.swift文件到工程里
//添加如下代码
import Foundation
import UIKit
// overriding @UIApplicationMain
custom_unity_init(CommandLine.argc, CommandLine.unsafeArgv)
UIApplicationMain(
CommandLine.argc,
UnsafeMutableRawPointer(CommandLine.unsafeArgv)
.bindMemory(
to: UnsafeMutablePointer<Int8>.self,
capacity: Int(CommandLine.argc)),
nil,
NSStringFromClass(AppDelegate.self)
)

11、添加依赖库、按图添加
dylib类型的库添加方法:
Build Phases -> Link Binary With Libraries —> + , 在对话框中点 Add Other, 然后在新的对话框用快捷键Command+Shift+G打开新的Folder对话框,输入/usr/lib/,然后点Go,在新弹出的dylib库目录选择libc++.dylib并添加。

12、在info.plist中加上相机权限:
Privacy - Camera Usage Description
步骤基本结束
bug fix、如果编译有问题、可按需尝试下边操作
- 如遇找不到UnityInterface.h:
在UnityBridge.h中修改UnityInterface.h的路径:(如果unityBridge与UnityInterface.h的Classes不在一个目录下,XXX/Classes/Unity/UnityInterface.h)
#import “Classes/Unity/UnityInterface.h”
(这个开始可以不动,报错了再尝试修改。)

2、如果找不到桥接文件
Unity.xcconfig中修改
SWIFT_OBJC_BRIDGING_HEADER = UnityFix/UnityBridge.h;
结语
按上面步骤融合完应该就可以了,但是我这边遇到一个问题,调起Unity的时候相机黑屏,其他的效果正常,就是没有相机成像。其实我就是抛砖引玉来的😂、求大神支招,后续解决了会更新上来。
2017.03.03更新
这段时间在做微信小程序、也没时间看、这几天忽然看到几个留言感觉不解决一下也不好了、就赶紧拿出来看看、
Q:之前留下的问题是相机黑屏没有画面
A:由于我们的Unity是在用EasyAR在做、所以需要修改的地方设计到EasyAR、不过我看论坛有人说其实黑屏的原理都差不多、
- 找到UnityAppController.mm文件、新增代码
extern "C" void ezarUnitySetGraphicsDevice(void* device, int deviceType, int eventType);
extern "C" void ezarUnityRenderEvent(int marker);

- (void)shouldAttachRenderDelegate {
//新增
UnityRegisterRenderingPlugin(&ezarUnitySetGraphicsDevice, &ezarUnityRenderEvent);
//新增
}

看网上说大部分黑屏问题可能出在shouldAttachRenderDelegate里、这里也是渲染的地方、要设置入口。