OpenNewWindowPlugin.jslib ※UTF-8Unity記事: 目次
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/* Unity 2018.2.11f1 jslib Plugin for WebGL 設置場所: Assets/Plugins/WebGL/ */ var OpenNewWindowPlugin = { OpenNewWindow: function(URL) { var url = Pointer_stringify(URL); window.open(url, '_blank', 'left=' + (screen.width-600)/2 + ', top=150, width=600, height=300'); } } mergeInto(LibraryManager.library, OpenNewWindowPlugin); |
TweetWithImgurLink.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
using System; using System.Collections; using System.IO; using System.Runtime.InteropServices; using System.Xml.Linq; using UnityEngine; using UnityEngine.Networking; /// <summary> /// /// Unity 2018.2.11f1 /// /// ClientIDは https://api.imgur.com/oauth2/addclient で取得 /// /// ClientIDを忘れた場合は https://imgur.com/account/settings/apps で確認 /// /// </summary> public class TweetWithImgurLink : MonoBehaviour { // Plugins [DllImport("__Internal")] private static extern void OpenNewWindow(string URL); // Inspector [SerializeField] private string imgurClientID; // ツイートボタンを押したときの処理 public void ClickTweetButton() { // 画像リンク付きツイート StartCoroutine(Tweet("コメント", "タグ")); } // 画像リンク付きツイート private IEnumerator Tweet(string comment, string tag) { // 1. スクリーンショットを撮る string fileName = string.Format("{0:yyyyMMddHmmss}", DateTime.Now); string filePath = Application.persistentDataPath + "/" + fileName + ".png"; ScreenCapture.CaptureScreenshot(filePath); float startTime = Time.time; while (File.Exists(filePath) == false) { if (Time.time - startTime > 6.0f) { yield break; } else { yield return null; } } byte[] imageData = File.ReadAllBytes(filePath); File.Delete(filePath); // 2. imgurへアップロード WWWForm wwwForm = new WWWForm(); wwwForm.AddField("image", Convert.ToBase64String(imageData)); wwwForm.AddField("type", "base64"); UnityWebRequest www; www = UnityWebRequest.Post("https://api.imgur.com/3/image.xml", wwwForm); www.SetRequestHeader("AUTHORIZATION", "Client-ID " + imgurClientID); yield return www.SendWebRequest(); string uploadedURL = ""; if (www.isNetworkError) { Debug.Log(www.error); } else { XDocument xDoc = XDocument.Parse(www.downloadHandler.text); uploadedURL = xDoc.Element("data").Element("link").Value; } // 3. 画像リンク付きツイート string tweetURL = "http://twitter.com/intent/tweet?text=" + comment + "&url=" + uploadedURL + "&hashtags=" + tag; #if UNITY_EDITOR Application.OpenURL(tweetURL); #elif UNITY_WEBGL OpenNewWindow(tweetURL); #else Application.OpenURL(tweetURL); #endif } } |