一、合并iOS工程和u3d工程
1靠柑、一個U3d導(dǎo)出的Xcode工程Unity-iPhone寨辩,單獨編譯運行OK
2、一個iOS工程ARHere歼冰,單獨編譯運行OK
3靡狞、打開終端cd 到Unity-iPhone文件夾,vim Podfile隔嫡,把ARHere的內(nèi)容復(fù)制粘貼甸怕,pod install --verbose --no-repo-update
4、打開Unity-iPhone文件夾中的Unity-iPhone.xcworkspace腮恩,不要急著做其他事情梢杭,先配置好
5、在build pathless 中的 link binary with libraries 中增加原iOS工程ARHere的庫文件(Unity-iPhone工程已經(jīng)有了的就不用加了秸滴,第三方.a的待會文件復(fù)制會自動帶過來)
->(Accelerate.framework ,AssetsLibrary.framework 這兩個是因為報了的Undefined symbols for architecture arm64 xxx錯誤才加武契,不一定非要)
6、在build setting 修改如下參數(shù)
Build Options —————> Enable BItcode ————> NO
Linking ——> Other Linker Flags———>$(inherited)
Search Paths ————> User Header Search Paths ——————> ${SRCROOT} 荡含, 改成遞歸模型(recursive)
如果出現(xiàn)這樣的錯誤的話試試這樣
d: warning: directory not found for option '-L"/Users/mlmk/Desktop/最新合并AR工程/10.31/Libraries"'
ld: library not found for -liPhone-lib
在Search Paths ——> Library searcher path 中將“$(SRCROOT)/Libraries” 把這個雙引號去除掉
像這樣的Undefined symbols for architecture arm64 xxx
Undefined symbols for architecture armv7:
就是缺少.framework庫文件支持啦咒唆,自己搞一搞
7、最后加入ARHere工程的文件释液,
(1)全释、對pct文件合并,之后保留一個误债,要U3d工程的就不需要修改pch引用路徑浸船。
(2)、對info.plist文件合并寝蹈,比如你用了微信第三方登錄等李命,就會有URL和像https訪問限制等其他配置,要保留進去
(3)箫老、項目的啟動圖LaunchScreen封字、運用圖標AppIcon在U3d導(dǎo)出的Xcode工程中的某個文件夾就有了,前提是你要在打包U3d Xcode工程中Player setting中設(shè)置了槽惫。
(4)周叮、main.m可以刪除了辩撑,如果是storyboard啟動的界斜,最好改成代碼啟動
(5)、對app delegate文件做繼承操作
#import <UIKit/UIKit.h>
#import "UnityAppController.h"
@interface UnitySubAppDelegate : UnityAppController
@end
#import "UnitySubAppDelegate.h"
#import "HomePageController.h"
IMPL_APP_CONTROLLER_SUBCLASS(UnitySubAppDelegate)// 指定啟動文件類
/**
* 如果你的U3d工程集成了EasyAR的SDK合冀,在Libraries/Plugins/IOS/下會有這么個文件
* EasyARAppController.mm各薇,它也是集成了UnityAppController的,所以你要把它內(nèi)容復(fù)制粘貼到你自己的app delegate文件中,然后干掉他
*/
extern "C" void ezarUnitySetGraphicsDevice(void* device, int deviceType, int eventType);
extern "C" void ezarUnityRenderEvent(int marker);
@interface UnitySubAppDelegate () {
HomePageController *_homeVC;
}
- (void)shouldAttachRenderDelegate;
@end
@implementation UnitySubAppDelegate
- (void)shouldAttachRenderDelegate;
{
UnityRegisterRenderingPlugin(&ezarUnitySetGraphicsDevice, &ezarUnityRenderEvent);
}
- (void)startUnity:(UIApplication *)application {
[super startUnity:application]; //先啟動unity
// 再啟動我們自己的界面峭判,如何如何這都得看你自己的需求
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
HomePageController *homeVC = [storyboard instantiateViewControllerWithIdentifier:@"homeVC"];
UIView *homeView = homeVC.view;
homeView.backgroundColor = [UIColor clearColor];
_homeVC = homeVC;
[UnityGetGLView() addSubview: homeView];
}
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[super application:application didFinishLaunchingWithOptions:launchOptions];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
[super applicationWillResignActive: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.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
[super applicationDidEnterBackground: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.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[super applicationWillEnterForeground: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.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[super applicationDidBecomeActive: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.
}
- (void)applicationWillTerminate:(UIApplication *)application {
[super applicationWillTerminate:application];
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
二开缎、后期U3d修改操作
因為平臺的原因每次U3d修改之后,又得重新打一個xcode工程包林螃,這樣調(diào)試效率著實很慢奕删,我沒有找到實時聯(lián)調(diào)的辦法,有的話就請留言告訴我一聲疗认。但替換還是很快很簡單的不需要像上面那么多步驟完残。
1、因為合并的環(huán)境已經(jīng)OK了横漏,再次合并只需要把最新的U3d工程的class谨设、data、Library替換掉就好了(當(dāng)然如果你在UnityAppController.mm做了修改缎浇,那就先替換這個文件啦)
2扎拣、如果合并不成功,各種系統(tǒng)報錯二蓝,搞了一會沒搞定亡笑,那就建議你重新合并一遍吧仑乌,不用太糾結(jié)啦,劃不來多少時間的晰甚。
3厕九、如果你換U3d版本或者xcode版本了扁远,替換又是各種系統(tǒng)問題,那還是建議你重新合并一遍吧畅买,不用太糾結(jié)啦谷羞,劃不來多少時間的。
三犀填、iOS和U3d的交互
(1)、合并之后會有很多交互的地方图贸,我的辦法是:凡是U3d調(diào)用iOS的方法全部放在一個類里面管理
#import <Foundation/Foundation.h>
@interface u3dRequest : NSObject
@end
#if defined(__cplusplus)
extern "C"{
#endif
extern void loadHomePage(); //無參無返回
extern void u3dRequestDataFromIos(const char *paraJson); // 有參無返回
extern const char* u3dGetAlbumPathFromios();// 有返回?zé)o參數(shù)
#if defined(__cplusplus)
}
#endif
#import "u3dRequest.h"
@implementation u3dRequest
@end
#ifdef __cplusplus
extern "C"{
#endif
void loadHomePage(){
UnitySubAppDelegate *appDelegate = (UnitySubAppDelegate *)[UIApplication sharedApplication].delegate;
UIView *homeView= [appDelegate.homeVC getHomeView];
[UnityGetGLView() addSubview:homeView ];
}
void u3dRequestDataFromIos(const char *paraJson) {
// 把格式化的JSON格式的字符串轉(zhuǎn)換成字典
NSString *jsonStr = [NSString stringWithUTF8String:paraJson];
NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSMutableDictionary *u3dParaDic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err];
if(err) {
NSLog(@"json解析失斍竺谩:%@",err);
return ;
}
NSString *typeFunction = u3dParaDic[@"mCmd"];
[u3dParaDic removeObjectForKey:@"mCmd"];
NSDictionary *params = [[SingleRequestParam sharedSingleton]requestWithDic:u3dParaDic];
NSString *url = [NSString stringWithFormat:@"%@%@",BaseURL,u3dParaDic[@"url"]];
[HYBNetworking postWithUrl:url refreshCache:YES params:params success:^(id response) {
NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
[resultDic setObject:typeFunction forKey:@"mCmd"];
[resultDic addEntriesFromDictionary:response];
NSError *parseError = nil;
NSData *resultJsonData = [NSJSONSerialization dataWithJSONObject:resultDic options:NSJSONWritingPrettyPrinted error:&parseError];
NSString *resultJsonString = [[NSString alloc] initWithData:resultJsonData encoding:NSUTF8StringEncoding];
DBLog(@"u3dRequestDataFromIos resultJsonString = %@",resultJsonString);
char * resultJson_char = (char *)malloc(strlen([resultJsonString UTF8String]) + 1);
strcpy(resultJson_char, [resultJsonString UTF8String]);
UnitySendMessage("PlatformSys", "ReceiveGameJson", resultJson_char);
} fail:^(NSError *error) {
NSMutableDictionary *errorDic = [NSMutableDictionary dictionary];
[errorDic setObject:typeFunction forKey:@"mCmd"];
[errorDic setObject:@"網(wǎng)絡(luò)連接錯誤!" forKey:@"errmsg"];
NSError *parseError = nil;
NSData *resultJsonData = [NSJSONSerialization dataWithJSONObject:errorDic options:NSJSONWritingPrettyPrinted error:&parseError];
NSString *errorJsonString = [[NSString alloc] initWithData:resultJsonData encoding:NSUTF8StringEncoding];
char * errorJson_char = (char *)malloc(strlen([errorJsonString UTF8String]) + 1);
strcpy(errorJson_char, [errorJsonString UTF8String]);
UnitySendMessage("PlatformSys", "ReceiveGameJson", errorJson_char);
NSLog(@"網(wǎng)絡(luò)連接錯誤净神!");
}];
}
const char* u3dGetAlbumPathFromios(){
//獲取沙盒路徑
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *imagePath=[NSString stringWithFormat:@"%@/MyAlbum",path];
// 判斷文件夾是否存在鹃唯,如果不存在瓣喊,則創(chuàng)建
if (![[NSFileManager defaultManager] fileExistsAtPath:imagePath]) {
[[NSFileManager defaultManager] createDirectoryAtPath:imagePath withIntermediateDirectories:YES attributes:nil error:nil];
} else {
NSLog(@"imagePath is exists.");
}
char * imagePath_char = (char *)malloc(strlen([imagePath UTF8String]) + 1);
strcpy(imagePath_char, [imagePath UTF8String]);
return imagePath_char;
}
#ifdef __cplusplus
}
#endif
(2)、iOS 調(diào)用U3d就超級簡單了洪橘,一句代碼熄求,放在你需要調(diào)用的任何地方
UnityPlayer.UnitySendMessage("Main Camera", "AgentPurchaseCancelled",msg);
參數(shù)一為unity腳本掛載的gameobject
參數(shù)二為unity腳本中要調(diào)用的方法名
參數(shù)三為傳遞的參數(shù)
這個方法是沒有返回值的
當(dāng)unity腳本中的方法為靜態(tài)方法時逗概,這個方法無效逾苫,所以只能調(diào)用非靜態(tài)的方法
(四)、合并后的截圖
最后的結(jié)果就是這樣子的啦
(1)瑟押、你的iOS工程最好搞成是真實文件夾對應(yīng)的勉耀,這樣一拉過來就行
(2)蹋偏、main.mm 用U3d工程的
(3)威始、Assets.xcassets 是原來iOS工程切圖黎棠,U3d工程的Images.xcassets主要是啟動圖和app圖標(如果啟動made with unity 圖,那是U3d自帶的脓斩,可以在player settings 勾選掉 貌似要破解版的或者付費版的)