iOS 原生项目集成unity项目


项目基本配置


拷贝unity项目中的文件到原生项目下

注:将项目添加到工程文件里面,不要引入到工程中
1.jpeg

将拷贝项目中文件添加到原生项目

将(Classes ,Libraries,MapFileParser.sh,Data)四个文件夹拖入到工程中
注:MapFileParser.sh可以不添加到工程中(没有亲试😁)
  1. Classes,Libraries, MapFileParser.sh选中拖入工程中
    2.jpg
注意导入工程时的选项:
3.jpg
  1. 选中 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

7.jpg

配置项目工程

注:参考着unity项目中导出的工程进行配置,不同的项目导出的需求不一样

---
1 添加引用库(根据unity项目中导出的进行配置,引用他人的图😁)
8.jpg

注:项目中配置的时候有几个选择了Optional,但是当我们编译成功后,这几个文件会变成Required,具体的原因不清楚(跪求原因)

---
2 添加文件路径以及库的搜索路径(根据自己项目的设置去配置)

Library Search Paths下添加以下路径(针对不同的项目,可能需要修改,结合unity项目配置)
"$(SRCROOT)"
"$(SRCROOT)/Libraries"
$(SRCROOT)/Libraries/Plugins/iOS

11.jpg

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,不需要设置这一步,导入了不设置也没什么影响)

15.jpg


6 修改配置文件
(1)pch 文件
Classes文件夹下,找到prefix.pch,如果 iOS 项目中已经创建了.pch文件,那么我们直接复制Classes文件夹下的prefix.pch到我们项目中的.pch文件中;如果项目本身没有创建.pch文件,那么我们按照通常的思路创建好.pch文件,设置好路径再把上述文件的内容copy过去。如图:

16.jpg

(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

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

推荐阅读更多精彩内容