- Home /
Other
Reading .json file from WebGL build
Hello everyone,
I have a problem with reading a .json file from my WebGL build. I found following solutions on the internet:
https://stackoverflow.com/questions/43693213/application-streamingassetspath-and-webgl-build
https://forum.unity.com/threads/webgl-builds-and-streamingassetspath.366346/
and I tried to implement it into my own script.
In the following code all variables are global.
I call the coroutine from the Start() method:
void Start() {
filepath = System.IO.Path.Combine(Application.streamingAssetsPath, filename);
StartCoroutine(LoadStreamingAsset());
}
This is the coroutine:
private IEnumerator LoadStreamingAsset()
{
if (filepath.Contains("://") || filepath.Contains(":///"))
{
WWW www = new WWW(filepath);
yield return www;
LoadContent(www.text);
}
else
// read nothing or log error
}
and I want to load the content into a List:
private void LoadContent(string fileContent)
{
jsonFile = JsonUtility.FromJson<JsonFile>(fileContent);
ContentToList();
StopAllCoroutines(); // don't know if this is necessary
}
I got numerous "memory access out of bounds" errors and sometimes the game would load (but without the content from the .json file). I guessed the coroutine would still be running so I used the "StopAllCoroutines()" method which solved the memory issues but the file still won't load. This is the first time I used coroutines in my game and I'm not sure if I'm doing the right thing here.
Am I correct that without the coroutine the filestream hasn't finished downloading while I try to load the content into the list? That was also a browser console error.
Thank you for your help!
Are you sure the WWW succeeded in downloading the file?
I managed to get the WWW part running and the file downloads! The code looks a little bit different but it works (thats all I'm concerned about now). I'm going to close this question. If you want any more information, let me know.
Hello, I have the same problem as you had. What did you changed to make the WWW part work?