2

I am trying to create a share button in Unity. Just one button that when is pressed it will show the social media applications that is installed on your phone, and allows the user to share. I keep finding tutorials over how to create a facebook share button or a twitter share button. But I just want to create a simple share button that allows you to share with every social media application. Here is an example:

EXAMPLE

I found a few assets, but not for sure if they will work right.

Assets: https://www.assetstore.unity3d.com/en/#!/content/37320
This asset allows you to share an image, I don't need to share an image, just text. But I thought it wouldn't be hard to modify it and only do text.

zhong
  • 21
  • 1
  • 1
  • 3

2 Answers2

1

There's two ways of doing this.
1. Create a native plugin, write wrapper code in Unity to call the native code (Probably the most widely used way to call native functions)
2. Write the code entirely in Unity, and use AndroidJavaObject to invoke the functions.


Option 1 - Native Java Code + Unity Wrapper
Here's a link I found on SO for the code for Sharing.
Here's a link to one of my older answers about plugins. You can modify the code there to fit your needs.

Option 2 - No native code.
This way is a little more interesting. We use Unity's AndroidJavaClass & AndroidJavaObject to eliminate the need of JARs altogether. Just stick the below code in a C# script, and call the function. (NOTE, I haven't tried this code, there may be errors. If there are, let me know and I'll edit my response)

private static AndroidJavaObject activity = null;

private static void CreateActivity () {
    #if UNITY_ANDROID && !UNITY_EDITOR
    if(activity == null)
        activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").
            GetStatic<AndroidJavaObject>("currentActivity");
    #endif

}

public static void ShareActivity (string title, string subject, string body) {
    CreateActivity();
    AndroidJavaObject sharingIntent = new AndroidJavaObject("android.content.Intent", "android.intent.action.SEND")
                      .Call<AndroidJavaObject>("setType", "text/plain")
                      .Call<AndroidJavaObject>("putExtra", "android.intent.extra.TEXT", body)
                      .Call<AndroidJavaObject>("putExtra", "android.intent.extra.SUBJECT", subject);

    AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", activity)
                      .CallStatic<AndroidJavaObject>("createChooser", sharingIntent, title);
    activity.Call("startActivity", intent);
}

Don't forget to add the activity to your AndroidManifest.xml!

Community
  • 1
  • 1
Venkat at Axiom Studios
  • 2,456
  • 1
  • 15
  • 25
0
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.InteropServices;

public class socioshare : MonoBehaviour
{
string subject = "Hey I am playing this awesome new game called SoccerCuby,do give it try and enjoy \n";
string body = "https://play.google.com/store/apps/details?id=com.KaliAJStudios.SoccerCuby";

public void OnAndroidTextSharingClick()
{
    //FindObjectOfType<AudioManager>().Play("Enter");
    StartCoroutine(ShareAndroidText());
}

IEnumerator ShareAndroidText()
{
    yield return new WaitForEndOfFrame();
    //execute the below lines if being run on a Android device
    //Reference of AndroidJavaClass class for intent
    AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
    //Reference of AndroidJavaObject class for intent
    AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
    //call setAction method of the Intent object created
    intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
    //set the type of sharing that is happening
    intentObject.Call<AndroidJavaObject>("setType", "text/plain");
    //add data to be passed to the other activity i.e., the data to be sent
    intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_SUBJECT"), subject);
    intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TITLE"), "TITLE");
    intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), subject);
    intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), body);
    //get the current activity
    AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
    //start the activity by sending the intent data
    AndroidJavaObject jChooser = intentClass.CallStatic<AndroidJavaObject>("createChooser", intentObject, "Share Via");
    currentActivity.Call("startActivity",jChooser);
}
}

unable to display the 1st string i.e "subject" in the message. the 2nd string "body" is being displayed accurately.rest everything is working Fine.