Tweet from Unity WebGL
Mobile-ready script for tweeting from Unity WebGL.
com.gigacee.tweetfromunitywebgl 
Install via UPM
Add to Unity Package Manager using this URL
https://www.pkglnk.dev/tweetfromunitywebgl.git?path=Assets/Plugins/TweetFromUnityWebGL README Markdown
Copy this to your project's README.md
## Installation
Add **Tweet from Unity WebGL** to your Unity project via Package Manager:
1. Open **Window > Package Manager**
2. Click **+** > **Add package from git URL**
3. Enter:
```
https://www.pkglnk.dev/tweetfromunitywebgl.git?path=Assets%2FPlugins%2FTweetFromUnityWebGL
```
[](https://www.pkglnk.dev/pkg/tweetfromunitywebgl)README
Tweet from Unity WebGL
Mobile-ready script for tweeting from Unity WebGL.
ζ₯ζ¬θͺγ«γγθͺ¬ζ / Explanation in Japanese
Demo
Basic Usage
This script uses .jslib plugin. You can tweet from WebGL by calling TweetFromUnity() in TweetFromUnityWebGL.jslib as follows:
using UnityEngine;
#if !UNITY_EDITOR && UNITY_WEBGL
using System.Runtime.InteropServices;
#endif
public class Demo1 : MonoBehaviour
{
#if !UNITY_EDITOR && UNITY_WEBGL
[DllImport("__Internal")]
private static extern string TweetFromUnity(string rawMessage);
#endif
public void Tweet()
{
#if !UNITY_EDITOR && UNITY_WEBGL
TweetFromUnity("Tweet Message");
#endif
}
}
When you run your game on PC, twitter.com will be opened in your browser by this JavaScript code:
window.open("https://twitter.com/intent/tweet?text=" + message, "_blank");
When mobile, the twitter app is launched by this:
location.href = "twitter://post?message=" + message;
:warning: If the twitter app is not installed in your mobile, this script won't work.
Sample1_Tweet scene is an example of it.
Tweet with Screenshot
You can also tweet with a screenshot of your game. Here's an example of using Imgur:
// Original code from https://github.com/ttyyamada/TweetWithScreenShotInWebGL
// Licensed under https://github.com/ttyyamada/TweetWithScreenShotInWebGL/blob/master/LICENSE
using System;
using System.Collections;
using System.Xml.Linq;
using UnityEngine;
using UnityEngine.Networking;
#if !UNITY_EDITOR && UNITY_WEBGL
using System.Runtime.InteropServices;
#endif
public class Demo2 : MonoBehaviour
{
[SerializeField] private string _imgurClientId;
#if !UNITY_EDITOR && UNITY_WEBGL
[DllImport("__Internal")]
private static extern string TweetFromUnity(string rawMessage);
#endif
public void TweetWithScreenshot()
{
StartCoroutine(TweetWithScreenshotCo());
}
private IEnumerator TweetWithScreenshotCo()
{
yield return new WaitForEndOfFrame();
Texture2D tex = ScreenCapture.CaptureScreenshotAsTexture();
var wwwForm = new WWWForm();
wwwForm.AddField("image", Convert.ToBase64String(tex.EncodeToJPG()));
wwwForm.AddField("type", "base64");
// Upload to Imgur
UnityWebRequest www = UnityWebRequest.Post("https://api.imgur.com/3/image.xml", wwwForm);
www.SetRequestHeader("AUTHORIZATION", "Client-ID " + _imgurClientId);
yield return www.SendWebRequest();
var uri = "";
if (!www.isNetworkError)
{
XDocument xDoc = XDocument.Parse(www.downloadHandler.text);
uri = xDoc.Element("data")?.Element("link")?.Value;
// Remove Ext
uri = uri?.Remove(uri.Length - 4, 4);
}
#if !UNITY_EDITOR && UNITY_WEBGL
TweetFromUnity($"Tweet Message%0a{uri}");
#endif
}
}
Sample2_TweetWithScreenshot scene is an example of it.
Special Characters
- line-break :
%0a - hashtag (#) :
%23
Example:
TweetFromUnity("You can include line-breaks%0aand hashtags in your tweet message!%0a%0a%23TweetFromUnityWebGL");
β
You can include line-breaks
and hashtags in your tweet message!
#TweetFromUnityWebGL
Installation
Package Manager
https://github.com/gigacee/TweetFromUnityWebGL.git?path=Assets/Plugins/TweetFromUnityWebGL
Manual
Copy Assets/Plugins/TweetFromUnityWebGL/TweetFromUnityWebGL.jslib to your project.
:warning: Be sure to put it in Assets/Plugins/ .
Comments
No comments yet. Be the first!
Sign in to join the conversation
Sign In