Touch a UIButton and show Unity on UIView or UIVie

2019-04-12 20:50发布

I read several answers on the Unity forum, including here and only show how to create a subclass and present my first UIViewController.

- (void)createViewHierarchyImpl
{
    homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];

    //_rootView = _unityView;
    _rootView = homeViewController.view;

    //_rootController = [[UnityDefaultViewController alloc] init];
    _rootController = homeViewController;
}

So far, so good.
The problem is that there are no examples for the new version of the Unity plugin that generates XCode, to show a UIViewController with Unity.

-(void)goToUnity:(id)sender
{
    NSLog(@"touch the button is OK");

    UIViewController *app = [[UIViewController alloc] initWithNibName:@"UnityView" bundle:nil];

    [self.navigationController pushViewController:app animated:YES];
}

Thus, nothing happens. Is there any way to call the Unity in "default" mode Xcode?

Thank you.

2条回答
女痞
2楼-- · 2019-04-12 21:11

Read an read and read many times, test and erros many times, i finally do! Touch a UIButton and open or do whatever you imagine with it.

I will share here for those who need a more complete code.
Getting Started:
You need to create your own main file .h and .m to be "overruled" the Unity automatically:

mainAppController.h

#import "UnityAppController.h"
#import "UI/UnityView.h"
#import "iPhone_View.h"

@interface mainAppController : UnityAppController
{
    UnityAppController  *_unityAppController;
}

- (void)createViewHierarchyImpl;

@end

You have to import this H headers to controll Unity stuffs.

mainAppController.m

#import "mainAppController.h"

@implementation mainAppController

-(void)createViewHierarchyImpl
{
    if(_unityAppController == nil)
    {
        _unityAppController = [[UnityAppController alloc] init];

        NSLog(@"Unity Controller was set NICE!!!!");
    }
    UIStoryboard *storyBoard;

    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
    {
        storyBoard = [UIStoryboard storyboardWithName:@"iPadMain" bundle:nil];
    }
    else
    {
        storyBoard = [UIStoryboard storyboardWithName:@"iPhoneMain" bundle:nil];
    }

    UIViewController *mainVC = [storyBoard instantiateInitialViewController];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.window.rootViewController = mainVC;

    _rootController = [self.window rootViewController];
    //_rootController.wantsFullScreenLayout = TRUE;
    _rootController.edgesForExtendedLayout = TRUE;

    _rootView       = _rootController.view;

    [self.window makeKeyAndVisible];
}

@end

IMPL_APP_CONTROLLER_SUBCLASS(mainAppController) //THIS IS VERY IMPORTANT!!!

This last line is the Unity which will "read" and use.

So, see, I used the storyboard, so I have more control over the navigation visually. But you can use whatever you want.

In another any UIViewController, I'll call the Unity, as follows:

ShowARViewController.h

#import <UIKit/UIKit.h>
#import "UnityAppController.h"
#import "UI/UnityView.h"
#import "iPhone_View.h"

@interface ShowARViewController : UIViewController
{
    UnityAppController *unityController;
    IBOutlet UIView *viewToUnity; // This is linked on Storyboard 
}
@end

ShowARViewController.m

#import "ShowARViewController.h"

@interface ShowARViewController ()

@end

@implementation ShowARViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    unityController = (UnityAppController*)[[UIApplication sharedApplication] delegate];
    [viewToUnity addSubview:unityController.unityView];
}
// more methods and codes...
@end

See, so I can have more control of Unity that I added in UIView and I can do what I want, for example, add a button above this view.

I hope I have helped really. If you have any questions or suggestions to make this code better, feel free to write me.

Regards

查看更多
【Aperson】
3楼-- · 2019-04-12 21:30

Try getting the controller like this:

UIViewController* controller = [[UnityDefaultViewController] alloc] init];
查看更多
登录 后发表回答