由于公司开发需要,需要与第三方对接unity3D方面的项目,对方提供unity集合成iOS项目。我这边需要把unity的项目融合到我的项目中。我自己也是看了很多博客文章以及自己多次配置踩坑,完成了融合,现在是做下总结。Xcode版本为11.3.1。iOS13.3 。
一、配置
1、首先复制unity项目中Classes、Data、Libraries、MapFileParser.sh文件到自己项目中的根目录中。如下图
unity项目.png 自己的项目.png2、将文件添加到项目工程中。⚠️data的添加方式
- Classes、Libraries、MapFileParser.sh添加
-
data的添加
folder references.png
3、添加framework,注意有些是Optional选择
添加framework.png添加libiconv.2.2dylib时,选择Add Other——Add Files——search输入框输入libiconv.2.2dylib添加即可,全Mac下搜索。
4、修改Header Search Paths和Library Search Paths
- Header Search Paths
"$(SRCROOT)"
"$(SRCROOT)/Classes"
$(SRCROOT)/Libraries/libil2cpp/include
$(SRCROOT)/Libraries/bdwgc/include
$(SRCROOT)/Classes/Native
$(SRCROOT)/Classes
Header Search Paths.png
- Library Search Paths
$(inherited)
$(PROJECT_DIR)/Libraries
"$(SRCROOT)/Libraries"
$(SRCROOT)
Library Search Paths.png
5、Other Link Flags
-weak_framework CoreMotion -weak-lSystem -licucore
Other Link Flags.png
6、Enable Bitcode 设置为NO
Enable Bitcode.png7、Prefix Header 和 Other C Flag设置
Other C Flag 设置为-DINIT_SCRIPTING_BACKEND=1 -DRUNTIME_IL2CPP=1
注意Prefix Header路径。由于header路径不同,你的header 路径填写的不一样,我是把prefix header 放到根目录下的Class文件下了。
image.png
8、修改Apple Clang - Language -> C Language Dialect选项,选择C99。修改Apple Clang - Language - C++ 中的 C Language Dialect 为C++11修改Enable C++ Runtime Types 为NO。
image.png9、添加自定义设置
image.png- GCC_THUMB_SUPPORT 为NO
- GCC_USE_INDIRECT_FUNCTION_CALLS 为NO
- UNITY_RUNTIME_VERSION 为unity版本
-
UNITY_SCRIPTING_BACKEND 为il2cpp
image.png
10、添加Run Script
"$PROJECT_DIR/MapFileParser.sh"
image.png
11、修改main.mm
在unity项目中Class文件中有main.mm文件,此时把main文件中代码全部复制到iOS项目中main.m文件。并将UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String: AppControllerClassName]);修改为 UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));,main.m命名为main.mm。此时有两个main.mm文件,删除Class文件的main.mm文件
12、修改prefix文件,添加#import "UnityAppController.h"
image.png13、修改UnityAppController.h文件
image.png修改的代码
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
return delegate.unityController;
14、修改AppDelegate.h和.m文件
AppDelegate.h文件如下
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@class UnityAppController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (readonly, strong) NSPersistentContainer *persistentContainer;
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong) UIWindow *unityWindow;
@property (nonatomic, strong) UnityAppController *unityController;
- (void)hideUnityWindow;
- (void)showUnityWindow;
- (void)saveContext;
AppDelegate.m文件
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
@synthesize window = _window;
- (UIWindow *)unityWindow{
return UnityGetMainWindow();
}
- (void)showUnityWindow{
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setTitle:@"退出Untity" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[backButton setBackgroundColor:[UIColor blackColor]];
[backButton setFrame:CGRectMake(self.unityWindow.frame.size.width-150, self.unityWindow.frame.size.height - 100, 100, 50)];
[backButton addTarget:self action:@selector(hideUnityWindow) forControlEvents:UIControlEventTouchUpInside];
[self.unityWindow addSubview:backButton];
UIButton *setBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[setBtn setTitle:@"切换戒指" forState:UIControlStateNormal];
[setBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[setBtn setBackgroundColor:[UIColor redColor]];
[setBtn setFrame:CGRectMake(self.unityWindow.frame.size.width-150, self.unityWindow.frame.size.height - 100, 100, 50)];
[setBtn addTarget:self action:@selector(switchRings:) forControlEvents:UIControlEventTouchUpInside];
[self.unityWindow addSubview:setBtn];
[self.unityWindow makeKeyAndVisible];
}
- (void)hideUnityWindow{
[self.window makeKeyAndVisible];
}
#pragma mark -切换戒指
-(void)switchRings:(UIButton *)btn{
//UnitySendMessage("UIRoot", "SwitchingRing", "B");
NSDictionary *dict = @{@"Size":@(9),@"Width":@(2)};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
const char *resultCString = NULL;
if ([str canBeConvertedToEncoding:NSUTF8StringEncoding]) {
resultCString = [str cStringUsingEncoding:NSUTF8StringEncoding];
}
UnitySendMessage("UIRoot", "SetFingerSize", resultCString);
}
- (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];
self.window.rootViewController = [[ViewController alloc]init];
self.unityController = [[UnityAppController alloc]init];
[self.unityController application:application didFinishLaunchingWithOptions:launchOptions];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
[self.unityController applicationWillResignActive:application];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
[self.unityController applicationDidEnterBackground:application];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[self.unityController applicationWillEnterForeground:application];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[self.unityController applicationDidBecomeActive:application];
}
- (void)applicationWillTerminate:(UIApplication *)application {
[self.unityController applicationWillTerminate:application];
}
此时算是配置完成。可以运行下看看项目。
二、结合时所遇到的问题
1、遇到系统Foundation中的NSobject报错,如图所示
此时是在prefix中,#import 写到了#ifdef _OBJC #endif外面。写在里面即可。
image.png
2、 image.png
此时run Script 选项第一个选上
2.png
3、如果系统中引用__weak 的报错,则把修改Apple Clang - Language -> C Language Dialect选项,选择GUN99
3.png
4、如果遇到这个错误,就是添加Framwork的时候少添加了MediaPlayer.framework。
4.png
5、如果出现XXXX.h文件找不到,就是Header Search Paths和Library Search Paths的路径没有设置好,自己对照下,是不是少添加了。忘记截图了,有点耐心对照下就OK。
6、提示项目was compiled with optimization - stepping may behave oddly; variables may not be available。
- UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String: AppControllerClassName]);修改为 UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));我第一次配置的时候,不知道这个情况,就出现的这种报错。
-
还有可能是unity打包的时候,unity打包设置Strip Engine Code没有选择(只是看有些博客这样写的。自己可以验证下)
image.png
其实配置的时候会有各种坑,自己配置的时候有些问题也忘记截图了,希望大家都能一次性配置成功。
最后再简单说下iOS和unity的交换,给unity发消息 UnitySendMessage("UIRoot", "SetFingerSize", resultCString);这些参数需要跟unity工程师那边商量的。
我也是初次接触unity配置。有什么不足之处欢迎指出。
网友评论