Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

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,071
    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:
    2
    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