This document explains how to include Unity as a Library into standard Android application through Activity. You can read more about Unity as a Library.
Requirements:
- Android Studio Iguana (2023.2.1) or later
- Unity version 6000.0.0b16 or later
Note
- For Unity versions from 2019LTS to 2021LTS, use 19LTS-21LTS branch.
- For Unity version 2022LTS, use 22LTS branch.
1. Get source
-
Clone or Download GitHub repo uaal-example. It includes:
* UnityProject * ... * Assets * Plugins * Android * MainApp.androidlib * ...
- UnityProject - this is a simple demo project made with Unity which will be exported for the standard Android application.
- MainApp.androidlib - Inside Unityproject, you can find MainApp.androidlib, which is Android Library Project and has a simple UI, with two entries - MainUnityActivity and MainUnityGameActivity.
2. Generate Gradle project for Android platform
- Open UnityProject in Unity Editor.
- Go to Build Profiles window (Menu / File / Build Profiles).
- Select and switch to Android Platform.
- Go to Player Settings window. (Click Player Settings button at the top of Build Profiles or use Edit / Project Settings menu and choose Player tab on the left.)
- Go back to Build Profiles window.
Everything is ready to build, run and debug:
If everything succeeded, at this point you should be able to run NativeAndroidApp:
-
Unity is running in another process android:process=":Unity" (AndroidManifest.xml at app module)
-
In step 2, if you select only Activity or GameActivy as Application Entry Point, MainUnityActivity.java or MainUnityGameActivity.java file must be deleted.
-
(Optional) We found some Android 7.* devices set frontOfTask to wrong state for activities, as a result when finishing/quitting Unity activity whole task goes to background instead of bringing back Main activity. Next workaround keeps expected behavior: add the below code to MainUnityActivity.java or UnityPlayerGameActivity.java or both in NativeAndroidApp
@Override public void onUnityPlayerQuitted() { SharedClass.showMainActivity(""); finish(); }