Unity/Csharp/Request
提供: 初心者エンジニアの簡易メモ
Httpリクエストサンプル
public class ComScript : MonoBehaviour { public void Exec () { string url = "ttp://hogehoge.com/api?name1=" string param = "value1"; url += WWW.EscapeURL (param); StartCoroutine(Get (url)); } public IEnumerator Get (string url) { var request = new UnityWebRequest(); request.downloadHandler = new DownloadHandlerBuffer(); request.url = url; request.SetRequestHeader("Content-Type", "application/json; charset=UTF-8"); request.method = UnityWebRequest.kHttpVerbGET; yield return request.Send(); if (request.isError) { Debug.Log(request.error); } else { if (request.responseCode == 200) { Debug.Log(request.downloadHandler.text); } } } }
呼び出し側
GameObject gameObj = new GameObject(); ComScript com = gameObj.AddComponent<ComScript>(); com.Exec ();