2

I have created an Android application in Android Studio and Unity. I can start UnityPlayerActivity from other activities in Android Studio. Now, I updated the Unity project and the scene in Unity and I want to start this updated Scene/Activity in the already built Android Project.

I don't want to create a new Android Studio project from Unity by exporting and then copy the other older files in the new project. This process can be very tedious and also VCS with GitHub would be another challenge. There are more than 40 files in my current project and just because of 1 update in Unity Project, I don't think creating a new project is worth it or something I would like to do.

Some suggestions which I already have are:

  1. Export from Unity again and copy-paste all the files created in the older project from Android Studio.
  2. Create a library and import this library in Android Studio (I am not very clear how this works but I don't think this will work because of the limited scope of a project).

So, is there any way such that I can export an Activity from Unity and start the new Activity in Android Studio? Any help is appreciated and thanks in advance if anyone can solve this for me.

2
  • Scene/Activity What do you mean? Do you mean Unity scene file or a java file?
    – AlexWei
    Commented Aug 28, 2019 at 10:06
  • @AlexWei In Unity, it is known as Scene while in Android, it is known as Activity. I want to bring the Unity Scene in my already built Android Studio project and start that Scene (or Activity because now it's Android). Commented Aug 28, 2019 at 13:04

1 Answer 1

0

The cleanest way would be to convert the exported Android-Project into a library.

  1. Remove the activity from src/main/AndroidManifest.xml
  2. Modify build.gradle
    • Change apply plugin: 'com.android.application' to apply plugin: 'com.android.library'
    • Change implementation fileTree(dir: 'libs', include: ['*.jar']) to api fileTree(dir: 'libs', include: ['*.jar'])
    • Remove all the bundle {...}-stuff

This should enable you to build a .aar-File which you can use in your native Android-Project.

Depending on your Unity-Version (and packages) other modifications could be needed, but that will be pointed out by errors in AndroidStudio when building the library.

The modifications are based on this article and complemented for Unity 2017.4.

The newest Unity beta (2019.3) also supports creating a library directly from Unity. But I would not recommend using a beta in production.

2
  • Are you sure I can start an activity which is present in a library? The last time I tried this method, after importing the library, my project was not able to start that activity and was throwing an error (probably something like out of scope, I don't remember). Commented Aug 28, 2019 at 13:06
  • That should not be a problem if everything is setup the right way. For better integration and customization it is also possible to make your own Activity that handles the UnityPlayer lifecycle. Take a look at UnityPlayerActivity, it doesn't do much. It is even possible to handle the lifecycle inside a Fragment.
    – aalmigthy
    Commented Aug 29, 2019 at 9:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.