Project is closed and not supported.
iOS Bridge library helps to embed Unity 3D view into UIKit applications and vise versa. Create GUI with UIKit view elements.
It works with Unity 3D Free and Pro.
Tested on versions 3.5 - 4.1
It's possible to open UIKit view controllers from Unity. Application can open any view controller no matter it custom or one of standard:
- GameCenter
- In-app purchases shop
- WebView
- MapKit
- Image Picker
Another way to use iOSBridge is to open 3D view. You can use iOS Bridge to create such things:
- Animated 3D Model view with full Unity3d engine power.
- 360-degeree panorama view
- Augmented reality applications
Also it's easy to add UIKit view above Unity3D container to create GUI elements. Create GUI with interface builder and use it without complex GUI systems for Unity. There are some nice things:
- Automatic HD/SD graphics selection
- Easy localization options
- Dynamic fonts
- Clear layout and sizing setup
- Simple animation
Some examples available here: http://blind-soft.info/iosbridge/examples/
-
Unity Free and Pro
iOS Bridge works in either version of Unity without any limitations.
-
ARC support
ARC support is available for UIKit part. Full instruction is available in Documentation http://blind-soft.info/iosbridge/documentation#ARC
-
Load Unity scenes by name
3D view can have multiple scenes in Unity, every of them can be loaded by name.
-
Storyboard, Nib or programmatically created UI
UIKit view controllers can be created in any way. It's possible to load it from storyboard, create separate nib-files for each view controller or create them programmatically.
-
UIKit view above unity's 3D view
Set transparent UIView with controls to create GUI for Unity 3D view. Touch interactions will be available
-
AppDelegate-like interface
Application structure will be close to ordinary iOS application with AppDelegate and View Controllers. Create UIKit root view controller and switch between UIKit and Unity with single call.
-
Unity stays in memory
Unity stays in memory all time. Unity engine can be paused to prevent CPU and GPU consumption, but can't be unloaded.
-
Messages can be send to unity only in active state
Unity can't respond to messages in paused state. It should be active to perform operations.
-
Not all UIAppDelegate methods supported
There's no support of the methods:
application:shouldSaveApplicationState: application:shouldRestoreApplicationState: application:viewControllerWithRestorationIdentifierPath:coder: application:willEncodeRestorableStateWithCoder: application:didDecodeRestorableStateWithCoder:
- Install and import iOS Bridge into your Unity 3D project.
- Open iOSBridge scene
- Open Build settings and switch to iOS
- Setup bundle id, application name
- Add iOSBridge scene to be first in list.
- This step depends on how much scenes will be
- Only one scene content can be placed to iOS Bridge scene
- One or more scenes create separate scenes and add them to build list
- Build project
- Open created project.
- Unity 3.5 - 4.1:
Open main.mm file and replace
UIApplicationMain(argc, argv, nil, @"AppController");
with
UIApplicationMain(argc, argv, nil, @"iOSBridgeAppDelegate");
Unity 4.2:
Open main.mm file and replace
const char* AppControllerClassName = "UnityAppController";
with
const char* AppControllerClassName = "iOSBridgeAppDelegate";
-
Open UIKitAppDelegate.m
-
Place creation of root UIView controller into
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
-
Set initialized UIView controller to
self.rootViewController = nil;/* replace with your controller*/
-
Set cover UIView for 3D View
self.unityOverlayView = nil;/* replace with your view*/
-
Switch to Unity from iOS by calling
[[iOSBridge shared] openUnity];
-
Switch to Unity scene from iOS
[[iOSBridge shared] openUnityWithScene: @"sceneName"];
-
Switch to ViewController from iOS by calling
[[iOSBridge shared] openUIKit];
-
Send message to Unity(works only when Unity is running)
[[iOSBridge shared] sendMessageToUnity: @"message"];
-
Switch to ViewController from Unity by calling
iOSBridge.shared.OpenUIKit( "message" );
-
Send message to UIKit
iOSBridge.shared.SendMessageToUIKit( "message" );
-
Receive message from UIKit in iOSBridge.cs file
iOSBridge::MessageFromiOS( string message );
-
Receive message from Unity in UIKitAppDelegate.m
- (void)didGotMessageFromUnity: (NSString*)message;
-
Receive level load event in UIKitAppDelegate.m
- (void)didLoadLevel: (NSString*)loadLevel;
You can add ARC support to project with these steps:
-
Open *main.mm and replace
NSAutoreleasePool* pool = [NSAutoreleasePool new]; ... [pool release];
with
@autoreleasepool {
...
}
-
Open Build Settings and set
Objective-C Automatic Reference Counting to YES
-
Open Build Phases->Compile Sources
Select all .m/.mm files except UIKitAppDelegate.m, hit Enter and type
-fno-objc-arc
You can create GUI with UIView overlay
-
Create your view with nib as usual
-
Open UIKitAppDelegate.m file and init your overlay view
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ... MyOverlayView *overlayView = [[[NSBundle mainBundle] loadNibNamed: @"MyOverlayView" owner: self.rootViewController options: nil] objectAtIndex: 0] retain]; self.unityOverlayView = overlayView; ... }
Overlay view should be inherited from UIUnityOverlayView for transparent background and Unity touch support
UIKit interface can be loaded from Storyboard.
-
Create your interface in storyboard as usual.
-
Open UIKitAppDelegate.m file and init your view controller.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ... UIStoryboard *storyboard = [[UIStoryboard storyboardWithName: @"Your_storyboard" bundle: nil] autorelease]; self.rootViewController = [storyboard instantiateInitialViewController] retain]; ... }
-
Navigation and Tab view controllers, as well as segues can be used as usual.
UIKit interface can be loaded from nib
-
Create your root view controller nib as usual
-
Open UIKitAppDelegate.m file and init your view controller
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ... ExampleViewController *exampleController = [[[ExampleViewController alloc] initWithNibName: @"ExampleViewController" bundle: nil] retain]; self.rootViewController = exampleController; ... }
UIKit can be launched after application start.
Set autostartUIKit flag in application launch handler
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
self.autostartUIKit = YES; // Autostart UIKit
...
}
Level can be loaded by name with command
[[iOSBridge shared] openUnityWithScene: @"sceneName"];
Unity Free will start synchronous load scene and then switch to unity.
Unity Pro will start asynchronous scene load. Unity will be shown after load finished.
Method
private IEnumerator LoadLevelAsync( string levelName )
can be used to send message with load progress.
##iOSBridge##
Main class, used to handle communication between Unity and UIKit
static extern void iOSBridgeFinishUnity ( string data )
Open UIKit frontend and pause Unity
data - parameter to be sent to UIKit
##iOSBridge##
Singleton, that used to switch between UIKit and Unity.
+ ( iOSBridge*) ) shared
Returns shared instance of iOSBridge singleton
- ( void ) openUIKit
Opens UIKit view and pauses Unity
- ( void ) openUnity
Opens Unity view and resumes last scene
- ( void ) openUnityWithScene : ( NSString* scene )
Opens Unity view with loading scene by name.
- (void)sendMessageToUnity: (NSString*)message;
Sends message to Unity
##UIKitAppDelegate##
This class should be used to handle UIApplicationDelegate events.
It hides iOSBridge and Unity code for better coding experience.
This class supports most of methods from UIApplicationDelegate protocol.
See limitations section for unsupported methods.
- ( BOOL ) application : ( UIApplication* ) application didFinishLaunchingWithOptions : ( NSDictionary* ) launchOptions
Standard application initialization method. Best place to init some analytics, debug stuff. Also UIKit interface initialization should be places here.
- (void)didGotMessageFromUnity: (NSString*)message;
Method is called on message from Unity
- (void)didLoadLevel: (NSString*)loadLevel;
Method is called after level was loaded
@property ( nonatomic ) BOOL autostartUIKit
UIKit will be launched after app start if this flag is set YES
@property ( nonatomic , strong ) UIViewController* rootViewController
Root UIKit view controller.
@property ( nonatomic , strong ) UIUnityOverlayView* unityOverlayView
Unity overlay view controller.
##UIUnityOverlayView##
UIView subclass with transparent background and Unity touch support.
This is ordinary UIView with overloaded methods didMoveToSuperview and pointInside:withEvent: #####Overloaded methods#####
- (void)didMoveToSuperview {
Set background color to transparent
- ( BOOL ) pointInside : ( CGPoint ) point withEvent : ( UIEvent* ) event
This method walks through all subviews and call same method for each. If there's no control at touch point, it will return NO and UIKit will pass touch event to underlying Unity view.