Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to save and load on WebGL?

Discussion in 'Web' started by SinSinx2, Jun 6, 2023.

  1. SinSinx2

    SinSinx2

    Joined:
    Mar 13, 2019
    Posts:
    12
    I am making a project on Unity for WebGL but the code that I wrote for saving and loading from a file on PC seems to not work on WebGL. Does anybody know how to save on WebGL? The project has quite a bit of variables so I would prefer to avoid using PlayerPrefs.

    This is the code that I tried to use for saving and loading

    Code (CSharp):
    1.     savePath = Application.persistentDataPath + "/Saves/";
    2.  
    3.     public void SaveGameSave(string saveFile)
    4.     {
    5.         gameSave.day = System.DateTime.Now.Day;
    6.         gameSave.month = System.DateTime.Now.Month;
    7.         gameSave.year = System.DateTime.Now.Year;
    8.         gameSave.hour = System.DateTime.Now.Hour;
    9.         gameSave.minute = System.DateTime.Now.Minute;
    10.         string json = JsonUtility.ToJson(gameSave);
    11.  
    12.         File.WriteAllText(savePath + saveFile + ".save", json);
    13.     }
    14.  
    15.     public GameSave LoadGameSave(string saveFile)
    16.     {
    17.         GameSave newGameSave = CreateGameSave();
    18.         if (File.Exists(savePath + saveFile + ".save"))
    19.         {
    20.             string json = File.ReadAllTest(savePath + saveFile + ".save");
    21.             newGameSave = JsonUtility.FromJson<GameSave>(json);
    22.         }
    23.         return newGameSave;
    24.     }
    25.  
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,508
    What does it make it "seem" like it's not working? Either it is, or it isn't.

    Typically it's either PlayerPrefs or Javascript LocalStorage, limited to 10 MB (I think that's also the limit for PlayerPrefs but that's my assumption).

    You can save the Json into PlayerPrefs as a string but I would at least format it to its minimal format, or better yet, use a binary serialization of either the json or the data in question.

    All other solutions involve either platform-specific code (save/open file dialog - windows-specific, may not work in every browser and is annoying for users and there is no guarantee user will do the right thing) or a remote server/database to set/get the data (requires at a minimum a unique user ID that is somehow generated, or better, a user account/login).
     
    GDevTeam likes this.
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,943
    PlayerPrefs is 1 MB.

    https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

    A backend service is the only real choice for WebGL. Everything else is very limited due to running in a browser.
     
    CodeSmile likes this.
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,508
    Oh, wow, just 1 MB. :eek:
     
  5. SinSinx2

    SinSinx2

    Joined:
    Mar 13, 2019
    Posts:
    12
    I can't find the file on which the save is written into and the code returns nothing when looking for the file too
     
  6. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,508
    There is no file system access in WebGL. It's all in a sandbox. You cannot read or write specific files that will show up in File Explorer. If you save in webgl you have to load in webgl and confirm that the values are identical.
     
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,943
    Like the docs says anything you try to save as a file is sent into
    idbfs/<md5 hash of data path>
    . An
    idbfs
    is just a database pretending to be a file system.

    https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html
    https://github.com/mlveis/idbfs

    The
    <md5 hash of data path>
    can be scrambled with new builds too which can lead to being unable to find the data that was associated with a previous build. The link below has a workaround for this.

    https://stackoverflow.com/questions/50040658/unity-webgl-remain-localdata-after-app-update
     
    Last edited: Jun 6, 2023
  8. SinSinx2

    SinSinx2

    Joined:
    Mar 13, 2019
    Posts:
    12
    Thank you very much for your help. After looking at multiple solutions, I have decided to pick the most simple one, which is to turn the save into a string and show it to the player so they can copy it locally. Perhaps not the best solution but my game is simple enough to allow that.