/// <summary> /// Coroutine to load a Texture2D from a URI. /// </summary> static public IEnumerable <Texture2D> ReadTextureEnum(Uri uri) { // Note!: Using `UnityWebRequestTexture` // is preferable to `Texture2D.LoadImage` // because it does not block the main Unity thread. UnityWebRequest request = UnityWebRequestTexture.GetTexture(uri); request.SendWebRequest(); while (!request.isDone) { yield return(null); } if (request.HasError()) { throw new Exception(string.Format( "failed to load image URI {0}: {1}", uri, request.error)); } Texture2D texture = DownloadHandlerTexture.GetContent(request); yield return(texture); }
/// <summary> /// Coroutine to download/read all bytes from a URI into an array. /// The URI may refer to a local file or an URL on the web. /// </summary> static public IEnumerable <byte[]> ReadAllBytesEnum( Uri uri, Action <ulong, ulong> onProgress = null) { // Handle reading data from Android content URIs. // For background, see: https://developer.android.com/reference/android/support/v4/content/FileProvider if (uri.Scheme == "content") { byte[] data = null; foreach (var result in ContentUriUtil.ReadAllBytesEnum(uri, onProgress)) { data = result; yield return(null); } yield return(data); yield break; } ulong size = 0; foreach (var result in GetSizeInBytesEnum(uri)) { size = result; yield return(null); } var request = new UnityWebRequest(uri); request.downloadHandler = new DownloadHandlerBuffer(); request.SendWebRequest(); while (!request.isDone) { onProgress?.Invoke(request.downloadedBytes, size); yield return(null); } if (request.HasError()) { throw new Exception(string.Format( "failed to read from {0}: {1}", uri, request.error)); } onProgress?.Invoke(request.downloadedBytes, size); yield return(request.downloadHandler.data); }
private IEnumerator PostReport(IObserver <string> observer, Dictionary <string, string> reportContents, IProgress <float> progress) { if (string.IsNullOrEmpty(reportUrl)) { throw new Exception("report url is empty."); } UnityWebRequest webRequest = null; switch (format) { case DataFormat.Form: webRequest = UnityWebRequest.Post(reportUrl, CreateReportFormSections(reportContents)); break; case DataFormat.Json: webRequest = UnityWebRequest.Post(reportUrl, CreateReportJson(reportContents)); break; } webRequest.timeout = 30; var operation = webRequest.SendWebRequest(); while (!operation.isDone) { if (progress != null) { progress.Report(operation.progress); } yield return(null); } var errorMessage = string.Empty; if (webRequest.HasError()) { errorMessage = string.Format("[{0}]{1}", webRequest.responseCode, webRequest.error); } observer.OnNext(errorMessage); observer.OnCompleted(); }