iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了将Unity应用程序集成到现有iOS应用程序中大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我有一个从Unity 5导出的iOS应用程序,我还使用vuforia为该Unity应用程序添加了ar.现在我想将unity应用程序集成到我现有的iOS应用程序中. 我已经遵循了http://www.the-nerd.be/2015/11/13/integrate-unity-5-in-a-native-ios-app-with-xcode-7/#comment-446教程,这对于在没有Vufori
我有一个从Unity 5导出的iOS应用程序,我还使用vuforia为该Unity应用程序添加了ar.现在我想将unity应用程序集成到我现有的iOS应用程序中.

我已经遵循了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 callBACk
extern "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 work

extern "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应用程序中所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。