項(xiàng)目基本配置
拷貝unity項(xiàng)目中的文件到原生項(xiàng)目下
注:將項(xiàng)目添加到工程文件里面,不要引入到工程中

將拷貝項(xiàng)目中文件添加到原生項(xiàng)目
將(Classes ,Libraries,MapFileParser.sh,Data)四個(gè)文件夾拖入到工程中
注:MapFileParser.sh可以不添加到工程中(沒(méi)有親試??)
-
Classes,Libraries, MapFileParser.sh選中拖入工程中
2.jpg
注意導(dǎo)入工程時(shí)的選項(xiàng):

- 選中
Data文件夾拖入工程中
4.jpg
截止到目前,我們的項(xiàng)目就整合完畢了!開(kāi)個(gè)玩笑
配置環(huán)境
刪除項(xiàng)目文件的.h文件和libil2cpp的引用(可以省略,刪除了只是提高了編譯的速度,貌似影響不大)
在iOS項(xiàng)目中,找到Classes->Native目錄,將目錄下的.h文件全部刪除引用,注意只是.h,因?yàn)槔锩孢€有.cpp,這里只需要在Native文件夾上右鍵選擇Sort by Type就可以把.h,.cpp分開(kāi)了。

刪除引用

找到Libraries->libil2cpp,刪除文件引用,同上面選擇Remove References

配置項(xiàng)目工程
注:參考著unity項(xiàng)目中導(dǎo)出的工程進(jìn)行配置,不同的項(xiàng)目導(dǎo)出的需求不一樣
---
1 添加引用庫(kù)(根據(jù)unity項(xiàng)目中導(dǎo)出的進(jìn)行配置,引用他人的圖??)

注:項(xiàng)目中配置的時(shí)候有幾個(gè)選擇了Optional,但是當(dāng)我們編譯成功后,這幾個(gè)文件會(huì)變成Required,具體的原因不清楚(跪求原因)
---
2 添加文件路徑以及庫(kù)的搜索路徑(根據(jù)自己項(xiàng)目的設(shè)置去配置)
在Library Search Paths下添加以下路徑(針對(duì)不同的項(xiàng)目,可能需要修改,結(jié)合unity項(xiàng)目配置)
"$(SRCROOT)"
"$(SRCROOT)/Libraries"
$(SRCROOT)/Libraries/Plugins/iOS

在
Other Linker Flags 下添加以下關(guān)鍵字(一般是這幾個(gè),針對(duì)不同的項(xiàng)目去參考自己導(dǎo)入的unity工程):-weak_frameworkCoreMotion-weak-lSystem
---
3 c,c++,ObjC 環(huán)境設(shè)置

關(guān)于 C Languge Dialect的選擇
默認(rèn):GNU99[-std=gnu99]
修改:C99[-std=c99] 注:修改之后使用__weak typeof (self)weakSelf = self編譯報(bào)錯(cuò),由于c語(yǔ)言編譯器導(dǎo)致的
修改:Compiler Default (好像可以但是未測(cè)試)
采用默認(rèn)的,我們?cè)诰幾g項(xiàng)目時(shí),在 UnityAppController.h 中我們?cè)O(shè)置的以下代碼會(huì)出錯(cuò)
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
return delegate.unityController;
針對(duì)上面出現(xiàn)的問(wèn)題,我們修改了寫(xiě)法如下可以正常運(yùn)行:
return (UnityAppController *)[[UIApplication sharedApplication] valueForKeyPath:@"delegate.unityController"];
注:關(guān)于 C Language Dialect 介紹參考下文
http://www.it1352.com/351417.html
關(guān)閉 bitcode 選項(xiàng)(不設(shè)置貌似也沒(méi)問(wèn)題)
4 添加 User-Defined 設(shè)置

添加內(nèi)容:
GCC_THUMB_SUPPORT - >NOGCC_USE_INDIRECT_FUNCTION_CALLS->NOUNITY_RUNTIME_VERSION->(這里填寫(xiě)你的unity的版本)UNITY_SCRIPTING_BACKEND->il2cpp
5添加 Run Script(如果沒(méi)有引入MapFileParser.sh,不需要設(shè)置這一步,導(dǎo)入了不設(shè)置也沒(méi)什么影響)

6 修改配置文件
(1)pch 文件
在Classes文件夾下,找到prefix.pch,如果 iOS 項(xiàng)目中已經(jīng)創(chuàng)建了.pch文件,那么我們直接復(fù)制Classes文件夾下的prefix.pch到我們項(xiàng)目中的.pch文件中;如果項(xiàng)目本身沒(méi)有創(chuàng)建.pch文件,那么我們按照通常的思路創(chuàng)建好.pch文件,設(shè)置好路徑再把上述文件的內(nèi)容copy過(guò)去。如圖:

(2)
main文件將unity項(xiàng)目中
Classes/下的main.mm文件復(fù)制,集成到iOS 項(xiàng)目中的main.m文件中,然后刪除Classes/下的main.mm,并且將iOS 項(xiàng)目中的main.m的后綴名修改成.mm。然后修改內(nèi)容如下:
(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,修改如下:
// 項(xiàng)目原本的
//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;會(huì)編譯報(bào)錯(cuò),不能通過(guò),
// AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
// return delegate.unityController;
// 所以采用下邊這種形式,C Language Dialect選擇GNU99[-std=gnu99],默認(rèn)形式,再次情形下,當(dāng)我們定義__weak typeof (self),可以正常使用
// 針對(duì)以上出現(xiàn)的問(wèn)題參考 http://www.it1352.com/351417.html
return (UnityAppController *)[[UIApplication sharedApplication] valueForKeyPath:@"delegate.unityController"];
}
啟動(dòng)和退出unity界面(注:退出unity并不是真正的退出了,只是暫停了)
[(AppDelegate *)[UIApplication sharedApplication].delegate showUnityWindow];
// 傳參數(shù)
// UnitySendMessage("", "", "");
// 不傳參數(shù)
UnityPause(false);
編譯可能報(bào)錯(cuò)以及解決辦法
(某個(gè)文件報(bào)錯(cuò))錯(cuò)誤提示: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導(dǎo)出iOS 工程時(shí)修改配置
Unity Other Setting -> Auto Graphics API 取消勾選 -> OpenGLES2
取消 Automatic Graphics API,保留OpenGLES2.0
注:參考文章
1.http://www.itdecent.cn/p/841e9e732322
2.https://blog.csdn.net/Elena_engineer/article/details/75969617

