Created
February 11, 2015 08:03
-
-
Save fum1h1ro/2e52d8fc8551e5ab8831 to your computer and use it in GitHub Desktop.
UnityからスクショをSlackに投げる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using MiniJSON; | |
using KeyValue = System.Collections.Generic.Dictionary<string, string>; | |
public class CaptureSendSlack : MonoBehaviour { | |
private static string CAPTURE_DIRECTORY; | |
private static string CAPTURE_FILENAME = "slack_share.png"; | |
private static string CAPTURE_PATH; | |
private bool _isCapturing = false; | |
private string _message; | |
private KeyValue _channels = new KeyValue(); | |
public string _channel; | |
public string _apiToken; | |
// | |
public string apiToken { | |
get { return _apiToken; } | |
set { _apiToken = value; } | |
} | |
// | |
void Start() { | |
StartCoroutine(get_channel_list()); | |
} | |
// Update is called once per frame | |
void Update() { | |
if (_isCapturing) { | |
if (System.IO.File.Exists(CAPTURE_PATH)) { | |
_isCapturing = false; | |
StartCoroutine(file_upload()); | |
} | |
} | |
} | |
void OnGUI() { | |
if (GUILayout.Button("SHOT")) Capture("hogehoge"); | |
} | |
// | |
public void Capture(string m) { | |
if (!_isCapturing) { | |
#if UNITY_EDITOR | |
if (CAPTURE_PATH == null) { | |
CAPTURE_PATH = CAPTURE_FILENAME; | |
} | |
#else | |
if (CAPTURE_DIRECTORY == null) { | |
CAPTURE_DIRECTORY = Application.persistentDataPath; | |
CAPTURE_PATH = System.IO.Path.Combine(CAPTURE_DIRECTORY, CAPTURE_FILENAME); | |
System.IO.Directory.CreateDirectory(CAPTURE_DIRECTORY); | |
} | |
#endif | |
if (System.IO.File.Exists(CAPTURE_PATH)) { | |
System.IO.File.Delete(CAPTURE_PATH); | |
} | |
_isCapturing = true; | |
_message = m; | |
Application.CaptureScreenshot(CAPTURE_FILENAME); | |
} | |
} | |
string escape(string txt) { | |
return System.Uri.EscapeUriString(txt); | |
} | |
string make_url(string action) { | |
return string.Format("https://slack.com/api/{0}?token={1}", action, _apiToken); | |
} | |
string channel_id(string cname) { | |
return _channels[cname]; | |
} | |
IEnumerator get_channel_list() { | |
WWW www = new WWW(make_url("channels.list")); | |
yield return www; | |
_channels.Clear(); | |
if (string.IsNullOrEmpty(www.error)) { | |
var json = Json.Deserialize(www.text) as Dictionary<string, object>; | |
var channels = json["channels"] as List<object>; | |
foreach (Dictionary<string, object> o in channels) { | |
string cname = o["name"] as string; | |
string cid = o["id"] as string; | |
_channels[cname] = cid; | |
} | |
} | |
} | |
IEnumerator post_message() { | |
WWW www = new WWW(make_url("chat.postMessage") + string.Format("&channel={0}&text=HogeFromUnity", channel_id(_channel))); | |
yield return www; | |
} | |
IEnumerator file_upload() { | |
while (_channels.Count == 0) yield return new WaitForSeconds(0.5f); | |
if (System.IO.File.Exists(CAPTURE_PATH) && channel_id(_channel) != null) { | |
byte[] data = System.IO.File.ReadAllBytes(CAPTURE_PATH); | |
WWWForm form = new WWWForm(); | |
form.AddBinaryData("file", data, CAPTURE_FILENAME, "image/png"); | |
WWW www = new WWW(make_url("files.upload") + string.Format("&channels={0}&initial_comment={1}", channel_id(_channel), escape(_message)), form); | |
yield return www; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment