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. Dismiss Notice

Question Can a WebGL Unity game run a local process to open an archive?

Discussion in 'WebGL' started by LuizFilipeBPenha, Jul 1, 2023.

  1. LuizFilipeBPenha

    LuizFilipeBPenha

    Joined:
    May 18, 2023
    Posts:
    3
    I have to open an archive in a WebGL project, it is in my computer. Do I have to put it on the localserver? Or it is necessary to use a web socket.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    A WebGL app has no access to the file system. With exceptions: you can hack in a Win32 OpenFileDialog but it'll only work on Windows and the user has to pick the correct file.

    In all other cases you need to download external files through a server, typically a web request. While you could make your computer open to the Internet, it's a security risk, requires port forwarding, and download speed for users will be limited to your upload speed at best.
     
    LuizFilipeBPenha likes this.
  3. LuizFilipeBPenha

    LuizFilipeBPenha

    Joined:
    May 18, 2023
    Posts:
    3
    I'm using File.ReadAllText(Application.dataPath + @"/Scripts/jsonFile.json");. It is in the project files, the user doesn't have to upload the file. But it seems like when exported to WebGL the file isn't read. Do I have to open it in a different way?
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    The File API on WebGL maps to the sandbox file store of the browser app. The file simply won‘t be there in a build because dataPath is probably within that sandbox area, not the system‘s dataPath (eg AppData folder).

    Instead you need to place the file in either a Resources folder and use Resources.Load or put it in StreamingAssets so that the file is part of the build.
     
  5. dagon

    dagon

    Joined:
    Jan 4, 2013
    Posts:
    20
    1. unity webgl can run a process by using url scheme
    2. unity webgl can open a local file by using html input file