FC2ブログ
    04 «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.» 06

    ハルシオンシステムの気ままBlog

    株式会社ハルシオンシステムのメンバーが送る、UnityやらJavaやらの技術的話題から、自社開発のアプリの宣伝とかとかのブログです。ほんと気ままにいきたいと思います。更新日は毎週 月 木でっす!

     

    【Unity】Unity5.4で正式リリースされたUnityWebRequestをちょっと触ったよ。 

    こんにちは。
    大坂です。

    山の日らしいです。
    山の天気が急変しやすいらしいので山に行く人は気を付けて下さい!
    僕はもちろん家にこもってます!

    さて、Unity5.4からUnityWebRequestが正式リリースされたみたいです。
    WWWに比べてAPIが使いやすくなってるみたいですね。
    ということで簡単な使い方です。

    using UnityEngine;
    using System.Collections;
    // 追加
    using UnityEngine.Networking;

    public class BlogTest : MonoBehaviour {

    // Use this for initialization
    void Start () {
    StartCoroutine(GetFile());
    }

    IEnumerator GetFile() {
    UnityWebRequest request = UnityWebRequest.Get("http://example.com/test.txt");
    yield return request.Send();

    // 何らかのエラーがあったら
    if(request.isError) {
    // エラー処理
    Debug.Log("エラー");
    } else {
    // レスポンスコードを見る
    if(request.responseCode == 200) {
    string test = request.downloadHandler.text;
    }
    }
    }
    }

    まぁこんな感じですかね?
    ほとんどWWWと変わりませんね。
    WWWに比べるとエラーチェックやレスポンスコード見るところがシンプルに書けるようになってますかね。
    WWWだとこんな感じですもんね。
        //何らかのエラーがあったら
    if(!string.IsNullOrEmpty(www.error)) {
    }

    //レスポンスコードを見る
    responseHeaders["STATUS"]に「HTTP/1.1 200 OK」とか入ってるので切り分けるか文字列探すか。
    if(www.responseHeaders.Count > 0 && www.responseHeaders["STATUS"].Contains("200")) {
    }


    あとは「DownloadHandlerScript」を使うとデータ受信してるときとかした後とかの処理が便利みたいです。
    Unity公式にあるサンプルだとログ出すようなのがあったのでとりあえずそれを使ってみる。
    一応コードも貼っておきます。細かい説明は公式に。
    using UnityEngine;
    using System.Collections;
    using UnityEngine.Networking;

    public class LoggingDownloadHandler : DownloadHandlerScript {

    // Standard scripted download handler - will allocate memory on each ReceiveData callback
    public LoggingDownloadHandler() : base() {
    }

    // Pre-allocated scripted download handler
    // Will reuse the supplied byte array to deliver data.
    // Eliminates memory allocation.
    public LoggingDownloadHandler(byte[] buffer) : base(buffer) {
    }

    // Required by DownloadHandler base class. Called when you address the 'bytes' property.
    protected override byte[] GetData() { return null; }

    // Called once per frame when data has been received from the network.
    protected override bool ReceiveData(byte[] data, int dataLength) {
    if(data == null || data.Length < 1) {
    Debug.Log("LoggingDownloadHandler :: ReceiveData - received a null/empty buffer");
    return false;
    }

    Debug.Log(string.Format("LoggingDownloadHandler :: ReceiveData - received {0} bytes", dataLength));
    return true;
    }

    // Called when all data has been received from the server and delivered via ReceiveData
    protected override void CompleteContent() {
    Debug.Log("LoggingDownloadHandler :: CompleteContent - DOWNLOAD COMPLETE!");
    }

    // Called when a Content-Length header is received from the server.
    protected override void ReceiveContentLength(int contentLength) {
    Debug.Log(string.Format("LoggingDownloadHandler :: ReceiveContentLength - length {0}", contentLength));
    }
    }

    各コールバックはこんな感じで呼ばれます。
    ReceiveData:データを読み取った時
    CompleteContent:読み取り完了した時
    ReceiveContentLength:ヘッダーからデータの長さを受け取った時

    で、用意したクラスはこんな感じでUnityWebRequestに渡してあげれば使えます。
        UnityWebRequest request = UnityWebRequest.Get("http://example.com/test.txt");
    LoggingDownloadHandler handler = new LoggingDownloadHandler();
    request.downloadHandler = handler;

    ということで、さらっと簡単な使い方でした!

    では今週もこれにて!
    また来週ノシ

    Category: 開発日記(Unity)

    tb 0 : cm 0   

    コメント

    コメントの投稿

    Secret

    トラックバック

    トラックバックURL
    →http://halcyonsystemblog.jp/tb.php/328-b6a31941
    この記事にトラックバックする(FC2ブログユーザー)