iOS 原生项目集成unity项目
项目基本配置
拷贝unity项目中的文件到原生项目下
注:将项目添加到工程文件里面,不要引入到工程中
1.jpeg
将拷贝项目中文件添加到原生项目
将(Classes ,Libraries,MapFileParser.sh,Data)四个文件夹拖入到工程中
注:MapFileParser.sh可以不添加到工程中(没有亲试😁)
-
Classes,Libraries, MapFileParser.sh
选中拖入工程中
2.jpg
注意导入工程时的选项:
3.jpg
- 选中
Data
文件夹拖入工程中
4.jpg
截止到目前,我们的项目就整合完毕了!开个玩笑
配置环境
删除项目文件的.h文件和libil2cpp的引用(可以省略,删除了只是提高了编译的速度,貌似影响不大)
在iOS项目中,找到Classes->Native目录,将目录下的.h文件全部删除引用,注意只是.h,因为里面还有.cpp,这里只需要在Native文件夹上右键选择Sort by Type就可以把.h,.cpp分开了。
5.jpg
删除引用
6.jpg
找到Libraries->libil2cpp
,删除文件引用,同上面选择Remove References
配置项目工程
注:参考着unity项目中导出的工程进行配置,不同的项目导出的需求不一样
---
1 添加引用库(根据unity项目中导出的进行配置,引用他人的图😁)
8.jpg
注:项目中配置的时候有几个选择了Optional,但是当我们编译成功后,这几个文件会变成Required,具体的原因不清楚(跪求原因)
---
2 添加文件路径以及库的搜索路径(根据自己项目的设置去配置)
在Library Search Paths
下添加以下路径(针对不同的项目,可能需要修改,结合unity项目配置)
"$(SRCROOT)"
"$(SRCROOT)/Libraries"
$(SRCROOT)/Libraries/Plugins/iOS
在
Other Linker Flags
下添加以下关键字(一般是这几个,针对不同的项目去参考自己导入的unity工程):-weak_framework
CoreMotion
-weak-lSystem
12.jpg
---
3 c,c++,ObjC 环境设置
13.jpg
关于 C Languge Dialect
的选择
默认:GNU99[-std=gnu99]
修改:C99[-std=c99]
注:修改之后使用__weak typeof (self)weakSelf = self
编译报错,由于c语言编译器导致的
修改:Compiler Default
(好像可以但是未测试)
采用默认的,我们在编译项目时,在 UnityAppController.h 中我们设置的以下代码会出错
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
return delegate.unityController;
针对上面出现的问题,我们修改了写法如下可以正常运行:
return (UnityAppController *)[[UIApplication sharedApplication] valueForKeyPath:@"delegate.unityController"];
注:关于 C Language Dialect 介绍参考下文
http://www.it1352.com/351417.html
关闭 bitcode 选项(不设置貌似也没问题)
4 添加 User-Defined 设置
14.jpg
添加内容:
GCC_THUMB_SUPPORT
- >NO
GCC_USE_INDIRECT_FUNCTION_CALLS
->NO
UNITY_RUNTIME_VERSION
->(这里填写你的unity的版本)UNITY_SCRIPTING_BACKEND
->il2cpp
5添加 Run Script
(如果没有引入MapFileParser.sh
,不需要设置这一步,导入了不设置也没什么影响)
6 修改配置文件
(1)pch
文件
在Classes
文件夹下,找到prefix.pch
,如果 iOS 项目中已经创建了.pch
文件,那么我们直接复制Classes
文件夹下的prefix.pch
到我们项目中的.pch
文件中;如果项目本身没有创建.pch
文件,那么我们按照通常的思路创建好.pch
文件,设置好路径再把上述文件的内容copy过去。如图:
(2)
main
文件将unity项目中
Classes/
下的main.mm
文件复制,集成到iOS 项目中的main.m
文件中,然后删除Classes/
下的main.mm
,并且将iOS 项目中的main.m
的后缀名修改成.mm
。然后修改内容如下:17.jpg
(3)
AppDelegate
文件AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic)UIWindow *unityWindow;
@property (nonatomic,strong)UnityAppController *conter;
@property (nonatomic,strong)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];
UIButton *button = [[UIButton alloc] init];
button.backgroundColor = [UIColor redColor];
button.frame = CGRectMake(0, 0, 100, 44);
[button setTitle:@"返回" forState:UIControlStateNormal];
[self.unityWindow addSubview:button];
[button addTarget:self action:@selector(hideUnityWindow) forControlEvents:UIControlEventTouchUpInside];
}
- (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];
ViewController *viewController = [[ViewController alloc]init];
UINavigationController *nav =[[UINavigationController alloc] initWithRootViewController:viewController];
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.
[self.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.
[self.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.
[self.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.
[self.unityController applicationDidBecomeActive:application];
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
[self.unityController applicationWillTerminate:application];
}
@end
(4)UnityViewController
修改
Classes
文件夹下的UnityAppController.h
,修改如下:
// 项目原本的
//inline UnityAppController* GetAppController()
//{
// return (UnityAppController*)[UIApplication sharedApplication].delegate;
//}
#import "AppDelegate.h"
// 修改之后的
inline UnityAppController* GetAppController()
{
// 这种方法使用的前提是需要修改C Language Dialect选择C99 [-std=c99] ,但是使用这种情况在我们使用 __weak typeof (self)weakSelf = self;会编译报错,不能通过,
// AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
// return delegate.unityController;
// 所以采用下边这种形式,C Language Dialect选择GNU99[-std=gnu99],默认形式,再次情形下,当我们定义__weak typeof (self),可以正常使用
// 针对以上出现的问题参考 http://www.it1352.com/351417.html
return (UnityAppController *)[[UIApplication sharedApplication] valueForKeyPath:@"delegate.unityController"];
}
启动和退出unity界面(注:退出unity并不是真正的退出了,只是暂停了)
[(AppDelegate *)[UIApplication sharedApplication].delegate showUnityWindow];
// 传参数
// UnitySendMessage("", "", "");
// 不传参数
UnityPause(false);
编译可能报错以及解决办法
(某个文件报错)错误提示:control may reach end of non-void function
解决办法Mismatched Return Type -> NO
validateRenderPassDescriptor:750: failed assertion `Texture at depthAttachment has usage (0x01) which doesn’t specify MTLTextureUsageRenderTarget (0x04)’
解决办法:
方法一:修改Xcode配置
Product -> Scheme -> Edit Scheme -> Run -> Options -> GPU Frame Capture -> OpenGL ES
方法二: Unity导出iOS 工程时修改配置
Unity Other Setting -> Auto Graphics API
取消勾选 -> OpenGLES2
取消 Automatic Graphics API
,保留OpenGLES2.0
注:参考文章
1.https://www.jianshu.com/p/841e9e732322
2.https://blog.csdn.net/Elena_engineer/article/details/75969617