Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

WebrequestTexture from StreamingAssets with WebGL

Discussion in 'WebGL' started by spelafort, Jul 26, 2020.

  1. spelafort

    spelafort

    Joined:
    May 8, 2017
    Posts:
    37
    hi all,

    I have a project where I'm trying to call a numbered png texture from StreamingAssets and apply it as the albedo of a URP material. Works fine for standalone but for WebGL I can't get it working.

    I know that the System.IO namespace doesn't work with WebGL, so I'm using UnityWebRequest. Here's the code in question:

    Code (CSharp):
    1. IEnumerator loadStreamingAsset(string fileName)
    2.     {
    3.         string url;
    4.         url =Application.dataPath + "/StreamingAssets/"+fileName;
    5.  
    6.         byte[] imgData;
    7.  
    8.         if (url.Contains("://") || url.Contains(":///"))
    9.         {
    10.             //This means we're in webGL
    11.             UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);
    12.             yield return www.SendWebRequest();
    13.             imgData = www.downloadHandler.data;
    14.  
    15.             if (www.isNetworkError || www.isHttpError)
    16.             {
    17.                 Debug.Log(www.error);
    18.             }
    19.             else
    20.             {
    21.                  myTex2D = DownloadHandlerTexture.GetContent(www);
    22.                 Debug.Log("WEB tried to load thing at " + url);
    23.             }
    24.         }
    25.         else
    26.         {
    27.              //we're in standalone, this part works
    28.             imgData = File.ReadAllBytes(url);
    29.             myTex2D.LoadImage(imgData);
    30.  
    31.             Debug.Log("FILE tried to load thing at " + url);
    32.         }
    33.  
    34.         myTex2D.Apply();
    35.                        
    36.     }
    37.  
    38. StartCoroutine(loadStreamingAsset(rndStr+".png"));
    Not throwing any errors, just getting this in the console with a blank material:

    WEB tried to load thing at http://localhost:51327/StreamingAssets/96.png
    (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)

    Tried:
    -switching to the deprecated WWW class
    -adding a texture to the material manually to make sure it's not a shader or WebGL graphics issue (manually added texture shows up fine)
    -prepending with "file://" and "file:///", which apparently one needed to do in previous versions

    I do assume this is something to do with the path, but I've read a bunch of threads and docs about this and I just don't see what I'm missing. Can anyone spot the problem?

    EDIT- this is both with 'build and run' as well as uploading it to itch, but in the itch I get this:

    [CachedXMLHttpRequest] 'https://v6p9d9t4.ssl.hwcdn.net/html/2528295/StreamingAssets/299.png' downloaded successfully (1612021 bytes) and stored in indexedDB cache.
    so perhaps I'm simply not applying the texture successfully.
     
    Last edited: Jul 27, 2020
    TimothyThomasson likes this.
  2. spelafort

    spelafort

    Joined:
    May 8, 2017
    Posts:
    37
    I'm getting this as well. My images are definitely png (not just the extension), and I'm not sure if the odd space behind the two HTTPs means that it's somehow getting the path wrong? Any help greatly appreciated!
     

    Attached Files:

  3. spelafort

    spelafort

    Joined:
    May 8, 2017
    Posts:
    37
    Tried to simplify. The first snippet works in the editor (again) but not in the WebGL build:
    Code (CSharp):
    1. UnityWebRequest www = UnityWebRequest.Get(filePathFromStreamingAssets);
    2.  
    3.          DownloadHandler handle = www.downloadHandler;
    4.  
    5.          yield return www.SendWebRequest();
    6.  
    7.          if (www.isNetworkError)
    8.          {
    9.  
    10.              UnityEngine.Debug.Log(www.error);
    11.          }
    12.          else
    13.          {
    14.              UnityEngine.Debug.Log("Success");
    15.  
    16.              if (myTex2D.LoadImage(handle.data))
    17.              {
    18.                  myTex2D.LoadImage(handle.data);
    19.                  myTex2D.Apply();
    20.              }
    21.          }
    the second snippet does't seem to work at all, but it's almost copied verbatim from the docs:
    Code (CSharp):
    1. UnityWebRequest www = UnityWebRequestTexture.GetTexture(filePathFromStreamingAssets);
    2.         yield return www.SendWebRequest();
    3.  
    4.         if (www.isNetworkError || www.isHttpError)
    5.         {
    6.             Debug.Log(www.error);
    7.         }
    8.         else
    9.         {
    10.             myTex2D = ((DownloadHandlerTexture)www.downloadHandler).texture;
    11.             Debug.Log("SUCCESS? " + filePathFromStreamingAssets);
    12.             myTex2D.Apply();
    13.         }
     
  4. digitalrowing

    digitalrowing

    Joined:
    Feb 25, 2022
    Posts:
    2
    I am getting the same issue. It does appear to be a problem in WebGL and Unity, is there any Unity peple that can assist please?