Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Need help with UnityWebRequestTexture

Discussion in 'Scripting' started by TheWebExpert, Mar 17, 2020.

  1. TheWebExpert

    TheWebExpert

    Joined:
    Apr 14, 2015
    Posts:
    198
    Code (CSharp):
    1.  
    2.  1   UnityWebRequest www = UnityWebRequestTexture.GetTexture(imgPath1);
    3.  2   yield return www.SendWebRequest();
    4.  3   if(www.isNetworkError)
    5.  4     {
    6.  5       print("Error: " + www.error);
    7.  6     }
    8.  7   else
    9.  8     {
    10.  9       Texture2D webTexture = DownloadHandlerTexture.GetContent(www);
    11. 10       Sprite webSprite = SpriteFromTexture2D(webTexture);
    12. 11       GameObject Signature = GameObject.Find("SignatureImage");
    13. 12     }
    14.  


    This code is giving me an error, "Texture has not yet finished downloading" on line 9. The image exists on the local disk; I'm just trying to read it into the sprite. Anyone have any ideas on how I can fix this??
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,368
    That seems odd since it appears you are already yielding properly. But I haven't used this API recently.

    You could try just doing
    yield return null;
    in a loop, checking to see if the
    www.isDone
    property is true, THEN move onto grabbing the content...

    Code (csharp):
    1. while( !www.isDone) {
    2.  yield return null;
    3. }
    You could also print out progress that way too...
     
  3. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,766
    The code looks correct. Can you show more of it with more context? Best, I'd like to see the whole class or at least the full methods involved.
     
  4. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,093
    It might be worth adding checks for both types of errors too, that's what the documentation recommends:

    Code (CSharp):
    1. if (www.isNetworkError || www.isHttpError)
     
    Kurt-Dekker likes this.
  5. TheWebExpert

    TheWebExpert

    Joined:
    Apr 14, 2015
    Posts:
    198
    I'm not sure which piece made it work, but it works now. Thanks!
     
    Kurt-Dekker likes this.
  6. SimonMarsh

    SimonMarsh

    Joined:
    Dec 5, 2021
    Posts:
    1
    This is the fix. Thanks
     
  7. Scottin3d

    Scottin3d

    Joined:
    Jun 29, 2016
    Posts:
    1
    All of these replies are waiting for the request to finish. You need to wait for the download to finish;

    Code (CSharp):
    1.  
    2. while (!www.downloadHandler.isDone)
    3. {
    4.     yield return null;
    5. }
    FYI for any future devs that stumble on this like myself.
     
    Last edited: May 24, 2024
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,766
    Not true. It doesn't matter which one you wait.