Unity - Take Screenshot And Share It On Social Media

There are times when you want to take a screenshot of the current screen and share it to the social media, and unfortunately Unity doesn't provide any internal implementation by which you can do this. But, using Android functionality we can easily do this with some scripting. Follow the below steps to achieve this:

1. Create a C# script named ShareScreenshot and paste the below code to it:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.IO;

public class ShareScreenshot : MonoBehaviour {
 private bool isProcessing = false;
 public float startX;
 public float startY;
 public int valueX;
 public int valueY;

 public void shareScreenshot(){

  if(!isProcessing && GameObject.Find("Ask Friend").GetComponent<Image>().enabled)
   StartCoroutine( captureScreenshot() );

 public IEnumerator captureScreenshot(){
  isProcessing = true;
  yield return new WaitForEndOfFrame();
  Texture2D screenTexture = new Texture2D(Screen.width*valueX/10000, Screen.height*valueY/10000,TextureFormat.RGB24,true);

  // put buffer into texture
  //screenTexture.ReadPixels(new Rect(0f, 0f, Screen.width, Screen.height),0,0);
  //create a Rect object as per your needs.
  screenTexture.ReadPixels(new Rect
                           (Screen.width*startX, (Screen.height*startY), Screen.width*valueX/10000, Screen.height*valueY/10000),0,0);

  // apply

  //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- PHOTO

  //byte[] dataToSave = Resources.Load<TextAsset>("everton").bytes;
  byte[] dataToSave = screenTexture.EncodeToPNG();
  string destination = Path.Combine(Application.persistentDataPath,System.DateTime.Now.ToString("yyyy-MM-dd-HHmmss") + ".png");
  File.WriteAllBytes(destination, dataToSave);

   // block to open the file and share it ------------START
   AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
   AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
   intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
   AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
   AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse","file://" + destination);
   intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObject);
   intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), "Which Football Club does this Rebus represent?\n" +
                                        "Download the game on play store at "+"\nhttps://play.google.com/store/apps/details?id=com.TGC.RebusFC&pcampaignid=GPC_shareGame");
   intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_SUBJECT"), "Which club is this?");
   intentObject.Call<AndroidJavaObject>("setType", "image/jpeg");
   AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
   AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
   // option one WITHOUT chooser:
   currentActivity.Call("startActivity", intentObject);
   // block to open the file and share it ------------END
  isProcessing = false;


Save the script and return to Unity editor.

2. Attach the script to Main Camera or any object that is present on the scene.

3. Call the shareScreenshot() method on click of a button.

4. Build and Run the game on your Android device to witness the magic right before your eyes.

See you around.
Share on Google+

About Sujit Horakeri

Sujit Horakeri is a game freak just like any other next door guy you would come across. He is a Web Developer by Profession, Game Developer by Choice.
Connect with him on:


  1. Hi, great tutorial, it says option 1 without chooser, is there an option with chooser?