Skip to content

Latest commit

 

History

History
61 lines (52 loc) · 3.86 KB

android.md

File metadata and controls

61 lines (52 loc) · 3.86 KB

Integrating Unity as a library into standard Android app

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

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.)
    • In Other Settings -> Configuration section -> select both Activity and GameActivity as Application Entry Point
  • Go back to Build Profiles window.
    • Select option “Export Project”, and Export UnityProject to a folder. (If you see Multiple application entries pop-up, click Yes.)

Project is ready

Everything is ready to build, run and debug:

If everything succeeded, at this point you should be able to run NativeAndroidApp:

Main Activity Unity Activity or GameActivity
Main Activity Unity is loaded and is running in a separate Activity. Light grey buttons in the middle are added from the MainUnityActivity or MainUnityGameActivity implemented in MainApp.androidlib

Notes

  • 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.

    • When only Activity is checked as ApplicationEntry Point

    • When only GameActivity is checked as ApplicationEntry Point

  • (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(); }