To get this live demo to work, you will need an Imgur Client ID and a Discord Webhook. See the next section how to get these values. When you have them, copy to clipboard and click the Paste button on screen. Simply hitting CTRL+V on your keyboard will not work. This is a known issue for Unity WebGL builds.
- Copy
Assets/Scripts/ShareUtil.cs
to your project - Create a new game object and attach the
ShareUtil
component - Fill in the ImgurClientId and DiscordWebhook parameters in the inspector. These are important
- Add a discord avatar image, it should be a publicly accessible image that will be used as an avatar by the webhook when posting in Discord
- Call
PostToDiscord
and/orPostToTwitter
The main code responsible for posting is the ShareUtil
class to be found in Assets/Scripts/ShareUtil.cs
The different functions have a coroutine version where the web requests will be handled. You can opt to call the coroutine or the function. Each function is documented in code to further ddefine what the function does and what each parameter is for.
This code uses Twitter Web Intents for tweeting. This is a very convenient method as we don't need the user to authenticate their account to our app. The user also has the option to view and edit the tweet before actual posting.
The problem with this approach is that we can't attach a screenshot to the tweet. To do so, we have to use Imgur (a image sharing service) and append the imgur url of the image so it will appear with the tweet.