Native Android text sharing in Unity


Native Android in Unity
In continuation to my previous blogs Native Android in Unity, will consider another example of native Android text sharing functionality in the unity app. This is a very common requirement in any unity app targeting Android platform to share the high score or challenge other players with a text message or a screenshot of the high score. The sharing can be done via any of the app available on the user’s device which supports sharing.
Android provides a native share functionality to do the same where we provide the text and/or the image to share to the Android system and Android provides a list of available apps on the device to share, where the user can pick one app to perform the share action.

Native Android text sharing in Unity
Now let’s see first how sharing works in native Android.

Android uses Intent to do the same.
For those who are new to native Android, An intent is an abstract description of an operation to be performed.
In an intent, we can set the action to be performed (sharing in our case), set the type of extras we will share ( text, image or any other format), pass all the extras to be shared (text and/or image) and finally call the Android system to provide the list of the available apps in the device which accepts this type of sharing.

The java code for the same will be
  
//Java
String shareSubject = "I challenge you to beat my high score in Fire Block";
String shareMessage = "I challenge you to beat my high score in Fire Block. " +
"Get the Fire Block app from the link below. \nCheers\n\n" +
"http://onelink.to/fireblock";

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSubject);
sendIntent.putExtra(Intent.EXTRA_TEXT, shareMessage);
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, "Share Via"));
And the Kotlin code will be
  
//Kotlon
val shareSubject = "I challenge you to beat my high score in Fire Block"
val shareMessage = "I challenge you to beat my high score in Fire Block. " +
"Get the Fire Block app from the link below. \nCheers\n\n" +
"http://onelink.to/fireblock"

val sendIntent = Intent()
sendIntent.action = Intent.ACTION_SEND
sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSubject)
sendIntent.putExtra(Intent.EXTRA_TEXT, shareMessage)
sendIntent.type = "text/plain"
startActivity(Intent.createChooser(sendIntent, "Share Via"))
Let’s see how we can use the same native Android share functionality in a unity app. As in the previous post about Native Android in Unity we learnt that we can use all the native android classes in unity using AndroidJavaClass and AndroidJavaObject.

Let’s use these classes and go step by step. First will create an AndroidJavaClass object of Intent class and will create and AndroidJavaObject of that class.
  
//C# code in unity
//create intent for action send
AndroidJavaClass intentClass = new
AndroidJavaClass("android.content.Intent");
AndroidJavaObject intentObject = new
AndroidJavaObject("android.content.Intent");

//set action to that intent object
intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string> ("ACTION_SEND"));

Next will set the share type as text and set extra subject and text to be shared.
  
//C# code in unity
var shareSubject = "I challenge you to beat my high score in" +
"Fire Block";

var shareMessage = "I challenge you to beat my high score in Fire
Block. " +
"Get the Fire Block app from the link below.
\nCheers\n\n" +
"http://onelink.to/fireblock";

//set the type as text and put extra subject and text to share
intentObject.Call<AndroidJavaObject>("setType", "text/plain");
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string> ("EXTRA_SUBJECT"), shareSubject);
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string> ("EXTRA_TEXT"), shareMessage);

Now that we have the intent object ready with all the extras to be shared, let’s call the Activity class’ createChooser method and pass the intent object along with the text to be displayed over the apps list.
  
//C# code in unity
//create current activity object
AndroidJavaClass unity = new
AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity =
unity.GetStatic<AndroidJavaObject> ("currentActivity");

//call createChooser method of activity class
AndroidJavaObject chooser =
intentClass.CallStatic<AndroidJavaObject> ("createChooser",
intentObject, "Share your high score");
currentActivity.Call ("startActivity", chooser);

Native Android text sharing in Unity
The entire script for Android native text sharing in unity (C#) would be

The above script can be used in any unity app targeting Android platform to share a text or high score natively in Android.

Update: