WebrequestTexture from StreamingAssets with WebGL

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:

IEnumerator loadStreamingAsset(string fileName)
    {
        string url;
        url =Application.dataPath + "/StreamingAssets/"+fileName;

        byte[] imgData;

        if (url.Contains("://") || url.Contains(":///"))
        {
            //This means we're in webGL
            UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);
            yield return www.SendWebRequest();
            imgData = www.downloadHandler.data;

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                 myTex2D = DownloadHandlerTexture.GetContent(www);
                Debug.Log("WEB tried to load thing at " + url);
            }
        }
        else
        {
             //we're in standalone, this part works
            imgData = File.ReadAllBytes(url);
            myTex2D.LoadImage(imgData);

            Debug.Log("FILE tried to load thing at " + url);
        }

        myTex2D.Apply();
                       
    }

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.

1 Like

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!

6135251--669188--Capture.PNG

Tried to simplify. The first snippet works in the editor (again) but not in the WebGL build:

UnityWebRequest www = UnityWebRequest.Get(filePathFromStreamingAssets);

         DownloadHandler handle = www.downloadHandler;

         yield return www.SendWebRequest();

         if (www.isNetworkError)
         {

             UnityEngine.Debug.Log(www.error);
         }
         else
         {
             UnityEngine.Debug.Log("Success");

             if (myTex2D.LoadImage(handle.data))
             {
                 myTex2D.LoadImage(handle.data);
                 myTex2D.Apply();
             }
         }

the second snippet does’t seem to work at all, but it’s almost copied verbatim from the docs:

UnityWebRequest www = UnityWebRequestTexture.GetTexture(filePathFromStreamingAssets);
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            myTex2D = ((DownloadHandlerTexture)www.downloadHandler).texture;
            Debug.Log("SUCCESS? " + filePathFromStreamingAssets);
            myTex2D.Apply();
        }

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?

1 Like