Share Button To Social Media Unity (android)
Solution 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)
privatestatic AndroidJavaObject activity = null;
privatestaticvoidCreateActivity () {
#if UNITY_ANDROID && !UNITY_EDITORif(activity == null)
activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").
GetStatic<AndroidJavaObject>("currentActivity");
#endif
}
publicstaticvoidShareActivity (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!
Solution 2:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.InteropServices;
publicclasssocioshare : 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";
publicvoidOnAndroidTextSharingClick()
{
//FindObjectOfType<AudioManager>().Play("Enter");
StartCoroutine(ShareAndroidText());
}
IEnumerator ShareAndroidText()
{
yieldreturnnewWaitForEndOfFrame();
//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.
Post a Comment for "Share Button To Social Media Unity (android)"