Social Sharing Tutorial (Unity, Android) (Code)

17  comments

This post contains all the code that’s been written in the YouTube video.

You can read it at your own pace, although watching the video prior to looking at this code example is recommended.

NativeShareScript.cs

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

public class NativeShareScript : MonoBehaviour {
    public GameObject CanvasShareObj;

    private bool isProcessing = false;
    private bool isFocus = false;

    public void ShareBtnPress()
    {
        if (!isProcessing)
        {
            CanvasShareObj.SetActive(true);
            StartCoroutine(ShareScreenshot());
        }
    }

    IEnumerator ShareScreenshot()
    {
        isProcessing = true;

        yield return new WaitForEndOfFrame();

        Application.CaptureScreenshot("screenshot.png", 2);
        string destination = Path.Combine(Application.persistentDataPath, "screenshot.png");

        yield return new WaitForSecondsRealtime(0.3f);

        if (!Application.isEditor)
        {
            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"),
                "Can you beat my score?");
            intentObject.Call<AndroidJavaObject>("setType", "image/jpeg");
            AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
            AndroidJavaObject chooser = intentClass.CallStatic<AndroidJavaObject>("createChooser",
                intentObject, "Share your new score");
            currentActivity.Call("startActivity", chooser);

            yield return new WaitForSecondsRealtime(1);
        }

        yield return new WaitUntil(() => isFocus);
        CanvasShareObj.SetActive(false);
        isProcessing = false;
    }

    private void OnApplicationFocus(bool focus)
    {
        isFocus = focus;
    }
}

 

About the author 

Matt Rešetár

Matt is an app developer with a knack for teaching others. Working as a freelancer and most importantly developer educator, he is set on helping other people succeed in their Flutter app development career.

You may also like

  • hi
    your code working well but
    when i post on facebook not ganaracter app link
    please solve my problem

    thanks

  • hey I’m vijay and I’ve pasted and done exactly as shown in youtube tutorial but I got an caution error i.e : : tiled GPU pref. warning : Render Texture colour surface (480 x 854) was not cleared/discarded, doing camera.renderer/Camera.ImageEffects.
    plz help I’m stuck here hope u reply very soon, anyway thnx.

  • Hi, im using you tutorial and trying to develop a video sharing but when i try to upload/share on youtube the video has an error or something like that and dont appear.

    IEnumerator ShareScreenshot()
    {
    isProcessing = true;

    if (!Application.isEditor)
    {
    AndroidJavaClass intentClass = new AndroidJavaClass(“android.content.Intent”);
    AndroidJavaObject intentObject = new AndroidJavaObject(“android.content.Intent”);
    intentObject.Call(“setAction”, intentClass.GetStatic(“ACTION_SEND”));
    AndroidJavaClass uriClass = new AndroidJavaClass(“android.net.Uri”);
    AndroidJavaObject uriObject = uriClass.CallStatic(“parse”, “file://” + savedPath);
    intentObject.Call(“putExtra”, intentClass.GetStatic(“EXTRA_STREAM”),
    uriObject);
    intentObject.Call(“putExtra”, intentClass.GetStatic(“EXTRA_TEXT”),
    “#test”);
    intentObject.Call(“setType”, “video/mp4”);
    AndroidJavaClass unity = new AndroidJavaClass(“com.unity3d.player.UnityPlayer”);
    AndroidJavaObject currentActivity = unity.GetStatic(“currentActivity”);
    AndroidJavaObject chooser = intentClass.CallStatic(“createChooser”,
    intentObject, “Share video.”);
    currentActivity.Call(“startActivity”, chooser);

    yield return new WaitForSecondsRealtime(1);
    }

    isProcessing = false;
    }

  • Hi,

    Great tutorials as always.

    What if I wanted to share only a predefined image instead of a screen shot?
    I would like that by clicking the share button, the icon of my game would be shared with his link from the PlayStore. I tried to make some changes to your code, but it did not work. I am a beginner in C # and would be very grateful if you could answer me how.

    Regards

  • Hi ,
    I tried to integrate sharing functionality in my APP.
    I followed your YouTube tutorial and code , but it seem like my sharing options are not appearing.
    I want to know that where am going wrong.
    Sorry in Advance for bad english

    Regards ,
    Santosh

  • Any idea why this code works if I share to WhatsApp but doesn’t work if I share to discord App? It gives me a “A bad network request has occurred, please try again”
    error every time.

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
    >