我已经遵循了http://www.the-nerd.be/2015/11/13/integrate-unity-5-in-a-native-ios-app-with-xcode-7/#comment-446教程,这对于在没有Vuforia的情况下集成Unity非常有用.
那么我怎样才能做到这一点,并且使用swift做得更好
解决方法 iOS和8.4及更高版本的Unity 5.3.8p8和Vuforia 6.0.117集成.这些教程是针对Objective-C编写的,但你可以毫无问题地为Swift做,只需创建一个PREFIX header并导入所有.h文件,并在我的示例中替换正确的代码.
导出Unity项目的XCode后,打开它并创建一个组文件夹.
在你创建的文件夹中,创建一个带有你想要名字的.mm文件(我的是mainAppController.mm),有:
// mainAppController.mm//// import this default headers to make Unity and Vuforia works#import <UIKit/UIKit.h>#import "UnityAppController.h"#import "UI/UnityVIEw.h"#import "UI/UnityVIEwControllerBase.h"#import "VuforiaRenderDelegate.h"// This is your MAIN VIEWCONTRolLER,that controller you want to open first when build/open your app.#import "MainVIEwController.h"// Unity native rendering callback plugin mechanism is only supported// from version 4.5 onwards#if UNITY_VERSION>434// Exported methods for native rendering callbackextern "C" voID UnitySetGraphicsDevice(voID* device,int deviceType,int eventType);extern "C" voID UnityRenderEvent(int marker);// This is for Vuforia Render Delegate,i copy it from VuforiaNativeRenderController.mm and add here to make it workextern "C" voID VuforiaSetGraphicsDevice(voID* device,int eventType);extern "C" voID VuforiaRenderEvent(int marker);#endif@interface mainAppController : UnityAppController<UIApplicationDelegate>// My historyboard works with NavigationController.// If your app doenst use navigation,just open the historiboard with your main VIEwController.@property (nonatomic,strong) UINavigationController *navigationController;- (voID)willStartWithVIEwController:(UIVIEwController*)controller;- (voID)shouldAttachRenderDelegate;@end@implementation mainAppController- (voID)shouldAttachRenderDelegate{ self.renderDelegate = [[VuforiaRenderDelegate alloc] init]; // Unity native rendering callback plugin mechanism is only supported // from version 4.5 onwards#if UNITY_VERSION>434 // // I comment this line bellow because Vuforia hendle it,and you see what will work with Vuforia. // //UnityRegisterRenderingPlugin(&UnitySetGraphicsDevice,&UnityRenderEvent); UnityRegisterRenderingPlugin(&VuforiaSetGraphicsDevice,&VuforiaRenderEvent);#endif}- (voID)willStartWithVIEwController:(UIVIEwController*)controller { // // Open your historyboard with your main vIEw. // In my case i use navigation controller. UIStoryboard *storyBoard; storyBoard = [UIStoryboard storyboarDWithname:@"Main" bundle:nil]; _rootController = [[UnitydefaultviewController alloc] init]; _rootVIEw = [[UIVIEw alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; _rootController.vIEw = _rootVIEw; MainVIEwController *mainVC = [storyBoard instantiateVIEwControllerWithIDentifIEr:@"IDMainVIEwController"]; self.navigationController = [[UINavigationController alloc] initWithRootVIEwController:mainVC]; [_rootVIEw addSubvIEw:self.navigationController.vIEw];}@end//// You have to put this line below and comment out the equal line below in file VuforiaNativeRenderController.mm//IMPL_APP_CONTRolLER_SUBCLASS(mainAppController)
你注意到我正在使用Storyboard.
所以我的MainVIEwController是导航控制器的VIEwController根.对!
在我的MainVIEwController中,我这样做:
// MainVIEwController.h//#import <UIKit/UIKit.h>#import "UnityAppController.h"#import "UI/UnityVIEw.h"#import "UI/UnityVIEwControllerBase.h"@interface MainVIEwController : UIVIEwController{ UnitydefaultviewController *unityVIEwController; UnityAppController *unityController;}-(IBAction) touchToload:(ID)sender;@end
// MainVIEwController.m//// This is just a EXAMPLE file,that i use in my project.#import "MainVIEwController.h"#import "ARVIEwController.h"@interface MainVIEwController ()@end@implementation MainVIEwController- (ID)initWithNibname:(Nsstring *)nibnameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibname:nibnameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (voID)vIEwDIDLoad{ [super vIEwDIDLoad]; // My project use navigation controller just for Transition animation right to left,thats why i hIDe it here on first vIEw. [self.navigationController setNavigationbarHIDden:YES];}- (voID)dIDReceiveMemoryWarning{ [super dIDReceiveMemoryWarning]; // dispose of any resources that can be recreated.}#pragma mark - Navigation// In a storyboard-based application,you will often want to do a little preparation before navigation//- (voID)prepareForSegue:(UIStoryboardSegue *)segue sender:(ID)sender//{// if ([segue.IDentifIEr isEqualToString:@"IDHomeVIEwController"])// {//// MyVIEwController *controller = (MyVIEwController *)segue.destinationVIEwController;//// controller.myProperty1 = ...;//// controller.myProperty2 = ...;// }// // //}-(voID)touchToload:(ID)sender{ // // Open historyboard with Unity and Vuforia,see details on ARVIEwController.h/m UIStoryboard *storyBoard; storyBoard = [UIStoryboard storyboarDWithname:@"Main" bundle:nil]; ARVIEwController *mainVC = [storyBoard instantiateVIEwControllerWithIDentifIEr:@"IDARVIEwController"]; [self.navigationController pushVIEwController:mainVC animated:YES];}@end
为了更好的知识,我在我的故事板中放了一个按钮去Unity VIEw.这样我就可以从xcode处理本机UI.
然后我有ARVIEwController显示Unity和Vuforia工作.
// ARVIEwController.h////#import <UIKit/UIKit.h>#import "UnityAppController.h"#import "UI/UnityVIEw.h"#import "UI/UnityVIEwControllerBase.h"@interface ARVIEwController : UIVIEwController{ IBOutlet UIVIEw *vIEwToUnity; UnitydefaultviewController *unityVIEwController; UnityAppController *unityController;}-(IBAction) goBack:(ID)sender;@end
// ARVIEwController.m////#import "ARVIEwController.h"#import <QuartzCore/QuartzCore.h>@interface ARVIEwController ()@end@implementation ARVIEwController- (ID)initWithNibname:(Nsstring *)nibnameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibname:nibnameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (voID)vIEwDIDLoad{ [super vIEwDIDLoad]; // Just setting Unity delegates and vIEw to add it as subvIEw for my main vIEw. // This allow me to add a UIbutton above the UnityVIEw to popVIEwController or anything i want to make native in iOS. unityVIEwController = [[UnitydefaultviewController alloc] init]; unityController = (UnityAppController*)[[UIApplication sharedApplication] delegate]; unityVIEwController.vIEw = (UIVIEw*)unityController.unityVIEw; [vIEwToUnity addSubvIEw:unityVIEwController.vIEw];}- (voID)dIDReceiveMemoryWarning{ [super dIDReceiveMemoryWarning]; // dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application,you will often want to do a little preparation before navigation- (voID)prepareForSegue:(UIStoryboardSegue *)segue sender:(ID)sender{ // Get the new vIEw controller using [segue destinationVIEwController]. // Pass the selected object to the new vIEw controller.}*/#pragma MARK -- Methods-(voID)goBack:(ID)sender{ [self.navigationController popVIEwControllerAnimated:YES];}@end
我做了一个回购下载项目工作.
https://bitbucket.org/jack_loverde/unity-5-vuforia-6-and-ios-native-integration,以防您想测试此版本的工作情况.
希望它对你有所帮助.
谢谢
总结以上是内存溢出为你收集整理的将Unity应用程序集成到现有iOS应用程序中全部内容,希望文章能够帮你解决将Unity应用程序集成到现有iOS应用程序中所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)