Unity as a Library on iOS
Understand how the Unity as a Library feature embeds the Unity Runtime Library in a native iOS application.
Read time 4 minutesLast updated 4 days ago
The Unity as a Library feature integrates the Unity Runtime Library into native iOS applications.
You can use this feature to include Unity-powered capabilities, such as 2D and 3D real-time rendering, augmented reality (AR) experiences, 3D model interaction, or 2D mini-games, in your native application. The Unity Runtime Library exposes controls to manage when and how to load, activate, and unload content in your native application.
The following sections describe the Xcode project structure that Unity generates, the methods that control the Unity runtime, and the known limitations of the feature.
Xcode project structure and integration
To use Unity as a Library for iOS, first build your Xcode project from Unity as you normally would. For more information, refer to Build an iOS application.
Every Unity iOS Objective-C Xcode project has the structure described in Structure of a Unity Xcode Objective-C project type:
- A library part in the UnityFramework target that contains source files (such as ), plug-ins, and dependent frameworks. Building this target in Xcode produces the
UnityFramework/UnityFramework.hbundle. Unity doesn't place a prebuiltUnityFramework.frameworkin the export folder..framework - A thin launcher part in the Unity-iPhone target that includes app representation data and runs the library. The Unity-iPhone target has a single dependency on the UnityFramework target.
To integrate Unity into another Xcode project, combine both Xcode projects (the native one and the one Unity generates) into a single Xcode workspace. Then add the built to Frameworks, Libraries, and Embedded Content (or Embedded Binaries) for your native application's target. After you do this, you can use the class to control the Unity runtime.
UnityFramework.frameworkUnityFrameworkFor example projects and plug-ins that demonstrate how to integrate Unity into an Xcode project, refer to the repository (Unity Technologies on GitHub).
uaal-exampleMethods to control the Unity runtime
After you load in the host app, control the Unity runtime through the Objective-C class. The class is declared in and is the principal class of the framework bundle.
UnityFramework.frameworkUnityFrameworkUnityFramework/UnityFramework.hThe following table lists the methods of the class:
UnityFrameworkMethod | Description |
|---|---|
| Returns the singleton instance of |
| Returns the |
| Sets the bundle where the Unity runtime looks for the |
| Runs Unity from the |
| Runs Unity when other views already exist. |
| Unloads Unity and triggers a callback to |
| Registers a listener object that receives callbacks for |
| Unregisters a listener object. |
| Shows a Unity view that's already running, while a non-Unity view is visible. |
| Pauses Unity. |
| Sets the executable header that CrashReporter uses. Call this method before you run Unity. |
| Acts as a proxy to |
| Unloads Unity completely and triggers a callback to Note: You can't run Unity again in the same process after this call. You can set |
Limitations
Unity doesn't control the runtime lifecycle, so Unity as a Library might not work in every scenario. Known limitations include:
-
Unity as a Library supports only full-screen rendering, not rendering to part of the screen.
-
You can't load more than one instance of the Unity runtime.
-
You can't adjust the device's status bar visibility through the iOS Player settings when you use Unity as a Library in a native iOS application. To hide the status bar, use one of the following options:
- Set the or
UIStatusBarHiddenkey in theUIViewControllerBasedStatusBarAppearancefile of your project.info.plist - Set the view controller property and call the
prefersStatusBarHiddenmethod.setNeedsStatusBarAppearanceUpdate - Set the view controller property .
childViewControllerForStatusBarHidden
- Set the