example of EasyHttpRpc
private EasyHttpRPC _easyHttpRPC;
private void Start()
{
// newするとサーバが起動する.CancellationTokenが必要.
// 任意でポート番号も指定できる.デフォルトは1234.
_easyHttpRPC = new EasyHttpRPC(this.GetCancellationTokenOnDestroy());
// 外から呼び出すメソッドを登録する.
_easyHttpRPC.RegisterRPC(nameof(Instantiate), Instantiate);
}
// 引数のNameValueCollectionにはGetのパラメータがそのまま入っている.
// Taskなので終了まで待ってからレスポンスを返せる.
private async Task<string> Instantiate(NameValueCollection arg)
{
await UniTask.SwitchToMainThread();
var argColor = arg["color"];
if (string.IsNullOrEmpty(argColor) || !ColorUtility.TryParseHtmlString(argColor, out var color))
{
color = Color.white;
}
var argCount = arg["count"];
if (!int.TryParse(argCount, out var count))
{
count = 0;
}
await _primitiveSupplier.Instantiate(color, count);
return "SUCCESS";
}
- Install the VSCode plugin REST Client.
- Clone this repository.
- Open and run Assets/Scenes/EasyHttpRPCExample.unity.
- Open http/rest.http in VSCode.
- Enter the address displayed on the screen into rest.http.
- Run API.
- VSCodeのプラグイン REST Client をインストールします。
- このリポジトリをクローンします。
- Assets/Scenes/EasyHttpRPCExample.unityを開いて実行します。
- http/rest.httpをVSCodeで開きます。
- 画面に表示されたアドレスをrest.httpに入力します。
- APIを実行します。
- instantiate
Instantiate cube. count, color. - count
Count instantiated cube. - error
throw error - post
post string