iPhone & Unity3D: Integrating 3rd Party Static Libraries in Unity3D Generated XCode Projects

Posted by on in Development-Blog
  • Font size: Larger Smaller

Deploying for iPhone from Unity3D can be a little bit tricky, especially when customised code or 3rd party libraries are part of the project. So I decided to publish my own little check list and some hopefully useful information. In general I recommend the newest Unity3D version you can get (3.4 at time of writing) because there were some problems in Unity3D 3.3 leading to a bunch of errors and warnings.

 

 

Start

My environment for getting the app up and running:

  • Unity3D 3.4
  • XCode 4.02
  • iOS 4.3.3
  • Some 3rd Party static library. In my case it is libm3SysLib.a and its header file m3SysLib.h. Put your library and header file at the places mentioning m3SysLib
  • target iOS version 4.3
  • target device iPhone 4 / iPod Touch 4th generation

First of all you have to check your build settings (File / Build Settings, button Player Settings):

Player Settings in Unity3D

 

Before 3.4 the target platform for armv7 was called "armv7 iPad only" which is pretty confusing as iPhone 4 was supported very well. SDK Version should be "iOS latest" to avoid manual configuration after XCode updates.

Pressing "Build And Run" pops  up a file chooser dialog for selecting output folder where to generate all XCode related stuff. Note that on subsequent builds to the same folder, Unity3D asks you if the generation should replace or append. Because we are going to make a couple of changes in the project file you should always press "Replace" later on, otherwise you have to repeat all your steps. Like always: backups are a pretty good thing ;-)

 

First Steps in XCode

Now in XCode we have to check the project settings first:

XCode 4 Project Settings

We use armv7 architecture only because of the static library to include. If you need to run your app in simulator you have to provide an i386 version of your lib as well. Thus configuration gets far more complicated. Note the "Latest iOS" entry which should be set automatically by Unity3D. If not, set it manually.

 

OK, let's try our very first CMD+B (build) without the library. I have got two warnings (Semantic Issues regarding kScreenOrientation and an incompatible pointer type UIViewEAGLView) that can be ignored. If your build fails or if XCode hangs during the last build phase, check the scheme (Product / Edit Scheme...). Sometimes the simulator is set as target by default after the very first Unity3D export, but we want to deploy the app on our iPhone:

XCode Edit Scheme 

 

Integrating The Library Files in XCode

Before we use a 3rd party library in XCode we need to

  • Tell XCode where to find library 
  • Specify the location of the header files
  • Optionally include a DefaultSettings.bundle

The library file must be linked to the app in XCode. This is done in project settings. Just select "Unity-iPhone" in the "Targets" section at the left and go to the "Build Phases" tab. The last expandable row shows the libraries to link with and new libraries can be added by clicking the "+" sign and choosing "Add Other...". Note that your library might depend on some frameworks in my case for example CoreMotion.framework. These have to be added as well. After adding the library file itsef (in my case libm3SysLib.a) from file system and dependent frameworks you should have something similar to:

Including A Library 

 

Header file including takes place in the "Build Settings" tab of the project. Just add the location in "User Header Search Paths". Hint: Use the search field to filter:

Header Includes 

 

If your library relies on a default settings bundle, just copy it from wherever it is supplied to the project root.

Adjusting The Code in AppController.mm

In order to handle app life cycle properly we need to edit some code in AppController which is the UIApplicationDelegate of our app. Following places will typically need to be adjusted:

  • Header file include section, take your header file instead of m3SysLib.h:
    #import "m3SysLib.h"
  • - (void) registerAccelerometer
    If you don't need it or like in my case use more sophisticated sensor handling, type return; in the first line to avoid unnecessary sensor usage.
  • - (void) Repaint
    I commented out the whole if block if (kAccelerometerFrequency > 1e-6 ... for the same reason
  • - (void) applicationDidFinishLaunching:(UIApplication*)application
    Here we do our library initialisation stuff
  • - (void) applicationDidBecomeActive:(UIApplication*)application
    Called every time the app becomes active
  • - (void) applicationWillResignActive:(UIApplication*)application
    Called every time the app becomes inactive i.e. goes to background
  • - (void) applicationWillTerminate:(UIApplication*)application
    Optionally for old iPhone 3 systems that do not support multi-tasking
  • Other events you want to react on

 

Finished

We are hopefully done and everything is up and running. Using our library from inside Unity3D would go beyond scope of this blog entry. I recommend Clever Martian's Blog - An Experiment with iPhone Native UI and Unity 3 Pro. It helped me a lot and his downloadable example project was easy to set up.

If you liked this posting don't hesitate to push the Like, +1 or Twitter button, or you may leave a comment below (no registration required).

 

Rate this blog entry:

Comments

  • Eric Lewis Saturday, 01 October 2011

    Game Development

    This is a nice Post.I want to Android application.

  • AppsMaker Monday, 03 October 2011

    Very good

    Thanks it's a very good tutorial! Helped me get my stuff runing

  • Sebbe Monday, 03 October 2011

    Very interesting

    Interesting post, I've never really tried integrating native stuff in unity, but always nice to be up to date on the possibilities.

    Btw. any chance you could elaborate on your "more sophisticated sensor handling"? That sounds interesting and is beyond my imagination.

  • Kay Monday, 03 October 2011

    @Sebbe

    Thanks. I will write an article about our "more sophisticated sensor handling" when we are going to launch our game in early 2012. It deals with getting more information out of the gyroscope and accelerometer sensor data to track more complex motions.

  • iphone application development india Wednesday, 19 October 2011

    hey

    Hey,

    Great post there. Very useful indeed.
    Thanks for the share. i

  • Tom Monday, 31 October 2011

    Native Plugin

    Hi,

    Useful post. Thanks very much. I also found this to be quite useful guide on getting native functionality in OSX by creating a new native plugin http://www.reigndesign.com/blog/unity-native-plugins-os-x/#more-2824

  • Dotnet Developer Monday, 06 February 2012

    Web Development

    Hi..I really liked all the stuff provided by you. Your efforts are really admirable. Thanks a lot for sharing.

  • Kay Monday, 06 February 2012

    @DotnetDeveloper

    Thank you :)

  • Printing Texas Friday, 17 February 2012

    Worthy blog!

    I totally appreciate you for sharing the worthy information. Liked reading and knowing about it. Thanks for sharing your efforts!

  • Offsite Data Protection Monday, 12 March 2012

    Fantastic Info

    This post was fantastic! You made some great points. Actually I was not aware about that. But I found here many beautiful info in your post.

  • iphone app development india Friday, 16 March 2012

    Helpful Post

    Hey, it's really good post. Thanks for sharing.

  • web design bangalore Friday, 08 February 2013

    web design

    well written, find your style of writing is very rich. Written in a very good, hope you can continue to try hard, to write better!

  • outsource photo retouching Tuesday, 26 February 2013

    outsource photo retouching

    This is an excellent blog. I got many unknown information from this post.

    Reply Cancel

Leave your comment

Guest Tuesday, 21 May 2013