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
  screenTexture.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);


  if(!Application.isEditor)
  {
   // 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 Unknown

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.
    Blogger
    Facebook

14 comments:

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

    ReplyDelete
  2. if(!isProcessing && GameObject.Find("Ask Friend").GetComponent().enabled)
    StartCoroutine( captureScreenshot() );
    }

    How to implement this code can you guide me

    ReplyDelete
  3. if(!isProcessing && GameObject.Find("Ask Friend").GetComponent().enabled)
    StartCoroutine( captureScreenshot() );
    }

    How to implement this code can you guide me

    ReplyDelete
  4. I was very impressed by this post, this site has always been pleasant news. Thank you very much for such an interesting post. Keep working, great job! In my free time, I like play game

    Catninja.org
    Wormate-io.com

    ReplyDelete
  5. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! smm panels list

    ReplyDelete
  6. object referance not set an object

    ReplyDelete
  7. Excellent blog I visit this blog it's really awesome. The important thing is that in this blog content written clearly and understandable. The content of information is very informative.
    oracle fusion financials classroom training
    Workday HCM Online Training
    Oracle Fusion Financials Online Training
    Oracle Fusion HCM Online Training
    Oracle Fusion SCM Online Training
    Oracle Fusion HCM Classroom Training

    ReplyDelete
  8. I love this post.


    โปรโมชั่นGclub ของทางทีมงานตอนนี้แจกฟรีโบนัส 50%
    เพียงแค่คุณสมัคร Gclub กับทางทีมงานของเราเพียงเท่านั้น
    ร่วมมาเป็นส่วนหนึ่งกับเว็บไซต์คาสิโนออนไลน์ของเราได้เลยค่ะ
    สมัครสล็อตออนไลน์ >>> goldenslot
    สนใจร่วมลงทุนกับเรา สมัครเอเย่น Gclub คลิ๊กได้เลย

    ReplyDelete
  9. Very cool!

    เว็บไซต์คาสิโนออนไลน์ที่ได้คุณภาพอับดับ 1 ของประเทศ
    เป็นเว็บไซต์การพนันออนไลน์ที่มีคนมา สมัคร Gclub Royal1688
    และยังมีหวยให้คุณได้เล่น สมัครหวยออนไลน์ ได้เลย
    สมัครสมาชิกที่นี่ >>> Gclub Royal1688
    ร่วมลงทุนสมัครเอเย่นคาสิโนกับทีมงานของเราได้เลย

    ReplyDelete
  10. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. smm panels list

    ReplyDelete