Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Asynchronous Image Download and Sprite Creation for UWP

Discussion in 'Windows' started by andrii_unity257, Dec 22, 2023.

  1. andrii_unity257


    Oct 25, 2023
    Hello Unity Community,

    I'm currently developing a UWP project for Hololens 2 with MRTK, where I need to asynchronously download and create sprites for 5-20 PNG/JPEG images simultaneously. The challenge is that these images cannot be stored locally in the application.

    The relevant code snippet for image processing is as follows:

    Code (CSharp):
    1. async Task<Sprite> Selector(string blobSasUri)
    2. {
    3.     using var request = UnityWebRequestTexture.GetTexture(blobSasUri);
    4.     await request.SendWebRequest();
    5.     var tex = DownloadHandlerTexture.GetContent(request);
    6.     var rect = new Rect(0.0f, 0.0f, tex.width, tex.height);
    7.     var sprite = Sprite.Create(tex, rect, new Vector2(0.5f, 0.5f), 100.0f);
    9.     return sprite;
    10. }
    This process is causing resource-intensive issues on Hololens 2, even during development in the Editor on PC, leading to temporary rendering stops.

    I'm specifically looking for advice on:

    1. Effective approaches or best practices for optimizing the asynchronous download of 5-20 PNG/JPEG images and sprite creation on UWP for Hololens 2 without storing images locally.
    2. Recommendations on Unity plugins or assets tailored for enhancing performance in scenarios involving multiple image downloads on UWP.
  2. timke


    Unity Technologies

    Nov 30, 2017

    My suggestion is to use the UnityEngine.Awaitable object instead of the System.Threading.Task.
    See the documentation for details on the difference.

    If this doesn't solve the problem, then I'd recommend using the Unity Profiler to try and identify what's causing the issue on HL2 or even just in the Editor.

    Also check if the performance issue also occurs using the "old" approach with Coroutines. It's possible there's a bug in Unity's async/await support since it was only recently introduced.

    I hope this helps.