Monday, February 3, 2014

Unity 3D: How to Post to Twitter from your Unity Game

Nobody can deny the power of social media in building an audience for your game. I'm going to be writing a few posts detailing the simplest ways I know of for sharing your game via some popular social media sites; today, I'm going to focus on Twitter.

I create almost all of my buttons with NGUI. If you're not using it, I've written a very simple post on getting started with it. If you're using NGUI, you'll call the ShareToTwitter () method in the OnClick () method of the button of your choice, passing in the text you'd like to share. Regardless, you just need to call this method with the text that you'd like to share, hashtags and all.

private const string TWITTER_ADDRESS = "http://twitter.com/intent/tweet";
private const string TWEET_LANGUAGE = "en";

void ShareToTwitter (string textToDisplay)
{
Application.OpenURL(TWITTER_ADDRESS +
            "?text=" + WWW.EscapeURL(textToDisplay) +
            "&lang=" + WWW.EscapeURL(TWEET_LANGUAGE));
}

So what are we doing here? Well, we're opening a URL, which in this case is the URL that Twitter uses to allow us to post to our account. On a PC, this will open the browser and attempt to post Twitter the textToDisplay parameter. On mobile devices, this will open the Twitter app or the browser, depending on what the user has.

You can build up the textToDisplay parameter however you like; just remember the 140-character limit. Include hashtags, a link, your score, whatever.

36 comments:

  1. I have one more with my game in ios. When the user goes to update their status through the game, they are being redirected to a browser even though they have the twitter app.

    how can I make it redirect to only the app and not the browser? I am really desperate to find this answer!

    ReplyDelete
    Replies
    1. I am even willing to pay upto $200 to fix these issues for me.

      Delete
    2. I'm not a big iOS guy, but my best guess is that while the Twitter app is installed, Twitter links are not associated with the Twitter app, but with the browser instead. This would be an iOS issue, not an issue with your app.

      Delete
  2. Thanks a lot! That was very helpful

    ReplyDelete
  3. This is awesome :) Any plans for Facebook integration tutorials? I always struggle with this sort of thing.

    ReplyDelete
    Replies
    1. Yes! As soon as I have time, that's going to be my next post. I'm glad it helped you out!

      Delete
    2. I just posted a simple way to post to Facebook. You can check it out here:

      http://unity3dtrenches.blogspot.com/2014/07/unity3d-how-to-post-to-facebook-from.html

      Delete
  4. This made my day! How bout tweeting with a picture ?
    Im thinking if it could only be done by saving your app image to users gallery
    and get it when posting.

    ReplyDelete
    Replies
    1. I'm thinking the same thing. I'll be addressing that in a future post. Thanks for reading!

      Delete
  5. This is excellent, thanks so much for sharing.

    Did anyone else have the issue of spaces showing up as + symbols on Twitter?

    ReplyDelete
    Replies
    1. I am having same issue. Everything works great on Android, and on iOS if twitter is not installed, but on iOS if twitter is installed it loads spaces as + in tweet.

      Did you ever figure this out?

      Delete
    2. In case anyone else ends up googling this:
      I ended up fixing this by doing the WWW.EscapeURL(textToDisplay) and then replacing the "+" with "%20". It seems to work on both platforms with and without twitter installed as expected.

      Delete
  6. Hi, well, I try this code but when anybody share on twitter, they can change the text to share. So, How Can I lock this ?

    Thanks!

    ReplyDelete
    Replies
    1. Unfortunately, you can't. Because we're opening the app itself to handle inputting text via the API, you can pre-form the text, but you can't prevent the user from changing it.

      Delete
    2. And can we share a screenshot intead ?

      Delete
    3. @Alberto: Yes, I'm almost positive you can. I'll be dealing with that in a future post. I just haven't had time to look into it yet.

      Delete
    4. Okey, Could you tell me when you make this post ?

      Thanks!

      Delete
    5. Hi again!

      So, you know when you can make twitter post ?

      I wait your answers

      Delete
    6. You know they do this in their own time and you get it for free, eh? How about a little patience?

      Delete
  7. This comment has been removed by the author.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Hey guys, just wondering if there is a simple way to check if the person successfully tweeted the tweet?
    Cheers in advance!!

    ReplyDelete
  10. I was wondering if there is a way to post an icon of your app or a custom graphic with your post. Thanks

    ReplyDelete
  11. hi any one can help me here! i tried the fb post same error like this twitter post, im new to c# here's the class:
    using UnityEngine;
    using System.Collections;

    public class twuittershare : MonoBehaviour {

    // Use this for initialization
    void OnMouseDown()
    {
    ShareToTwitter();
    }
    private const string TWITTER_ADDRESS = "http://twitter.com/intent/tweet";
    private const string TWEET_LANGUAGE = "en";

    void ShareToTwitter (string textToDisplay)
    {
    Application.OpenURL(TWITTER_ADDRESS +
    "?text=" + WWW.EscapeURL(textToDisplay) +
    "&lang=" + WWW.EscapeURL(TWEET_LANGUAGE));
    }
    }


    and found this error:
    Assets/twuittershare.cs(9,17): error CS1501: No overload for method `ShareToTwitter' takes `0' arguments


    tell me what to do please

    ReplyDelete
    Replies
    1. When you call ShareToTwitter() you need to have a string value inside the parenthesis.

      Delete
  12. Replies
    1. I don't think you'll find anyone that will write the script for you, but I will tell you that you're getting this error because you're not giving the ShareToTwitter () the parameters it expects. I'd recommend rather than worrying about this specific script, boning up on your basic C# skills, and then coming back to it:

      https://msdn.microsoft.com/en-us/library/aa288436(v=vs.71).aspx

      Good luck!

      Delete
  13. This works like a charm on PC and Andoid, but unfortunately not on a Mac or iPhone. On these platforms, nothing happens when i press the button in my game. Do you have any idea how this could be?

    Thanks!

    ReplyDelete
  14. Haha I use the same in my game. Simple and does the trick! Respect for taking the time to share it.
    Have you tried auto-attaching a screenshot though? That's something I've been working on without any success for a while...

    ReplyDelete
  15. For anyone wondering how to share an image with your tweet, just add the meta data to your html file as outlined here -

    https://dev.twitter.com/cards/types/summary-large-image

    obviously replacing out with your relevant info. twitter will find the meta data and the image will be added automatically.

    Thanks

    ReplyDelete
  16. hi
    i want to share my link to instagram and google plus by below methode .

    private const string TWITTER_ADDRESS = "http://twitter.com/intent/tweet";
    private const string TWEET_LANGUAGE = "en";

    void ShareToTwitter (string textToDisplay)
    {
    Application.OpenURL(TWITTER_ADDRESS +
    "?text=" + WWW.EscapeURL(textToDisplay) +
    "&lang=" + WWW.EscapeURL(TWEET_LANGUAGE));
    }

    is there a way?

    ReplyDelete
  17. hello ,
    its possible to share image and text by twitter url

    ReplyDelete