Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Native Android text Share in Unity
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class NativeAndroidTextSharingInUnity : MonoBehaviour {
public Button shareButton;
private bool isFocus = false;
private bool isProcessing = false;
void Start () {
shareButton.onClick.AddListener (ShareText);
void OnApplicationFocus (bool focus) {
isFocus = focus;
private void ShareText () {
if (!isProcessing) {
StartCoroutine (ShareTextInAnroid ());
Debug.Log("No sharing set up for this platform.");
public IEnumerator ShareTextInAnroid () {
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" +
isProcessing = true;
if (!Application.isEditor) {
//Create intent for action send
AndroidJavaClass intentClass =
new AndroidJavaClass ("android.content.Intent");
AndroidJavaObject intentObject =
new AndroidJavaObject ("android.content.Intent");
("setAction", intentClass.GetStatic<string> ("ACTION_SEND"));
//put text and subject extra
intentObject.Call<AndroidJavaObject> ("setType", "text/plain");
("putExtra", intentClass.GetStatic<string> ("EXTRA_SUBJECT"), shareSubject);
("putExtra", intentClass.GetStatic<string> ("EXTRA_TEXT"), shareMessage);
//call createChooser method of activity class
AndroidJavaClass unity = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity =
unity.GetStatic<AndroidJavaObject> ("currentActivity");
AndroidJavaObject chooser =
("createChooser", intentObject, "Share your high score");
currentActivity.Call ("startActivity", chooser);
yield return new WaitUntil (() => isFocus);
isProcessing = false;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment