といっても、neueccさんご本人がIssueを立てられているので、公式でもすぐサポートされると思いますが、そこはそれ。
流れとしては、UniRxにはCoroutineをObservableに変換するObservable.FromCoroutineがあるので、UnityWebRequestオブジェクトをいい感じにCoroutineにして、これをObservableに変換します。というか、ObservableWWWをベースにUnityWebRequestに合わせて修正したものを、UnityWebRequestのメソッドと引数に合わせてラップしていくだけです。
UnityWebRequestの基本的なCoroutineはこんな感じです。
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 System.Collections; | |
using UnityEngine; | |
using UnityEngine.Experimental.Networking; | |
public class UnityWebRequestUsage : MonoBehaviour | |
{ | |
void Start() | |
{ | |
StartCoroutine(GetText()); | |
} | |
IEnumerator GetText() | |
{ | |
using (UnityWebRequest request = UnityWebRequest.Get("http://unity3d.com/")) | |
{ | |
yield return request.Send(); | |
if (request.isError) // Error | |
{ | |
Debug.Log(request.error); | |
} | |
else // Success | |
{ | |
Debug.Log(request.downloadHandler.text); | |
} | |
} | |
} | |
} |
ファクトリーメソッドはGetのほか、Post、Put、Delete、HeadでRESTをカバーしていて、基本のGet以外にTexture用のGetTextureとAssetBundle用のGetAssetBundleもあります。実行はすべてSendで行われ、WWWのようにコンストラクト即実行とは違います。これに引っ掛かって少し悩みました。
これと同じことを、作成したObservableWebRequestでやるとこんな感じです。
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 UniRx; | |
public class ObservableWebRequestUsage : MonoBehaviour | |
{ | |
void Start() | |
{ | |
ObservableWebRequest.Get("http://unity3d.com/") | |
.Subscribe( | |
x => Debug.Log(x), // Success | |
ex => Debug.LogException(ex)); // Error | |
} | |
} |
基本的なテストは基本のGetで確認しましたが、他はこれからです。といっても、UnityWebRequestは標準で使う限りシンプルなので、間違える要素は少ないと思いますが。
0 コメント :
コメントを投稿