2

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 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

9 Comments

Hello i want to open unity AR view in my current view controller. I am doing the same. But app crashes say Unityview unrecognised selector sent to
@ios-developer what is your Unity version ?
I want to use the Xloudia AR. They provide support in unity. I have unity package and i export it as a Xcode project. Now i want to use it in my existing iOS project.
Do you know how to integrate unity Xcode project in objective c Xcode project?
Hi @Lenin this post has 6 years. I doesnt work with Unity anymore. Good luck
|
0

Try getting the controller like this:

UIViewController* controller = [[UnityDefaultViewController] alloc] init]; 

1 Comment

Nothing happend, touch the button, but the view doesnt show up. Trying to addSubview too, but i had "uncaught exception". Any advice ? Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.