Search Unity

How can i load local image asynchronously? "sprite.create" spend much time

Discussion in 'Getting Started' started by dvlee, Aug 24, 2016.

  1. dvlee

    dvlee

    Joined:
    Jun 24, 2016
    Posts:
    6
    I want to load local Image asynchronously, but "sprite.create" spend much time which make my UI stop. Somebody can help?

    Code (CSharp):
    1. WWW www = new WWW (filePath);
    2.         yield return www;
    3.  
    4.         Texture2D texture = new Texture2D(4, 4, TextureFormat.ARGB32, true);
    5.         www.LoadImageIntoTexture(texture);
    6.         www.Dispose ();
    7.         www = null;
    8.  
    9.         curTexture = texture;
    10.         img.sprite = Sprite.Create (curTexture, new Rect (0, 0, curTexture.width, curTexture.height), new Vector2 (0.5f, 0.5f));
     
    tuanxle likes this.
  2. LFCB

    LFCB

    Joined:
    Aug 21, 2015
    Posts:
    4
    Hi @dvlee did you found any solution for this issue?

    I got exactly the same problem and I just can´t figure how to solve it :(
     
    tuanxle likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Can you run this through the profiler and give us a screenshot of the appropriate frame?

    I've personally never had an issue with Sprite.Create.
     
  4. oANDRYo

    oANDRYo

    Joined:
    Nov 27, 2017
    Posts:
    6
    If you interested that's my profiler screen. I load texture with webRequest and it works fine, but when I use Sprite.Create I have such spikes in profiler.


    Code (CSharp):
    1. UnityWebRequest webRequest = UnityWebRequestTexture.GetTexture(DataFileUtility.GetFullPath(path));
    2.        
    3.         yield return webRequest.SendWebRequest();
    4.  
    5.         var texture = DownloadHandlerTexture.GetContent(webRequest);
    6.  
    7.         _sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)/*, 100.0f*/);
    upload_2019-1-10_10-27-49.png
     
  5. beatdesign

    beatdesign

    Joined:
    Apr 3, 2015
    Posts:
    137
    Exact same code, exact same problem.
    Shouldn't be an ASYNCH call? WHY IT STOPS the UI system?! Sprite.Create, even if is into a coroutine, stops the UI system!
    Anyone?
     
    AminAghajoon likes this.
  6. AminAghajoon

    AminAghajoon

    Joined:
    May 17, 2013
    Posts:
    20
    Nikitty likes this.