In continuation to my previous blogs Native Android in Unity and Native Android text sharing in Unity, will consider another example of native Android screenshot sharing functionality in the unity app.
If you have not read the previous blogs, I would strongly recommend to read them first. You can read them on the links below.
As the number says, an image gains more attention than a normal text while sharing. It’s better to challenge a player with your own high score screenshot in games instead of sharing just a plain text.
As in the previous post, we saw Android uses an Intent class object to perform any sharing operation and we can use the same Intent
class in unity using AndroidJavaClass and AndroidJavaObject.
We can share any image (existing or captured screenshot) in native Android using the same Intent
class. As we saw in the previous example of native text sharing, we can set the type of sharing to an Intent object. We will set the type of sharing to “image/png” in our case.
Let’s have a look at its native android code first.
//Java
String screenShotPath = "fireblock_highscore.png";
Uri uriToImage = Uri.fromFile(new File("file://" + screenShotPath));
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 shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSubject);
shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage);
shareIntent.setType("image/png");
startActivity(Intent.createChooser(shareIntent, "Share your high score"));
And the Kotlin code will be
//Kotlin
val screenShotPath = "fireblock_highscore.png"
val uriToImage = Uri.fromFile(File("file://" + screenShotPath))
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 shareIntent = Intent()
shareIntent.action = Intent.ACTION_SEND
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage)
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSubject)
shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage)
shareIntent.type = "image/png"
startActivity(Intent.createChooser(shareIntent, "Share your high score"))
Now let’s have a look at how we can use the same native Android screenshot share functionality in a unity app.
The first thing we need is the screenshot. Unity provides a class named ScreenCapture which has a public static method CaptureScreenshot which do the same functionality for us.
This method has two variants (method overloading) but will use the one which takes two parameters as below. The java code for the same will be
CaptureScreenshot(string filename, int superSize)
This method takes the first parameter as the file name in string
format with which the screenshot will be saved and the second parameter as the factor by which to increase resolution in int
format.
The code will look like
//C# code in unity
var screenshotName = "fireblock_highscore.png";
// wait for graphics to render
yield return new WaitForEndOfFrame ();
string screenShotPath = Application.persistentDataPath + "/" + screenshotName;
ScreenCapture.CaptureScreenshot (screenshotName, 1);
yield return new WaitForSeconds (0.5f);
Now that we got the screenshot captured in png format and saved at the path, we can share this image using the same Intent
we used while sharing the normal text. The only difference here will be the sharing type and the attaching of the image to the intent.
//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"));
Now that we got the intent object, we will set the share type as image/png
and attach the screenshot to it. In order to attach the screenshot, we need to convert it into Uniform Resource Identifier (URI) object from the file path.
//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";
//create URI for that intent
AndroidJavaClass uriClass = new AndroidJavaClass ("android.net.Uri");
AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse", "file://" + screenShotPath);
//set the type
intentObject.Call<AndroidJavaObject>("setType", "image/png");
//put image and string extra
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObject);
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. This step is the same what we did while sharing the text.
//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);
The entire script for Android native screenshot 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.