UnityWebRequest Example
Under ScriptEngine's INTEROP section, make sure to set the Objects mapping for "uwr".
TS
// Javascript
var uwr = require("uwr") // This is the exposed UnityWebRequestUsage object
uwr.GetTextFromUri("https://www.google.com", (text) => {
log(text)
})
CS
// C#
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
public class UnityWebRequestUsage : MonoBehaviour {
public void GetTextFromUri(string uri, Action<string> callback) {
StartCoroutine(GetText(uri, callback));
}
IEnumerator GetText(string uri, Action<string> callback) {
using (UnityWebRequest request = UnityWebRequest.Get(uri)) {
yield return request.Send();
if (request.result == UnityWebRequest.Result.ConnectionError) { // Error
callback(request.error);
} else { // Success
callback(request.downloadHandler.text);
}
}
}
}