Unity as a Library: Bring Unity Features to Your Android App 🎮
By: Julien Salvi
Tags: AndroidDev, Game Development, Unity, Android

When starting a new project, for instance a 3D game here, you will be able to easily add 3D components thanks to the “GameObject” menu located at the top on the editor. You can select multiple shapes, lights or even UI components , but you can also import a more complex 3D asset that you have locally or remotely if you get assets from the Unity Asset Store.

Therefore, we can create and attach a C# script to the 3D capsule we added on the scene. Keep in mind that the script file name should match the class name of your script. To attach it to the asset, click on “Add Component” and choose “New Script”. For the example, in the created script, we are going to implement a ChangeColor() method for changing the color of the material and 4 others functions for moving the object in the scene (a similar behavior you can find with the keypad arrows while playing a game).

Every script attached to an object extends the base class MonoBehaviour, it offers some lifecycle methods that are very helpful for developing your apps and games. Let’s have a look at a newly created script:

public class MyAsset : MonoBehaviour { // Start is called before the first frame update

void Start() {



} // Update is called once per frame

void Update() { }

}

You can override these methods, alongside many others depending on what you need, and implement the behavior of the 3D asset. Here for example, let’s override Update() for handling the click on the capsule from the touch screen and we are going to expose the ChangeColor() method that will be used by our Android application to change the color of the object.

In order to be notified by the Android application, the exposed method signature must be as the following:

// Exposed method to be notified by UnitySendMessage()

void MyMethod(string params) {

// Do something

}

What is very annoying here, is that the parameter of the function must be string type so it might be complicated to pass some complex data with the UnitySendMessage() method.

Now let’s have a look at the final script:

Once the scripting is done without any compilation error, we can convert our Unity project to an Android project that will be used with Android Studio.

Exporting your Unity project as an Android library

Now that we are all good on the Unity side, let’s focus on the integration of this game in an existing Android application. To export the project, go to the “File” menu and click on “Build Settings”, there you will be able to export the Unity project as an Android Studio compatible project. Make sure to target the Android platform and to have checked the “Export Project” box. If you already have some Java/Kotlin files in your Unity game, do not forget to check the “Symlink Sources” box before exporting.

As you may know, applications published on Google Play needs to support 64-bit architectures. To do so, click on the “Player Settings” and scroll to “Other Settings” to enable IL2CPP for the scripting backend configuration. Then, when exporting the project, ARM64 devices will be supported.

Build Settings window for exporting the project

IL2CPP configuration

You are now all set for working directly with the Unity project exported as Android Studio project. When you open this project on Android Studio, you will see about this screen: