How to access folder in WebGL Build

I have a project in which I want to get some files from a folder in my assets folder. It is working perfectly fine in Unity Editor without any issues…

When building for Windows; I’ve made StreamingAssets folder in the main Assets Folder and copied myFolder in it. Then gave it’s directory to my script = “Application.streamingAssetsPath + /myFolder”. It works same flawlessly.

On WebGL, idk where is this location or how to access my folder or save it in the first place for it to be used later. I tried…

  • Making Asset Bundles
  • Streaming Assets

-----.----------.----------.----------.-----

Nothing useful I found. I don’t know much about online stuff in Untiy so please don’t be too technical on that side. I just want to save myFolder in the WebGL build and want its location so I can get the files inside it. I can NOT directly attach the files as a reference to my script; I require the location of the folder for the code to read all the files inside it.

-----.----------.----------.----------.-----

Visual Overview of Problem:

  1. The folder which I need = StreamingAssets/CT
    image

  2. The code which gives the location of this folder =

  3. What I need =
    To get this location and folder in WebGL build

as per Unity - Scripting API: Application.streamingAssetsPath (unity3d.com)

" You cannot use synchronous filesystem APIs, such as the C# System.IO.File class, to access the StreamingAssets folder on the WebGL and Android platforms. No file access is available on WebGL. Android uses a compressed .apk file. These platforms return a URL. Use the UnityWebRequest class to access the assets."

If you look at your build folder, if the subfolder structure is honoured then you would see it there, and be able to use it as per similar by coimbining the Application.streamingAssetsPath and the path/location

Also there are threads here all about it, such as WebGL builds and streamingAssetsPath - Unity Engine - Unity Discussions

1 Like

Thanks for the answer, I was searching and trying to write something that would work… but this seems too complicated. It includes making a Python Script then adding the UnityWebRequest feature and customizing all properties to our data types.

Do you know any other simple alternative or maybe this solution in a simpler way… I just need the location of the folder where the files are saved in the StreamingAssets folder. And it DOES get built with WebGL build; I can see that in the build folder. But WebGL seems to be not working with local folders, only online fetching or URL, etc.

Let’s see on this restriction with the other side.

This is the WEB where you have the cost (in time, or in money) for every megabyte.
The small build size is critical for places with bad or expensive internet.

So, files have to be downloaded as the user dives deeper into the app.

Several tips:

  • Video Playing is supported only by URL with Path to File like this video.

  • I had to use UnityWebRequest for Image (Sprite) and Text files in my Gallery Kit.

  • Moreover, for specific files like a .txt you have to customize your server settings in case if the target domain with file is other than the domain where the App is located as described here.

You are right… we need to customize the code as well as settings for specific files; do you know any documentation or guide which explains this in simple words. The Unity Web Request Doc only tells the details of the properties and variables; nothing about how to retrieve the folder which HAS been built in the WebGL Build.

to avoid this practice.

For example, I use the next method in the Gallery Kit to Download Every Image when a new Gallery (specific city) is opened:

private IEnumerator LoadSpriteByURL(
        string url, Action<Sprite> OnSuccess, Action OnFail)
{
    using UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(url);

    yield return uwr.SendWebRequest();

    if (uwr.result == UnityWebRequest.Result.Success)
    {
        Texture2D texture2D =
            DownloadHandlerTexture.GetContent(uwr);

        OnSuccess?.Invoke(Sprite.Create(
            texture2D,
            new Rect(0f, 0f, texture2D.width, texture2D.height),
            new Vector2(0f, 0f)));
    }
    else
    {
        DebugPrinter.Print(
            $"{uwr.error} for URL: {url}");

        OnFail?.Invoke();
    }
}

Such way it works and it works asynchronously using coroutines.

1 Like