Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

UnityWebRequestTexture.GetTexture requires hardcoded string?

Discussion in 'Scripting' started by Roesh, Aug 25, 2018.

  1. Roesh

    Roesh

    Joined:
    Mar 10, 2018
    Posts:
    2
    Hello,

    I've run into an issue using UnityWebRequestTexture.GetTexture to fetch textures from the web. I call nine Coroutines to download textures from URLs created by concatenation:

    Code (CSharp):
    1. string baseUrl = "http://roesh.000webhostapp.com/";
    2.             string url = baseUrl + "Animals" +"/" + Chameleon + ".jpg";
    3.             StartCoroutine(GetTexture(img, index, url));        
    However this causes a "Generic/unknown HTTP error" in the editor from the coroutine below:
    Code (CSharp):
    1. IEnumerator GetTexture(GameObject img, int index, string uri)
    2.     {
    3.         using (UnityWebRequest www = UnityWebRequestTexture.GetTexture(uri))
    4.         {
    5.             Debug.Log("Accessing: " + uri);
    6.             yield return www.SendWebRequest();
    7.  
    8.             Texture2D tex = null;
    9.             if (www.error != null)
    10.             {
    11.                 debugTextOnline.SetActive(true);
    12.                 Debug.Log(www.error);
    13.             }
    14.             else
    15.             {
    16.                 tex = DownloadHandlerTexture.GetContent(www);
    17.                 if (tex != null)
    18.                 {
    19.                     assignTex(img, index, tex);
    20.                 }
    21.             }          
    22.         }
    23.     }
    This does not occur if I use a hard coded string as an argument. All nine textures in the scene are converted to the Chameleon texture (or other texture specified by a hardcoded url)
    Code (CSharp):
    1. string baseUrl = "http://roesh.000webhostapp.com/";
    2.             string url = baseUrl + userManager.name +"/" + fileName + ".jpg";
    3.             StartCoroutine(GetTexture(img, index, "http://roesh.000webhostapp.com/Animals/Chameleon.jpg"));    
    4.  
    Is this an issue with the UnityWebRequest Object itself? Is is possible that other parts of my code are causing an issue?
    I confirmed that the concatenation process works by using string.compare() between a hardcoded url and concatenated url to ensure I was passing the exact same arguments as well. I also built a .exe which has the same issue as well.
     
  2. Roesh

    Roesh

    Joined:
    Mar 10, 2018
    Posts:
    2
    I solved this: Turns out one of the concatenated strings was not trimmed and I was using string.compare wrong. Sorry about that.

    If this is not public yet, please delete it, or if you think it may help someone, then that's OK