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.