专栏首页iOS开发随笔Unity项目嵌入现有iOS项目的方法

Unity项目嵌入现有iOS项目的方法

因为现有项目有个功能需要调用Unity页面,所以就叫同事用Unity打一个包给我嵌入到现有iOS工程,就一个导入过程就非常曲折。(泪奔~~o(>_<)o ~~)

以下所有的文件结构和TARGETS配置都是参考Unity导出来的工程结构和配置,后面所有的删除都是删除引用。

1,首先将导出的包跑一下看看能不能跑起来(注意打出来的包是否支持模拟器运行),能跑起来才做下面的将Unity项目下面的四个文件复制到iOS项目的根目录

Unity打包出来的包

复制到iOS的项目后的目录如下:

复制到iOS项目后的目录结构

2,在自己iOS项目中引用这几个文件,但是引用跟我们平时引用的不一样,右键Add Files to ···分别选择ClassesLibrariesMapFileParser.sh,在Options里面勾选 Create groups,不要选Copy items if needed

引用文件

2.1,剩下的Data文件,右键Add Files to ···,在Options里面勾选 Create folder references,不要选Copy items if needed

Data导入

完成之后的文件夹目录如下:

目录

2.2,接下来删除多余的引用:

Classes->Native目录,将目录下的.h文件全部删除(注意:只删除引用,而且只有.h,因为里面还有.cpp,据说Unity2017不用删除,但是我还没有测试成功)

2.2

2.3,再删除Libraries->libil2cpp,这个文件的引用,同上面操作步骤一样的

2.3

3,对iOS工程环境的配置,这里的配置都是以Unity的配置为参考

3.1,添加应用库

3.1

3.2,添加头文件和库的搜索路径

3.2

每一个项目的配置和路径不一样,这里只是参考,一切以你Unity项目的配置为准

2.41

3.3,其它一些配置

3.3.1

3.3.2

3.3.3

3.3.4

3.3.5

3.3.6

上面注意:如果自己的iOS项目有pch文件,那就把Prefix.pch里面的文件拷贝到自己的pch文件中,反之也行(注意pch路径),并添加#import"UnityAppController.h"

pch文件

3.3.7

3.4,添加用户定义的设置(这个图是我用Unity2017来试的时候截的图,但是失败了,5.6可以)

3.4

4,修改main.m文件

Classes/文件夹里面的main.mm里面的代码,拷贝到Supporting Files/下的main.m文件中,并把后缀改成.mm,修改如下图

4

然后删除Classes目录下单main.mm文件。注意:一样是删除引用 到了这里基本集成完毕了~~~

5,修改AppDelegate 文件

AppDelegate.h文件

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UnityAppController * unityController;
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIWindow * unityWindow;

- (void)showUnityWindow;
- (void)hideUnityWindow;


@end

AppDelegate.m文件

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

@interface AppDelegate ()

@end

@implementation AppDelegate

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

- (void)showUnityWindow {
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn setTitle:@"返回iOS世界" forState:UIControlStateNormal];
    [btn setFrame:CGRectMake(100, 100, 100, 40)];
    [btn addTarget:self action:@selector(hideUnityWindow) forControlEvents:UIControlEventTouchUpInside];
    [self.unityWindow addSubview:btn];
    
    [self.unityWindow makeKeyAndVisible];
}

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


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    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.unityController = [[UnityAppController alloc] init];
    [self.unityController application:application didFinishLaunchingWithOptions:launchOptions];
    [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 invalidate graphics rendering callbacks. 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 active 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

6,修改UnityAppController.h文件

#import "AppDelegate.h"

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

7,启动Unity界面

进入Unity界面

[(AppDelegate *)[UIApplication sharedApplication].delegate showUnityWindow];
UnityPause(false);

跳出Unity界面

[(AppDelegate*)[UIApplication sharedApplication].delegate  hideUnityWindow];
UnityPause(true);

8,一些报错汇总:

1,报错:libil2cpp/include/codegen/il2cpp-codegen.h:368:1: Control may reach end of non-void function 
添加 return NULL;
2,编译时遇到Permission denied错误的是因为当前开发账号对项目目录没有权限执行MapFileParser.sh
解决方法:chmod +x   /Users/......./MapFileParser.sh (MapFileParser.sh所在的目录)
3,clang: error: no such file or directory: 'CoreMotion',注意Other Linker Flags的导入的顺序
4,加了extern "c",却不认识,一直报少“(”,在Build Settings选项中找到Compile Sources As这项设置成Objective-C++ 就解决问题了 

最后感谢这两位作者的引导文章: Unity(2017版本)嵌入现有iOS工程 unity与iOS合并 iOS (Swift)集成 Unity步骤和各种填坑。

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 20161216微信支付 全程配置

    傅_hc
  • HTML- white-space 和 overflow 两个重要的 CSS 属性

    CSS的作用是给 HTML 元素添加样式和布局,接下来利用CSS的 white-space 和 overflow 这两个属性让元素实现左右滑动,效果如下:

    傅_hc
  • IQKeyboardManager每次运行都会断点到Bug ID:#550

    傅_hc
  • iOS开发系列——第一个iOS程序和运行过程

    这里我想强调一下,前面的知识是你日后开发IOS的基础,没有那些知识你开发IOS会很痛苦,现在很多开发人员做开发都是一知半解,程序质量确实令人担忧,所以还是希望大...

    Kris大鲨
  • BART原理简介与代码实战

    最近huggingface的transformer库,增加了BART模型,Bart是该库中最早的Seq2Seq模型之一,在文本生成任务,例如摘要抽取方面达到了S...

    kaiyuan
  • 5篇生成模型相关 paper

    Bayesian GAN Yunus Saatchi Andrew Gordon Wilson

    用户1908973
  • Introduction to Model Driven Development with AndroMDA

    Introduction AndroMDA (pronounced "Andromeda") is a free and open source extensi...

    张善友
  • Miguel de Icaza 细说 Mix 07大会上的Silverlight和DLR

    Mono之父Miguel de Icaza 详细报道微软Mix 07大会上的Silverlight和DLR ,上面还谈到了Mono and Silverligh...

    张善友
  • Linux系统内部的名称解析与安全认证(原创)

    我们都知道计算机最喜欢的是数字,而人类喜欢的是语言,所以我们在计算机上运行的进程、定义的用户、端口号、协议、ip地址等都需要转换成数字的形式让计算机明白,在Li...

    用户2645267
  • 【论文推荐】最新6篇目标跟踪相关论文—动态记忆网络、相关滤波器、单次学习、相关、循环自回归网络、三维多目标

    【导读】专知内容组整理了最近六篇目标跟踪(Object Tracking)相关文章,为大家进行介绍,欢迎查看! 1.Learning Dynamic Memor...

    WZEARW

扫码关注云+社区

领取腾讯云代金券