Unity 5.1.3-f1 on MacOS X 10.8.5
入門
https://plot.ly/
というグラフ描画サービスをUnityからアクセスしたい。
でも、RESTなんたらはよく分からない。
とりあえず、入門してみた。
UnityでRESTを使った例は以下で見つかった。
http://enpel.hateblo.jp/entry/2012/10/15/200048
実際のAPIについては
https://plot.ly/rest/
の 「POST/clientresp
」をクリックすると表示される例を元にコーディングした。
code
using UnityEngine;
using System.Collections;
public class restTestCS : MonoBehaviour {
IEnumerator getRestResponse()
{
string url = "https://plot.ly/clientresp";
string dataStr =
"un=7of9&" +
"key=bkzm2ze0iz&" +
"origin=plot&" +
"platform=lisp&" +
"args=[[0, 1, 2], [3, 4, 5], [1, 2, 3], [6, 6, 5]]&" +
"kwargs={\"filename\": \"plot from api\"," +
"\"fileopt\": \"overwrite\"," +
"\"style\": {" +
"\"type\": \"bar\"" +
"}," +
"\"traces\": [1]," +
"\"layout\": {" +
"\"title\": \"experimental data\"" +
"}," +
"\"world_readable\": true" +
"}";
byte [] data = System.Text.Encoding.UTF8.GetBytes (dataStr);
WWW www = new WWW (url, data);
yield return www;
int nop = 1; // for breakpoint
}
void Start () {
StartCoroutine ("getRestResponse");
}
void Update () {
}
}
int nop = 1
の行でブレーク設定して上記を実行して、イミディエイトにてwww.textを見ると以下の文字列が取得できた。
> www.text
"{\"url\": \"https://plot.ly/~7of9/127\", \"message\": \"\", \"warning\": \"\", \"filename\": \"plot from api\", \"error\": \"\"}"
とりあえず成功したようだ。
補足
なお、上記のコードを現時点で実行すると、以下のエラーが出るようになっている。理由としては、API Keyをbkzm2ze0iz
から変更済みだから。
> www.text
"{\"url\": \"\", \"message\": \"\", \"warning\": \"\", \"filename\": \"\", \"error\": \"Aw, snap! You tried to use our API as the user '7of9', but the supplied API key doesn't match our records. You can view your API key at plot.ly/settings.\\n\\nYou're most likely getting this message because your local credentials file isn't synced with the Plotly server you're communicating with.\\n\\nGo to plot.ly/<language>/getting-started (e.g., plot.ly/python/getting-started) for more information.\\n\\nMake sure that you're logged in as 7of9.\\n\\nNeed help? Feedback? Write support@plot.ly.\"}"
API Keyはplot.lyでログインして Setting -> API settingsの画面で表示される。
Generate a new key
でAPI Keyを変更可能。
コードの以下の部分は、それぞれのユーザのidとAPI Keyを設定して使う。
"un=7of9&" +
"key=bkzm2ze0iz&" +
やっていること
上記のコードでやっているのは、データのx,y座標を指定して、グラフを作成しているようだ。
既存データの読込みをやろうとしていたら、データの追加(とグラフの作成)ができていた。
実際に追加されたデータとグラフは以下で見ることが出来る。
https://plot.ly/128/~7of9/
https://plot.ly/127/~7of9/
www.text
で戻ってくるurl (https://plot.ly/~7of9/127
)を開くとグラフが見えるということらしい。