Unity - Share Text Message / Game Link To Other Apps In Android

Adding the capability to Send Text Data from our game to other Android apps will drastically increase the number of downloads that our app / game gets. Wonder how? Well, we will add the Share App feature to our game with a single click of a button and there you go, it definitely helps your users to share your game among their friends (on WhatsApp, Facebook, Gmail, Twitter and what not!) easily.
We will demonstrate how to add the feature in this short post of ours.

Create a new C# Script named ShareApp and attach it to the Main Camera. Open the script and paste the following content to it

using UnityEngine;
using System.Collections;


public class ShareApp : MonoBehaviour {
 
 string subject = "WORD-O-MAZE";
 string body = "GAME LINK HERE + DESCRIPTION";

 public void shareText(){
 //execute the below lines if being run on a Android device
 #if UNITY_ANDROID
  //Refernece of AndroidJavaClass class for intent
  AndroidJavaClass intentClass = new AndroidJavaClass ("android.content.Intent");
  //Refernece 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_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
  currentActivity.Call ("startActivity", intentObject);
 #endif
  
 }
 
} 

Save the script and switch to Unity. Create a Unity Button and call the function shareText, onClick of the button.

Share App / Text - Unity Android

Once you build this, you will see something like the below screenshot, from our game, when you click on the Share Text button.

Share App / Text - Unity Android

See you around.
Share on Google+

About Sujit Horakeri

Sujit Horakeri is a game freak just like any other next door guy you would come across. He is a Web Developer by Profession, Game Developer by Choice.
Connect with him on:
    Blogger
    Facebook

29 comments:

  1. Hi Sujit, how can i send a text with an image? thanks!

    ReplyDelete
  2. I am curious why yours shows as "Share Via" and then asks the user to choose an app and mine shows like this. http://i.imgur.com/NZjQh9s.png

    I would like to remove the "Just once" and "Always" part and force it to always ask the user what app to share with. Any ideas? Thanks

    ReplyDelete
    Replies
    1. Share Via is shown when you are sharing any app or content for the first time. If you have shared a content using, say twitter, the last time, that will be populated first. Regardless, you can choose other apps to share the content.
      As far as clearing this selection is concerned, I'm not sure as to where this can be done. I'll let you know once I figure it out.

      Delete
  3. Hi Sujit, I have tried your script but it's not share my text on Facebook.

    ReplyDelete
    Replies
    1. For Facebook you need to download the FB Unity SDK and then use their methods. facebook has ots security it won't allow to share without login.

      Delete
  4. thank you so much for this script, very helpful : )
    just one question, how can i have the same functionality in ios?
    any suggestion? thank you!

    ReplyDelete
  5. Hey it is useful but why it is not sharing text on Facebook ???
    Whats the way to share link to facebook ...

    ReplyDelete
    Replies
    1. https://www.facebook.com/sharer/sharer.php?u=" + System.Uri.EscapeUriString(PlayStoreLink)

      Delete
    2. Add your app link like , it will work on fb too

      string body ="https://play.google.com/store/apps/details?id=com.Infocom.Teen_Patti";

      Delete
    3. Add your app link like , it will work on fb too

      string body ="https://play.google.com/store/apps/details?id=com.Infocom.Teen_Patti";

      Delete
  6. What about IOS please? share text on IOS through unity game

    ReplyDelete
  7. Thank u so much ! this was very helpful . but is there any way by which i can check if the user has actually shared the text or not ?

    ReplyDelete
    Replies
    1. You can check if the user has clicked the button through which you are sharing the text. Set a playerprefs flag and assign true when that button is pressed. But then there will be cases when the user has pressed the button and not really shared it.

      Delete
  8. working but in facebook share, text box is empty not getting text from script.

    I from unitydiy blog

    ReplyDelete
    Replies
    1. Add your app link like , it will work on fb too

      string body ="https://play.google.com/store/apps/details?id=com.Infocom.Teen_Patti";

      Delete
  9. I am having a weird problem...when i share my game on gmail or messaging it perfectly works...but in facebook it did not work...
    my link is this--https://play.google.com/store/apps/details?id=com.Crea2rsGameStudio.control


    but in fb it becomes this---https://play.google.com/store/apps/details?id=com.Crea2rsGameStudio.control%29


    plz help me...why it is happening

    ReplyDelete
  10. i have a question i am able to share an image with text but when i trie different apps it behaves differently eg on whatsapp it takes the image with the text if i tried facebook just takes the image without text please help

    ReplyDelete
  11. hi
    can i set defult app for sharing??
    i don’t want user choose desired app for sharing. i want just share my game via whatsapp.
    thanks

    ReplyDelete
  12. can anyone help me about ios???
    i want share my game in ios.

    ReplyDelete
  13. This is amazing tutorial. Thanks Sujit Horakeri.

    ReplyDelete
  14. The new Samsung Galaxy Note 8 will hopefully avoid the fiasco that was the bane of the Galaxy Note 7. With the Korean giant looking to recoup its multi-billion dollar losses, we have created a wishlist of the features which will be seen on the Samsung Galaxy Note 8. We are waiting with baited breath the release of the new device. Galaxy Note 8 Wishlist

    ReplyDelete
  15. Wonderful blog. Thanks for sharing. See this source to know how to spy on your spouse.

    ReplyDelete