Skip to content

Instantly share code, notes, and snippets.

@divide-by-zero
Last active February 8, 2018 15:27
Show Gist options
  • Save divide-by-zero/a1da265c9225a5d905c7c1423267b278 to your computer and use it in GitHub Desktop.
Save divide-by-zero/a1da265c9225a5d905c7c1423267b278 to your computer and use it in GitHub Desktop.
Unity(WebGL)からGyazoAPIを使って画像をアップロードする ref: https://qiita.com/divideby_zero/items/8109a508afe91fee82ac
[Serializable]
public class GyazoResponse
{
public string image_id;
public string permalink_url;
public string thumb_url;
public string url;
public string type;
}
using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class GyazoUploadTest1 : MonoBehaviour
{
public Button _sendButton;
public Text _timeText;
public void Start()
{
//クリックしたらスクリーンショットを撮って、Gyazoにアップロード
_sendButton.onClick.AddListener(() =>
StartCoroutine(UploadGyazoIterator("取っておいたAccessTokenを書きねぇ"))
);
}
private IEnumerator UploadGyazoIterator(string accesstoken)
{
//スクリーンショット取得
yield return new WaitForEndOfFrame();
var tex = ScreenCapture.CaptureScreenshotAsTexture();
var gyazoUploadUrl = "https://upload.gyazo.com/api/upload";
var form = new WWWForm();
form.AddField("access_token", accesstoken);
form.AddBinaryData("imagedata", tex.EncodeToPNG(), "screenshot.png", "image/png");
using (var request = UnityWebRequest.Post(gyazoUploadUrl, form))
{
yield return request.SendWebRequest();
var response = request.downloadHandler.text;
Debug.Log("Gyazo Upload Response:\n" + response);
}
UnityEngine.Object.Destroy(tex);//後始末
}
void Update()
{
//現在時間を表示
_timeText.text = DateTime.Now.ToString();
}
}
using (var request = UnityWebRequest.Post(gyazoUploadUrl, form))
{
yield return request.SendWebRequest();
var response = request.downloadHandler.text;
Debug.Log("Gyazo Upload Response:\n" + response);
//パース処理+ブラウザによるOpen処理
var gyazoResponseObj = JsonUtility.FromJson<GyazoResponse>(response);
Application.OpenURL(gyazoResponseObj.permalink_url);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment