Integrating Unity into a Native iOS App – Example

After describing the Idea of adding Unity into a native App here, i got a lot of questions about some sample source code. So here it is 😉

For a minimum working sample of how to integrate Unity, you need a Unity project of course. Best way to start is from the iOS Export of the unity project. Personally i prefer to not change any of the Unity-generated files itself, but only integrate them, so i dont have to care about these changes everytime i do a new unity export.

After exporting from Unity you get a “normal” XCode project containing a “Classes” and a “Libraries” folder. You should be able to run it on a device, otherwise there is some problem with your Unity-project which need to be fixed before integrating it.

Let native Objective-C take over

To keep it seperated: create another folder next to Classes called “native” and add it to the project. In this folder you can now put all your custom code.

First we need out own Application Delegate to gain control. For the sample Project i just created a new XCode Single-view project and copied the files into the native folder (except for the main.m).

Changes to AppDelegate.h

AppDelegate should subclass UnityAppController (instead of UIResponder<UIApplicationDelegate>) therefore you also need to include the according .h:

#import "UnityAppController.h"

@interface AppDelegate : UnityAppController
Changes to AppDelegate.m

To make the unity-generated main.mm use our AppDelegate we just need to add the Line

IMPL_APP_CONTROLLER_SUBCLASS(AppDelegate)

somewhere above the implementation of AppDelegate. To really gain controll over the view hierachy, we just implement our own version of startUnity:


- (void) startUnity:(UIApplication*) application {
  [super startUnity:application]; //this initializes Unity and the whole view hierachy (including the main window) created by unity
// take over control:
  self.window.rootViewController = ownRootViewController;
//now the stage is ours.
//You can do
[ownRootViewController.view addSubview:super.rootViewController.view];
//to add the unityView
}

Now we can do whatever we want with the unity view. If you include UnityAppController+UnityInterface.h you can use setPaused on the AppDelegate to pause and unpause the unityRenderengine (which you should do if unity isnt shown).

I exported a very simple Example (a cube with a particlesystem) from Unity and integrated it into a simple native app while writing this post. You can find the full source on github:

GitHub Sample

(Unfortunatly github allows only filesizes to 100MB but the libiPhone-lib.a file from unity is >400MB, so you have to add this file from your own export. Better still: use your own export and replace the all the folders except the “native” folder)

I hope this helps.
feel free to leave a comment.

7 thoughts on “Integrating Unity into a Native iOS App – Example”

  1. Thank you so much for this tutorial! I am having an issue trying to complete this with a Unity Project that’s using the Google Cardboard SDK. Specifically, I am getting Uncaught exception: NSInvalidArgumentException: -[CardboardAppController unityController]: unrecognized selector sent to instance 0x15f6c33b0.
    Xcode sees two AppControllers, the “CardboardAppController.mm” in the iOS/Plugins/ folder, and the “UnityAppController.mm” in the imported “Classes” folder… Any advice on how to merge or bridge the two?

    1. Unfortunatly i have no experience with the cardboard SDK.
      Is the CardboardAppController subclassing the UnityAppController?
      I guess that the Cardboard SDK changes the main.mm where the Application Delegate is defined from the standard-Unity way (which uses the macro) to the CardboardAppController. But thats just a guess.
      Hope it helps.

  2. Hello, there!
    I’ve tried to implement unity3d into an existing project like you’ve shown, with a bit of my own tweaks.
    It seems to work fine.
    Now i want to be able to startUnity only when i tap on a specific button, and not from the appDelegate when i app starts up.

    Is that possible? Could you help me out?

    Thanks a bunch.

  3. Hey Markus, thanks for the tutorial.
    Do you think it is possible to combine 100 unity apps into one app?

    Seems like it’d be a different process than building one unity app into xcode and then writing a wrapper; any ideas for how to go about this?
    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *