Seeya
Seeya

Reputation: 163

Integrating Unity3d into iOS UITabbarController / UINavigationController

I've manage to compile my iOS project with Unity in it. Tested and the unity project runs in my iOS project if it's the first view.

However I'm trying to initialize it in another subview. Initially my application AppDelegate will handle the self.window.rootViewController It seems like I have to pass this handling of "UnityAppController" in the Plugin/iOS folder, overwriting (void)willStartWithViewController:(UIViewController*)controller method.

- (void)willStartWithViewController:(UIViewController*)controller {
    _rootController = [[UIViewController alloc] init];
    _rootView       = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _rootController.view = _rootView;

    LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:nil bundle:nil];

    [_rootView addSubview:loginViewController.view];
}

The application compiles and builts however the view is always empty. I see it does load my LoginViewController background colour but no labels or textfield which there was initially is on screen.

I'm not sure if Unity's View is already created and on top of my LoginViewController as I don't see the Scene I created in Unity.

tl;dr
View not displaying buttons, labels etc after integrating Unity3D project into iOS existing project.

I'm using Unity 5.0.0f4 and Xcode 6.3.1.

Any help is appreciated! Thanks!

Update
So I managed to solved it somehow. I changed the implementation file type from .m to .mm and it worked. I'm not sure why but it works. Anyone can explain this?

I'm still having problems setting up my rootViewController. My main.mm file look like this.

int main(int argc, char* argv[])
{
    @autoreleasepool
    {
        UnityInitTrampoline();
        UnityParseCommandLine(argc, argv);

#if INIT_SCRIPTING_BACKEND
        InitializeScriptingBackend();
#endif

        RegisterMonoModules();
        NSLog(@"-> registered mono modules %p\n", &constsection);
        RegisterFeatures();

        // iOS terminates open sockets when an application enters background mode.
        // The next write to any of such socket causes SIGPIPE signal being raised,
        // even if the request has been done from scripting side. This disables the
        // signal and allows Mono to throw a proper C# exception.
        std::signal(SIGPIPE, SIG_IGN);

        UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String:AppControllerClassName]);
        //UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }

    return 0;
}

I'm trying to setup a UITabBarController but I can't seem to do it. I've a ViewController that is a subclass of UnityAppController in which I overwrite the method willStartWithViewController.

- (void)willStartWithViewController:(UIViewController*)controller {

    if(_unityController == nil)
    {
        _unityController = [[UnityAppController alloc] init];
        NSLog(@"Unity Controller set");
    }

    _rootController = [[UIViewController alloc] init];
    _rootView       = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    UITabBarController *tabVC = [[UITabBarController alloc] init];

    LoginViewController *c = [[LoginViewController alloc] init];
    [tabVC addChildViewController:c];
    [[UIApplication sharedApplication] keyWindow].rootViewController = tabVC;

}

The view loads but nothing appears on the screen.

Upvotes: 0

Views: 2404

Answers (1)

Seeya
Seeya

Reputation: 163

So after many days of researching and testing hope my answer will help someone who is facing this problem.

Assuming you integrated unity with your project and there is no compiling errors. There are 3 files you have to modify in order to get this to work. I’m using Unity v5.0.0f4 and XCode 6.3.1. There are quite some changes in Unity’s v5 and v4. One main change is the createHierarchy is deprecated and you will have to use willStartWithViewController.

[main.mm]

In the main function inside main.mm file, edit with the extended “UnityAppController” class.

UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String:AppControllerClassName]);

[YourUnityAppController.mm]

This is the file where you create in unity under Plugin/iOS/YourUnityAppController.mm

This initializes the rootController and rootView used by Unity. We will not do anything else here.

- (void)willStartWithViewController:(UIViewController*)controller {

   _rootController = [[UIViewController alloc] init];
   _rootView       = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

}

In your didFinishLaunchingWithOptions method create an observer. We will need to use this to listen when Unity is being created completely and then set our window.rootView with anything we want.

The process I noticed how Unity creates its view is as follows

didFinishLaunchingWithOptions will call its super class which will invoke “willStartWithViewController”. Which means anything below the super class constructor will be invoked after “willStartWithViewController” is finished.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleUnityReady) name:@“UnityReady” object:self];

   [super application:application didFinishLaunchingWithOptions:launchOptions]
}

And our method to handle the observer.

- (void)handleUnityReady {
   _window.rootViewController = yourTabBarController;
}

This prevents Unity from overriding your view with its view when the app launches.

[UnityAppController.mm]

Inside this file locate startUnity function. We will need to add our callback in there. Make sure it is added after [self showGameUI]

 [[NSNotificationCenter defaultCenter] postNotificationName:@“UnityReady” object:self];

That is it! The Unity view is being created but not on top of your application. You can then grab the Unity view using GetAppController().unityView and assign it to any view you want.

References:

http://forum.unity3d.com/threads/tabbar-controller.156868/ http://answers.unity3d.com/questions/299550/combine-unity-and-native-opengl-in-ios-sparrow.html?sort=oldest http://www.taidous.com/thread-11147-1-1.html https://github.com/bzgeb/pd-for-unity/blob/master/iOS/Classes/iPhone_View.mm

Upvotes: 4

Related Questions