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文件夾中的一些無關(guān)文件清理掉,它們會影響編譯速度,刪除時選擇Remove Reference

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

Libraries文件夾下的libil2cpp文件夾(可選)

3. 添加相關(guān)框架

在iOS工程中新建Frameworks group, 將需要添加的framework放在這個group下,至于需要添加哪些framework可以參見Unity工程的TARGETS -> General -> Linked Frameworks and Libraries,如圖所示:

4. 配置Build Settings相關(guān)選項

(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

注意:根據(jù)工程實際路徑來配置,$(SRCROOT)是指.xcproj文件所在的路徑,Classes和Libraries文件夾是在與Unity5Native.xcproj文件同級的Unity5Native文件夾下面的,所以SRCROOT下面要再添加Unity5Native層級。如果此處路徑配置不正確,可能會出現(xiàn)諸如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) 設(shè)置Prefix Header的路徑

根據(jù)pct文件所在路徑實際設(shè)置,由于我是放在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文件內(nèi)容替換

將Classes文件夾下的Prefix.pch文件中的內(nèi)容全部復制粘貼到PrefixHeader.pch文件的

之間,并且引入UnityAppController.h文件如圖所示:



6. Supporting Files下面的main.m

重命名成main.mm

將Classes文件夾下的main.mm文件中的全部內(nèi)容復制到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

{

//調(diào)用Unity方法

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

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

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

[delegate hideUnityWindow];

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容