Skip to content

Commit

Permalink
com.utilities.rest 2.2.2 (#47)
Browse files Browse the repository at this point in the history
- Improved handling of multimedia import problems
  • Loading branch information
StephenHodgson authored Oct 28, 2023
1 parent d73b82f commit 0c0a7ac
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
48 changes: 35 additions & 13 deletions Runtime/Rest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -559,20 +559,27 @@ public static async Task<Texture2D> DownloadTextureAsync(
throw new RestException(response, $"Failed to download texture from \"{url}\"!");
}

Texture2D texture;
var downloadHandler = (DownloadHandlerTexture)webRequest.downloadHandler;

if (!isCached)
try
{
await Cache.WriteCacheItemAsync(downloadHandler.data, cachePath, cancellationToken);
}
if (!isCached)
{
await Cache.WriteCacheItemAsync(downloadHandler.data, cachePath, cancellationToken);
}

await Awaiters.UnityMainThread;
var texture = downloadHandler.texture;
downloadHandler.Dispose();
await Awaiters.UnityMainThread;
texture = downloadHandler.texture;

if (texture == null)
if (texture == null)
{
throw new RestException(response, $"Failed to load texture from \"{url}\"!");
}
}
finally
{
throw new RestException(response, $"Failed to download texture from \"{url}\"!");
downloadHandler.Dispose();
}

texture.name = Path.GetFileNameWithoutExtension(cachePath);
Expand Down Expand Up @@ -630,16 +637,31 @@ public static async Task<AudioClip> DownloadAudioClipAsync(
throw new RestException(response, $"Failed to download audio clip from \"{url}\"!");
}

AudioClip clip;
var downloadHandler = (DownloadHandlerAudioClip)webRequest.downloadHandler;

if (!isCached)
try
{
await Cache.WriteCacheItemAsync(downloadHandler.data, cachePath, cancellationToken);
if (!isCached)
{
await Cache.WriteCacheItemAsync(downloadHandler.data, cachePath, cancellationToken);
}

await Awaiters.UnityMainThread;
clip = downloadHandler.audioClip;

if (clip == null ||
clip.loadState == AudioDataLoadState.Failed ||
clip.loadState == AudioDataLoadState.Unloaded)
{
throw new RestException(response, $"Failed to load audio clip from \"{url}\"!");
}
}
finally
{
downloadHandler.Dispose();
}

await Awaiters.UnityMainThread;
var clip = downloadHandler.audioClip;
downloadHandler.Dispose();
clip.name = Path.GetFileNameWithoutExtension(cachePath);
return clip;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "Utilities.Rest",
"description": "This package contains useful RESTful utilities for the Unity Game Engine.",
"keywords": [],
"version": "2.2.1",
"version": "2.2.2",
"unity": "2021.3",
"documentationUrl": "https://github.com/RageAgainstThePixel/com.utilities.rest#documentation",
"changelogUrl": "https://github.com/RageAgainstThePixel/com.utilities.rest/releases",
Expand Down

0 comments on commit 0c0a7ac

Please sign in to comment.