SCIO Development Blog
iPhone game development with Unity3D, Blender and XCode by Kay Bothfeld
iPhone & Unity3D: Integrating 3rd Party Static Libraries in Unity3D Generated XCode Projects
- Hits: 3457
- 11 Comments
- Subscribe to updates
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
- First Steps in XCode
- Integrating The Library Files in XCode
- Adjusting The Code in AppController.mm
- Finished
My environment for getting the app up and running:
- Unity3D 3.4
- XCode 4.02
- iOS 4.3.3
- 3rd Party static library libm3SysLib.a which handles accelerometer and gyroscope input
- 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):
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 ;-)
Now in XCode we have to check the project settings first:
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:
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 libm3SysLibDebug.a from file system and dependent frameworks you should have something similar to:
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:
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:
#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
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.
Comments
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.
@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.
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





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