Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

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

Discussion in 'Web' started by luizfbp, Jul 1, 2023.

  1. luizfbp

    luizfbp

    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:
    6,641
    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.
     
    luizfbp likes this.
  3. luizfbp

    luizfbp

    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:
    6,641
    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
     
    Harpaceas likes this.