Unity as a Library on iOS and tvOS
Understand how the Unity as a Library feature embeds the Unity Runtime Library in a native iOS and tvOS application.
阅读时间7 分钟最后更新于 19 天前
The Unity as a Library feature integrates the Unity Runtime Library into native iOS and tvOS 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. Both iOS and tvOS share the same project structure and integration workflow.
The following sections describe how to integrate Unity as a Library with the Objective-C and Swift Xcode project types, the methods that control the Unity runtime, and the known limitations of the feature.
Objective-C Xcode project structure
To use Unity as a Library for iOS and tvOS, first build your Xcode project from Unity as you normally would. For more information, refer to Build an iOS application.
Every Unity iOS and tvOS 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-exampleObjective-C methods 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 |
Swift Xcode project structure
Every Unity iOS and tvOS Swift Xcode project has the structure described in Structure of a Unity Xcode Swift project type:
- A library part in the UnityFramework target that contains plug-ins and dependent frameworks. Building this target produces the bundle. The UnityFramework target links the UnityAPI target.
UnityFramework.framework - A public API part in the UnityAPI target that produces the static framework. It contains the trampoline source code and exposes the public
UnityAPI.frameworkmodule, including theUnityAPIAPI andUnityPlayerheaders, to native plug-ins. This target doesn't exist in the Objective-C project type.UnityPluginInterface - A thin launcher part in the Unity-iPhone target that includes app representation data and runs the library. Unlike the Objective-C project type, is linked with the main binary. The entry point is
UnityFramework.frameworkinstead ofMainApp/MainApp.swift. The Unity-iPhone target has a single dependency on the UnityFramework target.main.mm
The file name matches the Unity project name (sanitized), not the hardcoded used in the Objective-C project type.
.xcodeprojUnity-iPhone.xcodeprojTo 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 from the framework to control the Unity runtime.
UnityFramework.frameworkUnityPlayerUnityAPITo access the API, import at the top of your source file:
UnityAPIimport UnityAPI
For example projects and plug-ins that demonstrate how to integrate Unity into an Xcode project, refer to the repository (Unity Technologies on GitHub).
uaal-exampleSwift methods to control the Unity runtime
After you add the Unity Xcode project to your workspace and link into your host app, control the Unity runtime through :
UnityFramework.frameworkUnityPlayer.sharedMethod | Description |
|---|---|
| Returns the singleton instance of |
| Sets the bundle where the Unity runtime looks for the |
| Starts the Unity engine. On first call, initializes the runtime and loads the first scene. After |
| Unloads Unity and posts |
| Quits Unity completely and posts |
| Controls whether |
| Pauses the Unity Player. |
| Resumes the Unity Player. |
| Returns whether the Unity Player is paused. |
| Returns the view that Unity renders to. |
| Returns the root view controller of the window that displays the Unity view. |
| Sends a message to a C# script on a GameObject. Equivalent to |
Lifecycle notifications
Subscribe to notifications to react to Unity lifecycle events:
NotificationCenter- : Unity posts this notification when
UnityNotifications.unityDidUnloadcompletes. Use it to update the UI of your host application.unload() - : Unity posts this notification when
UnityNotifications.unityDidQuitcompletes.quit()
For the full list of available notifications, refer to Listen for lifecycle events.
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.
-
(iOS only) 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