Why would you need to use an Android Plug-In on Unity?
If you are working on a mobile game you will notice that there will be time in which you need to use a feature that is only native to a specific OS. There will be time you may want to access the camera, storage, etc.
For a basic introduction I’m going to share how to get the “Battery Percentage” of your android phone and add two numbers using a return method inside the Android Plug-In and make Unity get the information and display it on a UI Text.
What Will You Need?
Obviously you need to have Unity😂 but make sure you have the Android modules installed in your desired Unity version; also make note in what version you’re following this guide, depending on the Unity version you’re using is where the plug in must be stored.
Aaaaaand you will need Android Studio installed on your PC in order to create your plug-in but to also acquire the necessary API / SDKs for each desired version of Android.
PERSONAL NOTE: The SDK folder that Android Studio use to store all the Android API / SDKs version is also the folder I tell Unity to use. Even if Unity screams at me that it is not the recommended selection, this allows me to be updated to both minimum and targeted API / SDKs if I make changes directly on Android Studios. Also both the Android Plug-In and Unity must have the same minimum and targeted API /SDK version, if one of them is off the build on Unity will be canceled and it will throw some errors.
Once you have Unity and Android Studio on your system lets go and make a plug in😆
Creating A New Android Project
On Android Studio:
Press New Project -> Select “No Activity” or your desire preference
Fill out the following information
Once the project is done compiling, we are going to create our android plugin that is known on Android Studio as a “Module”.
Lets go to File -> New -> New Module
In the following windows select to create a Android Library and fill out the information and press Finish
Look for the module you just created, there will be a new folder inside your project, and navigate to the java folder, select the folder with your package name, right click it and go to New and select Java Class and names your Java Class
Take note of how you called your class, this is very important since you’re going to reference the class inside Unity.
Inside the class type the following code
Inside this class we have two method,
Add(): a method that will return a int of the sum of two numbers.
GetBatteryPercentage(): The battery percentage will be return as a int. This method will seem complicated but all you have to understand is that the reason for the if state is to identity the Android version on the device and depend on the version is how I’m retrieving the battery percentage.
Now lets build our plug in. Go to Build and select to Make Module
Once the build / module is done you will find your AAR file inside your Android Project folder go to the unityplugin folder -> build -> output -> aar.
Remember where it is stored since we’re going to use it on Unity soon.
On Unity create a simple scene with Two UI Text and Two Buttons
One Button is going to active the add method and display it in one of the UI Text; the other is going to activate the method to get the Battery Percentage from the plug in and display the information on the second UI Text
Now lets store the AAR file on unity. Inside your Plugins folder (if you don’t have it create it), you will create another folder named Android, create another folder named libs and store the AAR folder. The naming convention must be followed as shown on the picture and must be written accordingly(Assets/Plugins/Android/libs)
Create a C# script in unity and place it on a Game Object; my script is called “AndroidNativeCodeRunnerManager”. Write the following code:
Place your UI Textbox on the script
Add to your Buttons the reference to the public methods
Now before building make sure both Unity and the Android Module are using the minim SDK / API and the targeted SDK / API
File->Build Setting-> Make sure you are on the Android Platform
Press Player Settings
And go to Player->other settings
Go to the Gradle Scripts folder and select the Gradle that has your Module / Plug-in name and double click it
Your going to see what is the minimum and targeted SDK of your Module / Plug-In
If everything is okay hit build, test it on a Android Device and VOALA
You created your first Unity project using a Android Plug-In
Personally it took me almost 3 day to make the battery percentage works but once I understood the issue I got it running and with this experience it seems I need to learn a little bit of JAVA 😂
TO BE CONTIUNUED😎