Search Unity

Unity as a Library - Unload and Activities Stack

Discussion in 'Android' started by chris_unity549, Jan 5, 2021.

  1. chris_unity549

    chris_unity549

    Joined:
    Aug 28, 2020
    Posts:
    7
    Hello everyone,

    We are using Unity as a Library to create a module that will be distribute to several clients for them to integrate into their existing Android apps and we are facing some issues with the lifecycle limitations of UaaL.

    Let's get some things out of the way first:
    1. Our module depends on another module, let's call it "main", already deeply integrated with the client apps.
    2. Because of #1 we can't run Unity in a different process, because we will lose all object references to the "main" module.
    3. We need to be able to re-launch Unity as many times as we want.

    So we decided to create our own "UnityActivity" that implements "IUnityPlayerLifecycleEvents" and copy the code from "UnityPlayerActivity". Then instead of killing the entire process, we unload the UnityPlayer and launch the previous activity (like in the UaaL example).

    With this approach the Unity Activity is never killed and remains in the stack. This is not very noticeable in the UaaL example since it only consists of the MainActivity (with the button to launch Unity) and the UnityActivity. So once you are back to the MainActivity the only things to do is either to launch Unity again or close the app.

    But in a real app the activity structure is more complex than that. We can have the following Activities in our stack:
    A > B > C > Unity Activity

    Since we don't want to kill the Unity Activity as we can't launch it again after that the only way to go back to A for example is to manually resume B from C and A from B when the back button is pressed and still at the end the Unity Activity will still be in the stack.

    At that point (landed in A), if we click back it will bring back the Unity Activity. What the UaaL is doing is it calls "finishAffinity" to kill all the Activities in the Task. Since we want this module to be integrate on different client apps with different structures this isn't a very nice UX to implement.

    Is there a better way to close/unload/kill the Unity Activity to won't mess with the Activity Stack?

    We are using Unity 2019.4.1f1.
    UaaL V2 thread
    UaaL example

    Thanks
     
    Last edited: Jan 6, 2021
  2. dimitris_baud

    dimitris_baud

    Joined:
    Jan 15, 2018
    Posts:
    31
    Same issue here. I can't figure out how to use UaaL without interrupting the normal Android app flow.

    This is a major blocker.
     
    john_goren likes this.
  3. RDudnikovTangoMe

    RDudnikovTangoMe

    Joined:
    Oct 8, 2020
    Posts:
    4
  4. chankeiro

    chankeiro

    Joined:
    Oct 25, 2021
    Posts:
    11
    Last edited: Jul 30, 2022
  5. 1Riptide

    1Riptide

    Joined:
    Jan 10, 2016
    Posts:
    4
    Bump. This is a real problem in the wild for Android. (!)
    Not a good look for Unity if you are among those who evangelize on its behalf.

    Elaborating : Its worth mentioning that this all very-much doable in older versions of Unity. We are currently in the process or investigating the upgrade to LTS and have found this as a major blocker. For the project in question we use a custom UnityPlayer object that can exist inside of either Fragments or Activities and do nothing more than override the UnityPlayer kill() method to get around the app closing problem described here. This was working as of version: 2019.2.17f1 but does not work in LTS versions.
     
    Last edited: Aug 18, 2022