Unity is a game developing engine developed by Unity Technology, which runs on Windows, MacOS and Linux, and is used for building cross-platform games and apps (Android, iOS, Windows, PlayStation). It’s been here for 15 years now, giving us the opportunity to make high quality graphic experiences and games on mobile. Unity has been becoming more mainstream and popular: according to the company’s statement, “50% of new mobile games are made with Unity”. A lot of gaming studios (Blizzard, White Elk, Square Enix, etc) depend on Unity for the development of their best games as well as AR/VR standalone applications.
Unity has lately improved the backing for building an amazing game experience on Android and gave us Unity as a library which is meant for adding a Unity game in an existing Android app as an appropriate library integration. It helps in full stack android development. We will see here how to export a basic Unity experience and how to incorporate it into your own application, how to simply extend the UnityPlayerActivity generated in the library, and further add some innate UI components to your extracted Activity in order to easily interact with the game in many ways!
How to get started?
First, we have to build a small 3D experience with Unity (version 2020.1.11f1). We would not need any extended knowledge about the game engine. The important thing here is to have access to an Android library that we can use in an already existing Android application.
When we are starting a new project, for example a 3D graphics or game, we will be able to simply add 3D components, thanks to the “GameObject” menu which can be found located at the top part on the editor. If we see here, there are many customisations that can be selected- multiple shapes, lights and even UI components, but we can as well import a more intricate 3D asset that we may have on the local computer or remotely if we get assets from the Unity Asset Store.
Hence, we can build and attach a C# script onto the 3D capsule we previously added on the part. You have to keep in mind that the script file name must match the class name of the script. To assign it to the asset, click on the “Add Component” option and choose “New Script”. As an example, in the created script, we can choose to implement a ChangeColor() technique to change the color of the material. Similarly, there are many other functions for moving the object (a comparable behavior you can find while playing a game with the keypad arrows), or implementing other actions in the scene.
Each one of the scripts attached to an object outspreads the base class, and it offers some lifespan methods that are really helpful for developing your applications and games. So that it is notified by the Android app or game, the exposed method signature should have the parameter of the function as a string type.
Once creating the script is done without compilation errors of any kind, we can move on to convert our Unity project to an Android project that will be based in the Android Studio. These are some of the most basic things you need to learn here for full stack android development services.
Exporting your project in Unity to an Android library
Now that we are all set on the Unity side, let us focus on the incorporation of this game in an already existing Android application. To export the project, we have to go to the “File” menu and click on the “Build Settings” option, there we will be able to transfer the Unity project into an Android Studio compatible project. We have to make sure to target the Android platform and also to have confirmed the “Export Project” box. If there is already some Java/Kotlin files in the Unity game, don’t overlook the “Symlink Sources” box (check it) before the transfer.
As you might be aware, apps published on Google Play are required to support 64-bit architectures. To make it so, click on the “Player Settings” and scroll down to “Other Settings” to enable IL2CPP in order to configure the scripting backend. Then, when we are exporting the project, ARM64 based devices will be supported.
Exploring your Unity project
You are now all done with working directly with the Unity project exported as an Android Studio project. When we open this project on Android Studio, we will see the following project structure: It is made of two modules (launcher and unityLibrary), the first is essentially for launching the application where we can implement all the Android gear we want—whether creating new Activity, Views or Fragment. The second module is the unity game as library module. Here, you will see that you have access to the UnityPlayerActivity which can be extended. The most interesting thing here is that we can use the UnityPlayer to send notifications to our Unity game. Now let us bring this module into a prevailing Android application.
After introducing the Unity module in our app or game, let’s see here the generated UnityPlayerActivity. What is interesting here is the UnityPlayer. If we just look at the implementation behind its hood, it is a FrameLayout. It will grasp the SurfaceView where the Unity understanding will be concentrated and take care of all the things that the game requires to run smoothly on Android. Do not forget to add the string resource or else the Unity game will crash.
Now that you know how the UnityPlayerActivity is structured, you can extend it in your app module to get the best out of it. Thanks to the UnityPlayer, you can add any of the UI native components to your screen in order to interact with the app or game. Now here, we can add two buttons at the top, one for changing the color of your gaming components and the other is for exiting the Unity game.
The UnityPlayer object runs a static method to send native communications to the Unity game objects. To direct the action, we have to provide the game object we want to target, the name of the method on our script, and the parameters (always as a String) we want to pass.
Conclusion
In this article, we went through how to take advantage of Unity as a game developing Library to bring incredible games or AR/VR experiences to your Android applications. If you are interested to learn more about the Unity game engine and how to go about mastering it, Unity itself provides a plethora of high-quality resources for beginners and even advanced learners.