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; } }
hi
your code working well but
when i post on facebook not ganaracter app link
please solve my problem
thanks
Hello!
I’d like to help but you must’ve misspelled “ganaracter” so I don’t know exactly what you’re asking about.
hello,
when i post on Facebook not sharing app link
how to share 2 link in one post on Facebook?
how to share both image and link in one post on Facebook?
thanks
i have the same problem, can you tell me if you resolve it. thank you.
Sharing link on facebook is not allowed.
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.
Hello! I’m sorry but I have no idea how I could help you with this. Try asking on http://answers.unity3d.com
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;
}
Dik za zdrielanie 😉
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
very nice Thank you so much
is this code working for Android Oreo and above ?
Sadly, not on all Oreo devices. I can however recommend this plugin https://github.com/olokobayusuf/NatShare-API
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.
It works if I remove the picture when in confirmation screen in discord…
How in share Video is possible in this Code