Unity集成到iOS本地工程中

集成步骤

所有的配置参考Unity导出来的工程的配置

1. iOS工程中新建Vendor group

2. 添加Unity的文件至iOS工程中

将Unity工程下的3个文件夹Classes、Libraries、Data添加至iOS工程中,添加时注意选择Copy items if needed选项,Classes和Libraries文件夹选择Create groups, 而Data文件夹选择Create folder references选项。

添加完毕以后可以把Unity文件夹中的一些无关文件清理掉,它们会影响编译速度,删除时选择Remove Reference

Classes->Native文件夹下的*.h文件

Libraries文件夹下的libil2cpp文件夹(可选)

3. 添加相关框架

在iOS工程中新建Frameworks group, 将需要添加的framework放在这个group下,至于需要添加哪些framework可以参见Unity工程的TARGETS -> General -> Linked Frameworks and Libraries,如图所示:

4. 配置Build Settings相关选项

(1) Enable Bitcode -> NO

(2) Other Linker Flags

添加-weak_framework CoreMotion -weak-lSystem


(3) Header Search Paths

添加以下选项:

"$(SRCROOT)"

"$(SRCROOT)/Unity5Native/Classes"

$(SRCROOT)/Unity5Native/Classes/Native

$(SRCROOT)/Unity5Native/Libraries/libil2cpp/include

$(SRCROOT)/Unity5Native/Libraries

注意:根据工程实际路径来配置,$(SRCROOT)是指.xcproj文件所在的路径,Classes和Libraries文件夹是在与Unity5Native.xcproj文件同级的Unity5Native文件夹下面的,所以SRCROOT下面要再添加Unity5Native层级。如果此处路径配置不正确,可能会出现诸如il2cpp-config.h file not found的编译错误。


(4)Library Search Paths

添加以下选项:

"$(SRCROOT)"

"$(SRCROOT)/Unity5Native/Libraries"

(5)Custom Compiler Flags -> Other C Flags

添加-DINIT_SCRIPTING_BACKEND=1

(6) C Language Dialect选择C99 [-std=c99]

(7) 新建pch文件:PrefixHeader.pch

(8) 设置Prefix Header的路径

根据pct文件所在路径实际设置,由于我是放在Supporting Filegroup组下面的,实际在与Unity5Native.xcproj文件同目录下的Unity5Native文件夹下。

(9)Precompile Prefix Header选择YES

(10) C++ Language Dialect选择C++11 [-std=c++11]

(11)Enable C++ Runtime Types选择NO

(12) Warnings-Objective C->Overriding Deprecated Objective-C Methods选择YES

(13) Warnings-Objective C->Unintentional Root Classes选择YES

(14) User-Defined

添加如下选项:

GCC_THUMB_SUPPORT->NO

GCC_USE_INDIRECT_FUNCTION_CALLS->NO

UNITY_RUNTIME_VERSION->5.5.2f1

UNITY_SCRIPTING_BACKEND->il2cpp

5. pch文件内容替换

将Classes文件夹下的Prefix.pch文件中的内容全部复制粘贴到PrefixHeader.pch文件的

之间,并且引入UnityAppController.h文件如图所示:



6. Supporting Files下面的main.m

重命名成main.mm

将Classes文件夹下的main.mm文件中的全部内容复制到Supporting Files文件夹下的main.mm文件中,并进行修改:

//const char* AppControllerClassName = @”UnityAppController”;const char* AppControllerClassName = “AppDelegate”;

Build Phases中移除Classes下的main.mm

7. UnityAppController.h

作如下修改:

//inline UnityAppController GetAppController()

//{

// return (UnityAppController)[UIApplication sharedApplication].delegate;

//}

#import "AppDelegate.h"                                         

inline UnityAppController*  GetAppController()                

 {                                                                

AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;                                     

 return delegate.unityController;                          

}                                                                            

8. AppDelegate.h和AppDelegate.m

AppDelegate.h文件作如下修改:

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) UIWindow *unityWindow;

@property (nonatomic, strong) UnityAppController              *unityController;

- (void)showUnityWindow;

- (void)hideUnityWindow;

@end

AppDelegate.m文件作如下修改:

#import “AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (UIWindow *)unityWindow {

return UnityGetMainWindow();

}

- (void)showUnityWindow {

[self.unityWindow makeKeyAndVisible];

}

- (void)hideUnityWindow {

[self.window makeKeyAndVisible];

}

- (BOOL)application:(UIApplication *)application              didFinishLaunchingWithOptions:(NSDictionary                  *)launchOptions {

// Override point for customization after application launch.

self.window.backgroundColor = [UIColor redColor];

self.unityController = [[UnityAppController alloc]        init];

[self.unityController application:application              didFinishLaunchingWithOptions:launchOptions];

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 throttle down OpenGL ES frame rates. 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 inactive 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

9. ViewController.m

作如下修改:

#import “ViewController.h"

#import “AppDelegate.h"

#import “UnityView.h”

@interface ViewController ()

@property (nonatomic, strong) UIButton *showUnityButton;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view,            typically from a nib.

self.view.backgroundColor = [UIColor blueColor];

self.showUnityButton = [UIButton                                        buttonWithType:UIButtonTypeSystem];

[self.showUnityButton setTitle:@"Show Unity”                    forState:UIControlStateNormal];

self.showUnityButton.frame = CGRectMake(0, 0, 100,          44);

self.showUnityButton.center = self.view.cent                      er;

[self.showUnityButton addTarget:self                          action:@selector(showUnityView:)                            forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:self.showUnityButton];

}

- (void)showUnityView:(id)sender {

[(AppDelegate *)[UIApplication                                                          sharedApplication].delegate showUnityWindow];

UIButton *btn = [UIButton                                buttonWithType:UIButtonTypeCustom];

btn.frame = CGRectMake(100, 200, 200, 50);

[btn setTitle:@"Exit" forState:UIControlStateNormal];

[btn setTintColor:[UIColor whiteColor]];

[btn setBackgroundColor:[UIColor blueColor]];

//[delegate.unityController.window addSubview:btn];

[delegate.unityController.unityView addSubview:btn];

[btn addTarget:self action:@selector(exitAR)                forControlEvents:UIControlEventTouchUpInside];

}                                                            - (void)exitAR

{

//调用Unity方法

//    void UnitySendMessage(constchar* obj, constchar*        method, constchar* msg);

UnitySendMessage("Manager", "End", "");

AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

[delegate hideUnityWindow];

}

@end

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

推荐阅读更多精彩内容