Native Android in Unity

Native Android in Unity
While developing unity games in C# targeting android platform, we always want to use few native android features in our game. These features can be showing notifications on certain actions in the game or can be sharing the high score with other players and inviting them to try our game using android native share. Android gives us all the possibilities to achieve these native android functionalities in unity app using C#.
Let’s take a very basic example of native android functionality ie showing a Toast using unity app.

For all those who are not familiar with what a toast is, toast is a simple message popup that appears at the bottom side (by default) of the screen with a custom message and for a long or short duration. A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. Toasts automatically disappear after a timeout.

Native Android in Unity
To show this toast in native android we need a simple call to the Toast class static method makeText which return us a Toast class object and then call show method on that object.
//Java Code
Context context = getApplicationContext();
CharSequence text = "This is a toast";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);;

//Kotlin Code
val text = "This is a toast"
val duration = Toast.LENGTH_SHORT

val toast = Toast.makeText(applicationContext, text, duration)

makeText accepts three arguments,
1. Context: the reference of current activity or application
2. Text to show (CharSequence): text needs to be displayed
3. Duration (int): duration for which the toast needs to be displayed

The duration value can choose between Toast.LENGTH_LONG or Toast.LENGTH_SHORT.
Now in order to show the same code in unity app, we need use the same Toast class of android but how?

using AndroidJavaClass and AndroidJavaObject.

AndroidJavaClass is the Unity representation of a generic instance of java.lang.Class whereas AndroidJavaObject is the Unity representation of a generic instance of java.lang.Object.

First, lets put a Platform #define directives because this particular piece of code will only be supported on the Android platform
//C# code
//our entire code

Next, create a new AndroidJavaClass object of Toast class.
//C# code
AndroidJavaClass toastClass =
new AndroidJavaClass("android.widget.Toast");
The constructor of AndroidJavaClass takes a string object as the argument. The string should be the fully qualified name (<packagename>.<classname>) of the class whose reference need to be created. In our case, the fully qualified name of Toast class will be android.widget.Toast which can be obtained from the official documents of Toast class here.

Now, as we know that makeText method of Toast class accepts three arguments, but the AndroidJavaObject.Call or AndroidJavaObject.CallStatic method accepts all the arguments as an object array so let’s create an object array with all the required arguments in it.
//C# code
//create an object array of 3 size
object[] toastParams = new object[3];

//create a class reference of unity player activity
AndroidJavaClass unityActivity =
new AndroidJavaClass ("com.unity3d.player.UnityPlayer");

//set the first object in the array
//as current activity reference
toastParams[0] =
unityActivity.GetStatic<AndroidJavaObject> ("currentActivity");

//set the second object in the array
//as the CharSequence to be displayed
toastParams[1] = "This is a Toast";

//set the third object in the array
//as the duration of the toast from
toastParams[2] = toastClass.GetStatic<int> ("LENGTH_LONG");
The thing to notice here is the AndroidJavaObject.GetStatic method. This method is used to get the value of a static field in an object type.

Now that we have the object array ready, let’s call the makeText method of Toast class with passing the object array as an argument.
//C# code
AndroidJavaObject toastObject =
("makeText", toastParams);
AndroidJavaObject.CallStatic method is used to call a static Java method on a class. This method returns us an AndroidJavaObject class object which is the Toast class object in our case. We can call the show method on this object now.
//C# code
toastObject.Call ("show");
AndroidJavaObject.Call method is used to call a Java method on an object (non-static).
The entire code put together will be

Native Android in Unity
This will show the required native toast. This toast can be shown on any button click, on start/update or any other method of any game object, on some action performed or network call response, anywhere.