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.
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.
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.
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.
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.
if (Application.platform == RuntimePlatform.WindowsPlayer)
{
LoadSaveGame.SaveFadeData(data);
}
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
LoadSaveGame.SavePlayerPrefData(data);
}
/// <summary>
/// SavePlayerPrefs - Saves the players progress and game status in player prefs
/// in case this is running on WebGL platform
/// </summary>
/// <param name="data"></param>
public static void SavePlayerPrefData(GamePlayerData playerData)
{
XmlSerializer serializer = new XmlSerializer(typeof(GamePlayerData));
using (StringWriter sw = new StringWriter())
{
serializer.Serialize(sw, playerData);
Debug.Log(sw.ToString());
PlayerPrefs.SetString("PlayerData", sw.ToString());
}
}
/// <summary>
/// LoadPlayerPrefs - Loads the players progress and game status in player prefs
/// in case this is running on WebGL platform
/// </summary>
/// <returns></returns>
public static GamePlayerData LoadPlayerPrefData()
{
XmlSerializer serializer = new XmlSerializer(typeof(GamePlayerData));
string textData = PlayerPrefs.GetString("PlayerData");
if (textData.Length == 0)
{
GamePlayerData playerData = new GamePlayerData();
return playerData;
}
else
{
using (var reader = new System.IO.StringReader(textData))
{
GamePlayerData playerData = serializer.Deserialize(reader) as GamePlayerData;
return playerData;
}
}
}
I think that this will be a good solution and the files that get saved in windows platform are on 1k to 2k.
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.