Integrate the Unity Runtime Library into Android applications using the Unity as a Library feature.
You can use this feature to include Unity-powered features, such as 3D/2D Real-Time Rendering, AR Experience, 3D model interaction, or 2D mini-games, into your application. The Unity Runtime Library exposes controls to manage the Unity runtime lifecycle within the application.
Important: The introduction of Unity as a Library in your project might require you to adapt native and managed plug-ins to work properly for Android. Plug-ins that make changes to Gradle manifests need to use the Gradle changes outlined in Using Unity as a library in native iOS/Android apps.
You don’t need to do anything different when you build your Gradle project from Unity.
Every Android Gradle project that Unity generates has the following structure:
To integrate Unity into another Android Gradle project, you must include the unityLibrary module of the generated Android Gradle project in your Android Unity project through the settings.gradle file.
This repository contains example Projects and plug-ins that demonstrate how to integrate Unity into an Android app, along with further documentation.
To control a Player, relay an Intent to launch the Unity activity and extend it if needed. For more information, refer to Android developer documentation on Intents and Intent Filters. You can also use the UnityPlayerForActivityOrService or UnityPlayerForGameActivity Java API, depending on the application entry point you have selected.
UnityPlayerForActivityOrService is the main class for hosting the Unity runtime in your Android application. It extends the abstract UnityPlayer base class and provides the concrete implementation for Activity-based or Service-based Unity integration.
UnityPlayerForGameActivity is an alternative to UnityPlayerForActivityOrService for applications that use Google’s GameActivity. It extends the abstract UnityPlayer base class and works with the GameActivity threading model. The corresponding entry point activity is UnityPlayerGameActivity, which extends GameActivity.
IUnityPlayerLifecycleEvents provides a way to interact with three important lifecycle events of the Unity Player:
IUnityPlayerLifecycleEvents.onUnityPlayerLoaded when the Unity Player finishes initialization. This is a default method that does nothing unless you override it.IUnityPlayerLifecycleEvents.onUnityPlayerUnloaded when Application.Unload, UnityPlayerForActivityOrService.unload(), or UnityPlayerForGameActivity.unload() unloads the Unity Player. This puts the Unity Player in a paused state where it unloads all Scenes, but keeps everything else loaded in the memory.IUnityPlayerLifecycleEvents.onUnityPlayerQuitted when the Unity Player quits. The process that was running Unity ends after this call.You can pass an instance of IUnityPlayerLifecycleEvents to the UnityPlayerForActivityOrService or UnityPlayerForGameActivity constructor. You can also override its methods in subclasses of UnityPlayerActivity or UnityPlayerGameActivity.
Unity doesn’t control the runtime lifecycle, so Unity as a Library might not work for all possible use cases. Known limitations include: