Интегрируйте приложение unity в существующее приложение iOS

У меня есть экспорт приложения iOS из Unity 5, а также я использовал vuforia, чтобы добавить ar в это приложение Unity. Теперь я хочу интегрировать приложение Unity в свое существующее приложение для iOS.

Я подписался на http://www.the-nerd.be/2015/11/13/integrate-unity-5-in-a-native-ios-app-with-xcode-7/#comment-446, и это хорошо для интеграции Unity без Vuforia.

Итак, как я могу это сделать, а также лучше сделать это с помощью быстрого


person Sulo    schedule 11.01.2016    source источник
comment
Спасибо, @Krypton. Но на мой вопрос нет ответа: P   -  person Sulo    schedule 12.01.2016


Ответы (1)


Я выполнил интеграцию с Unity 5.3.8p8 и Vuforia 6.0.117 для iOS 8.4 и выше. Это руководство создано для Objective-C, но вы можете без проблем сделать это для Swift, просто создайте ЗАГОЛОВОК ПРЕФИКСА и импортируйте туда все файлы .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 deviceType, 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)

Вы заметили, что я использую раскадровку. Итак, мой 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. Затем у меня есть 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 на тот случай, если вы хотите проверить работу этой версии.

Надеюсь, это вам поможет.

Спасибо

person Daniel Arantes Loverde    schedule 04.11.2016
comment
Этот код не может быть скомпилирован из-за ожидаемого идентификатора или символа '(' в строках, начинающихся с extern C void. Как вы его решили? - person delete; 28.01.2018
comment
Какая у вас версия Unity и Vuforia? - person Daniel Arantes Loverde; 28.01.2018
comment
Мы используем Unity в самой последней версии 2017.3.03f, которая поставляется с Vuforia (в случае с Unity 2017.2: library.vuforia.com/articles/Training/) - person delete; 28.01.2018
comment
Пардон, эта версия у меня еще не работала. Если эта ошибка возникла из-за того, что вы открыли мой проект в этой последней версии Unity, вероятно, причина ошибки. Поскольку в Unity и Vuforia многое меняется, вам лучше открыть мой XCode, сравнить со своим XCode и адаптировать то, что вам нужно. Спасибо за твое сообщение! - person Daniel Arantes Loverde; 28.01.2018