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 WebGL with user data files

Discussion in 'General Discussion' started by mcanty55, Dec 20, 2022.

  1. mcanty55

    mcanty55

    Joined:
    Apr 17, 2014
    Posts:
    15
    I've just completed a Windows standalone game and it works perfectly. I now would like to publish a webGL version to showcase on Unity Play. The problem is. I have four files that need to be created and used for the game. Is there a way to have webGL use these files?

    One file is for the games level data and gets copied to a persistent folder on installation.
    the other three files are created the first time the game is run.

    If anyone would like to see the game, I can share a link to my google drive download.
     
    Last edited: Dec 20, 2022
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,355
    With WebGL, I'm pretty sure you have zero access to the end-user's file system, except through PlayerPrefs which creates a browser cookie. You'll probably need to redesign your game to use some sort of server-hosted system.
     
    Ryiah and CodeSmile like this.
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,028
    Yep. Though I'm not certain how StreamingAssets folder behaves in WebGL. I would at least research if it's possible to read/write to/from the StreamingAssets folder in a WebGL build. But wouldn't be surprised if that isn't allowed either due to the browser sandbox.
     
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,124
    You can use UnityWebRequest to load data from files stored in the StreamingAssets folder. What you won't be able to do is write to them or any other file on the user's computer due to browser security restrictions.
     
  5. mcanty55

    mcanty55

    Joined:
    Apr 17, 2014
    Posts:
    15
    Understood.
     
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,124
    Last edited: Dec 21, 2022
  7. mcanty55

    mcanty55

    Joined:
    Apr 17, 2014
    Posts:
    15
    Thank you all. I've coded a solution that I am testing this morning.

    Checking to see if this application platform is either windows or WebGL.

    Code (CSharp):
    1. if (Application.platform == RuntimePlatform.WindowsPlayer)
    2. {
    3.     LoadSaveGame.SaveFadeData(data);
    4. }
    5. if (Application.platform == RuntimePlatform.WebGLPlayer)
    6. {
    7.     LoadSaveGame.SavePlayerPrefData(data);
    8. }
    9.     /// <summary>
    10.     /// SavePlayerPrefs - Saves the players progress and game status in player prefs
    11.     /// in case this is running on WebGL platform
    12.     /// </summary>
    13.     /// <param name="data"></param>
    14.     public static void SavePlayerPrefData(GamePlayerData playerData)
    15.     {
    16.         XmlSerializer serializer = new XmlSerializer(typeof(GamePlayerData));
    17.         using (StringWriter sw = new StringWriter())
    18.         {
    19.             serializer.Serialize(sw, playerData);
    20.             Debug.Log(sw.ToString());
    21.             PlayerPrefs.SetString("PlayerData", sw.ToString());
    22.         }
    23.     }
    24.  
    25.     /// <summary>
    26.     /// LoadPlayerPrefs - Loads the players progress and game status in player prefs
    27.     /// in case this is running on WebGL platform
    28.     /// </summary>
    29.     /// <returns></returns>
    30.     public static GamePlayerData LoadPlayerPrefData()
    31.     {
    32.  
    33.         XmlSerializer serializer = new XmlSerializer(typeof(GamePlayerData));
    34.         string textData = PlayerPrefs.GetString("PlayerData");
    35.         if (textData.Length == 0)
    36.         {
    37.             GamePlayerData playerData = new GamePlayerData();
    38.             return playerData;
    39.         }
    40.         else
    41.         {
    42.             using (var reader = new System.IO.StringReader(textData))
    43.             {
    44.                 GamePlayerData playerData = serializer.Deserialize(reader) as GamePlayerData;
    45.                 return playerData;
    46.             }
    47.         }
    48.  
    49.     }
    I think that this will be a good solution and the files that get saved in windows platform are on 1k to 2k.
     
  8. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,355
    This makes sense. Browser cookies are only meant to store very small amounts of data. Most websites store all of the user's data on a server and only ever use cookies to store a token to identify the user's login session.
     
  9. Romaleks360

    Romaleks360

    Joined:
    Sep 9, 2017
    Posts:
    69
    This is wrong. The docs cay that PlayerPrefs in WebgGL are stored in IndexedDB, not in the cookies:

    https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
    1mb is a weird limitation considering that IndexedDB has no strict size limits.