Unity项目集成到现有iOS项目

Unity项目集成到现有iOS项目

1.将Unity项目文件复制到到iOS项目跟目录下

主要有5个文件,ClassesDataLibrariesMapFileParserMapFileParser.sh,(前三个是文件夹)。

Unity集成_复制文件.png

2.在iOS项目中添加文件引用

(1)对Classes,Libraries,MapFileParser.sh添加

注意这里是在app的根目录下,右键“Add File to ...”,选中三个文件(文件夹),记住在options里面勾选“Create groups”“Copy items if needed”就不用选中了。

Unity集成_添加文件1.png

(2) 对Data添加

同样是在app的根目录下,右键添加文件,选中Data文件夹,但是这里的options,是选“Create folder references”,记住了,别选错。

Unity集成_添加文件2.png

完成这两步之后,项目工程应该是这样子。


Unity集成_添加文件最终版.png

3.删除项目文件.h文件,跟libil2cpp的引用

(1)

在iOS项目中,找到Classes->Native目录,将目录下的.h文件全部删除引用,注意只是.h,因为里面还有.cpp,这里只需要在Native文件夹上右键选择Sort by Type就可以把.h.cpp分开了。

Unity集成_删除.h文件路径.png

。这里只是删除引用,选择这里面估计有几千个文件慢慢等一下。
Unity集成_删除.h引用.png

(2)

找到Libraries->libil2cpp,删除文件引用,同上面选择Remove References

Unity集成_删除libil2cpp引用.png

4.对工程TARGETS进行配置。

note:这里的全部配置,其实都可以参考Unity导出的项目去配置的

(1)添加引用库

Unity集成_添加引用库.png

注意有3个是选择optional的。

(2)添加头文件,库搜索路径

在项目TARGETS->Build Settings搜索Search。

Header Search Paths下添加以下路径

"$(SRCROOT)/Classes"

"$(SRCROOT)"

$(SRCROOT)/Classes/Native

$(SRCROOT)/Libraries/bdwgc/include

$(SRCROOT)/Libraries/libil2cpp/include

Unity集成_Header路径添加.png

Library Search Paths下添加以下路径

"$(SRCROOT)"

"$(SRCROOT)/Libraries"

$(SRCROOT)/Libraries/Plugins/iOS

Unity集成_Library路径添加.png

(3)配置Other Linker Flags

Other Linker Flags下添加以下关键字:

-weak_framework

CoreMotion

-weak-lSystem

Unity集成_OtherLinker.png

Note:当项目中集成了多个第三方库的时候,例如本次项目,需要集成百度地图SDK,两个库之间的文件、文件内容可能有重复,这时候就会出现duplicate symbol的警报后来我找到了篇文章去解决。iOS 解决一个因三方静态库冲突产生的duplicate symbol的问题

(4)C,C++,ObjC环境设置

Unity集成_OtherCFlags.png
Unity集成_Language1.png

这里的Prefix Header选择自己项目的.pch文件距离,具体设置可以百度搜索。

Unity集成_language2.png
Unity集成_EnableBitcode.png

(5)添加User-Defined设置

根据图示


Unity集成_UserDefined.png

分别添加键值对。

GCC_THUMB_SUPPORT -> NO

GCC_USE_INDIRECT_FUNCTION_CALLS->NO

UNITY_RUNTIME_VERSION->5.4.2f2

UNITY_SCRIPTING_BACKEND->il2cpp

(6)添加Run Script

Build Phases页面下左上角的加号,选择New Run Script Phase,然后在Run Script进行配置.

Unity集成_RunScript.png

Note:再说一遍,以上的所有项目TARGETS的配置,都可以在Unity导出的Xcode项目中参照着修改配置!!!

5.文件配置和修改

(1)pch文件

Classes文件夹下,可以找到Prefix.pch文件。如果你有自己的pch文件,那就可以把这个文件里面的全部内容复制到自己的.pch文件里面,如果你要使用它也可以,那就在上面设置设置Prefix Header的时候设置好使用的文件路径就好。

Unity集成_Prefix配置.png

(2)修改main文件

Classes/下的main.mm文件里面的内容,全部复制到集成项目的Supporting Files/下的main.m文件中,然后删除Classes/main.mm,并且把文件后缀改成.mm,。并且按照下图对内容进行修改

Unity集成_main修改.png

(3)AppDelegate修改

AppDelegate.h文件修改

#import <UIKit/UIKit.h>
#import "UnityAppController.h"
@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.m文件修改

#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


-(UIWindow *)unityWindow{
    return  UnityGetMainWindow();
}

-(void)showUnityWindow{
    [self.unityWindow makeKeyAndVisible];
}

-(void)hideUnityWindow{
     [self.window makeKeyAndVisible];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

    _unityController = [[UnityAppController alloc] init];
    [_unityController application:application didFinishLaunchingWithOptions:launchOptions];

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];


    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];
    self.window.rootViewController = nav;


    [self.window makeKeyAndVisible];


    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

    [_unityController applicationWillResignActive:application];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

    [_unityController applicationDidEnterBackground:application];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

    [_unityController applicationWillEnterForeground:application];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    [_unityController applicationDidBecomeActive:application];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

    [_unityController applicationWillTerminate:application];
}

@end

(4)UnityViewController修改

(1)

Classes/目录下找到UnityAppController.h文件,按照以下修改

Unity集成_UnityAppController修改.png

inline UnityAppController*  GetAppController()
{
    return (UnityAppController*)[UIApplication sharedApplication].delegate;
}

修改成

#import "AppDelegate.h"
inline UnityAppController*  GetAppController()
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    return delegate.unityController;
}
(2)如果有Main.storyboard就把它移除。

6.启动和隐藏Unity界面

 //进入unity界面
[(AppDelegate *)[UIApplication sharedApplication].delegate showUnityWindow];
UnityPause(false);

//推出Unity界面
[(AppDelegate *)[UIApplication sharedApplication].delegate hideUnityWindow];
UnityPause(true);

7.问题收集

(1)在使用Unity摄像头的时候黑屏

问题表现为,在使用Unity打包出来的项目运行是正常的,但是集合到现有项目的时候,摄像头显示区域是黑屏。错误码是:EasyAR is running on an unsupported graphics device of type -4
解决的方法:

1.在UnityAppController.h中添加以下两个方法

extern "C" void ezarUnitySetGraphicsDevice(void* device, int deviceType, int eventType);
extern "C" void ezarUnityRenderEvent(int marker);
UnityAppController.h.png

2.在UnityAppController.mm中添加这两个方法

UnityAppController.mm.png

3.在UnityAppController.mm实现以下方法
- (void)shouldAttachRenderDelegate
{
UnityRegisterRenderingPlugin(&ezarUnitySetGraphicsDevice, &ezarUnityRenderEvent);
}

UnityAppController.mm.png

(2)按照上文启动Unity的窗口,后来我发现,这个Unity的窗口其实在你主app窗口下面一直运行,知道你第一次使用暂停,这里会使得手机发烫,一直占用资源。

解决方法:
我在UnityAppController.mm- (void)startUnity:(UIApplication*)application最后添加了UnityPause(true);使得Unity在初始化完成之际,马上停止界面,稍微对情况有所改善,如果有更好的方法,希望在下面留言。

UnityAppController.mm.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,290评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,399评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,021评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,034评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,412评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,651评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,902评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,605评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,339评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,586评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,076评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,400评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,060评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,851评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,685评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,595评论 2 270

推荐阅读更多精彩内容