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 条评论
登录 后参与评论

相关文章

来自专栏一“技”之长

Swift学习第二练——Swift项目时光电影

    很早以前的一个OC的练习项目,用swift重新写了一遍,因为xcode版本的更新对swift的兼容度也在不断改变,此版本适用于xcode6.1。

10050
来自专栏xx_Cc的学习总结专栏

iOS-网络编程(二)文件上传和断点离线下载

42380
来自专栏xx_Cc的学习总结专栏

iOS-控制器View的创建和生命周期

35360
来自专栏.NET开发那点事

angularjs的表单验证

angularjs内置了常用的表单验证指令,比如min,require等。下面是演示: <!DOCTYPE html> <html> <head> ...

19960
来自专栏Rindew的iOS技术分享

iOS初步集成极光推送后你还要做这些事

37050
来自专栏娱乐心理测试

iOS 如何把图片资源打包成bundle文件及遇到的坑(详解)

(1.)"Base SDK" 设置为 "Latest iOS (iOS 11.2)" (Xcode 9.2为例)

33220
来自专栏清墨_iOS分享

iOS多个网络请求完成后执行下一步

在开发中,我们很容易遇到这样的需求,需要我们同时做多个网络请求,所有网络请求都完成后才能进行下一步的操作。如下载多个图片,下载完了才能展示。 今天我们就来研究一...

56970
来自专栏.NET开发那点事

angularjs的表单验证

angularjs内置了常用的表单验证指令,比如min,require等。下面是演示:

7800
来自专栏iOS 开发杂谈

App的生命周期

注意: application:didFinishLaunchingWithOptions:: App 首次启动时调用,一般在这个函数里创建 window 对...

24410
来自专栏進无尽的文章

基础篇-应用之间的跳转

在应用A跳转到应用B,则给A、B自身自定义URL Schemes(自定义的协议头)后,通过在A中处理B的URL Schemes,就可以在A中启动B了。

16810

扫码关注云+社区

领取腾讯云代金券