iOS项目与unity融合配置

由于公司开发需要,需要与第三方对接unity3D方面的项目,对方提供unity集合成iOS项目。我这边需要把unity的项目融合到我的项目中。我自己也是看了很多博客文章以及自己多次配置踩坑,完成了融合,现在是做下总结。Xcode版本为11.3.1。iOS13.3 。

一、配置

1、首先复制unity项目中Classes、Data、Libraries、MapFileParser.sh文件到自己项目中的根目录中。如下图
unity项目.png
自己的项目.png
2、将文件添加到项目工程中。⚠️data的添加方式
  • Classes、Libraries、MapFileParser.sh添加
group方式.png
  • 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.png
7、Prefix Header 和 Other C Flag设置

Other C Flag 设置为-DINIT_SCRIPTING_BACKEND=1 -DRUNTIME_IL2CPP=1

image.png

注意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.png
9、添加自定义设置
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文件

image.png

12、修改prefix文件,添加#import "UnityAppController.h"
image.png
13、修改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报错,如图所示

image.png

此时是在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配置。有什么不足之处欢迎指出。

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