Skip to content

Commit

Permalink
Merge pull request #30 from rwth-acis/maintenance/async-fix
Browse files Browse the repository at this point in the history
Fixed async/await errors on Unity 6
  • Loading branch information
BenediktHensen authored Oct 10, 2024
2 parents e6c8da0 + 82126af commit 61c3304
Show file tree
Hide file tree
Showing 5 changed files with 633 additions and 584 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@
using i5.Toolkit.Core.Utilities.Async;

namespace i5.Toolkit.Core.Utilities.ContentLoaders
{
/// <summary>
/// Adapter class which loads textures using Unity's WebRequestsTexture
/// </summary>
public class UnityTextureLoader : IContentLoader<Texture2D>
{
/// <summary>
/// Loads the texture at the given URI using Unity's built-in methods
/// </summary>
/// <param name="uri">The uri where the texture is stored</param>
/// <returns>Returns a WebResponse with the results of the web request</returns>
public async Task<WebResponse<Texture2D>> LoadAsync(string uri)
{
using (UnityWebRequest req = UnityWebRequestTexture.GetTexture(uri))
{
await req.SendWebRequest();

if (req.isNetworkError || req.isHttpError)
{
i5Debug.LogError("Error fetching texture: " + req.error, this);
return new WebResponse<Texture2D>(req.error, req.responseCode);
}
else
{
Texture2D texture = DownloadHandlerTexture.GetContent(req);
return new WebResponse<Texture2D>(texture, req.downloadHandler.data, req.responseCode);
}
}
}
}
{
/// <summary>
/// Adapter class which loads textures using Unity's WebRequestsTexture
/// </summary>
public class UnityTextureLoader : IContentLoader<Texture2D>
{
/// <summary>
/// Loads the texture at the given URI using Unity's built-in methods
/// </summary>
/// <param name="uri">The uri where the texture is stored</param>
/// <returns>Returns a WebResponse with the results of the web request</returns>
public async Task<WebResponse<Texture2D>> LoadAsync(string uri)
{
using (UnityWebRequest req = UnityWebRequestTexture.GetTexture(uri))
{
await req.SendWebRequest();

if (req.result == UnityWebRequest.Result.Success)
{
Texture2D texture = DownloadHandlerTexture.GetContent(req);
return new WebResponse<Texture2D>(texture, req.downloadHandler.data, req.responseCode);
}
else
{
i5Debug.LogError("Error fetching texture: " + req.error, this);
return new WebResponse<Texture2D>(req.error, req.responseCode);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ public async Task<WebResponse<string>> LoadAsync(string uri)
{
await req.SendWebRequest();

if (req.result == UnityWebRequest.Result.ConnectionError ||
req.result == UnityWebRequest.Result.ProtocolError)
if (req.result == UnityWebRequest.Result.Success)
{
i5Debug.LogError("Get request to: " + uri + " returned with error " + req.error, this);
return new WebResponse<string>(false, req.downloadHandler.text, req.downloadHandler.data, req.responseCode, req.error);
return new WebResponse<string>(req.downloadHandler.text, req.downloadHandler.data, req.responseCode);
}
else
{
return new WebResponse<string>(req.downloadHandler.text, req.downloadHandler.data, req.responseCode);
}
i5Debug.LogError("Get request to: " + uri + " returned with error " + req.error, this);
return new WebResponse<string>(false, req.downloadHandler.text, req.downloadHandler.data, req.responseCode, req.error);
}
}
}
}
Expand Down
Loading

0 comments on commit 61c3304

Please sign in to comment.