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.
    Blogger
    Facebook

40 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
  16. Thanks for above information, I like it. check it once Android Online Training Hyderabad

    ReplyDelete
  17. I want to share my game link only on WhatsApp at a single tap....plz help me

    ReplyDelete
  18. Rajasthan Royals will kick-off their season with an away fixture against Sunrisers Hyderabad on April being unfaithful, followed by a home game against Delhi Daredevils on April 11 at Sawai Mansingh Stadium in Jaipur.
    Kolkata Knight Riders – KKR 2018 Schedule & Tickets
    Buy RCB 2018 Jersey From Amazon & Flipkart

    ReplyDelete
  19. good luck i always check your blog roz

    ReplyDelete
  20. I am happy to find this post Very useful for me, as it contains lot of information

    Android Training Institute in Chennai

    android classes in chennai

    ReplyDelete

  21. The UFC initially planned an abysmal title bout between Nurmagomedov in addition to The Ultimate Fighter: Team Lesnar vs. Team dos Santos welterweight winner Tony Ferguson, howeverit was canceled due to the Russian with medical issues connected with his weight reduction weigh-in day.Ferguson eventually won the interim title at UFC 216 against Kevin Lee.Ferguson was subsequently expected to fulfill Nurmagomedov in UFC 223, along with the winner being crowned the undisputed winner (because McGregor could be stripped off of this title whenever the bout happened ). Subsequently, it was scrapped once more (fourth moment ) since Ferguson got hurt, and after a few chances to get an opponent, Nurmagomedov eventually won the vacant title against Al Iaquinta. [9] [10] This card has been marked by an attack on a bus containing lots of fighters scheduled to compete in case (such as Nurmagomedov and 2 teammates), ran by McGregor and his team.

    Jussier Formiga was scheduled to confront Sergio Pettis in January 2017 in UFC Fight Night: Rodríguez vs. Penn.. However, Formiga pulled out of this struggle for undisclosed reasons.The pairing was rescheduled for this particular occasion.
    https://ufc229fightlive.com/

    ReplyDelete
  22. Perhaps you have finished to this? Well, now arrive in the following step.Gradually, I will take you to the limitation to finish it quickly. Now grab your telephone and swap the candy and try to wash all the icing. This individual move is very likely to produce the ample space so that you can quickly produce unique candies. Along
    Puzzle out to make the specific candies as many as possible. The individual candy is your trick to maneuver the amount economically and quickly. Candy Crush

    ReplyDelete
  23. Your website is really cool and this is a great inspiring article. best backlinks

    ReplyDelete
  24. When there you get a chance attempt to coincide with all the candy close to the bottom of the area. By making this motion likely, you may produce the chance to change a substantial variety of candy. Normally, this produces frequently a chain reaction, and in the Crash video game, the show response usually means a lot of chances to locate the free unique candy. Candy Crush

    ReplyDelete
  25. During this website, you will see this shape, i highly recommend you learn this review.
    https://qualityseopackages.com/qspx-managed-whitelabel-seo-services/

    ReplyDelete