- Home /
How to load and save data within webGL build?,Persistent load save data on webgl
Hi all,
Im really stuck at this specific problem where I have a game where you load and save data. When I build it as a WebGL and upload all files to a webserver my problem appear. I want the data to be persistent even if I run the game with different webbrowsers or a different PC. I cant find a method to perserv the data this way. If I save som data on my PC, I cant see it on the next PC. The game works exactly how I want it if I build as "PC, Mac & Linux Standalone". Altough if I move the game to another PC (and not along the profiles "appdata") I recreate the problem.
PlayerPrefs I learned this coding from here: https://docs.unity3d.com/2017.4/Documentation/ScriptReference/PlayerPrefs.html This is the best feature and works perfect for my game, if I just play it on the same PC with the one webbrowser. Loads and Saves data from/in IndexedDB "/idbfs/xxxxxxxxxxxxxxxxxxxx/PlayerPrefs" in the players webbrowser.
Application.persistentDataPath - with BinaryFormatter, Serialize and Deserialize I learned this coding from here: https://unity3d.com/learn/tutorials/topics/scripting/persistence-saving-and-loading-data Ok method but unfortunately it works similar to PlayerPrefs. Ofcourse works on Editor/PC.
StreamReader I learned this coding from here: https://support.unity3d.com/hc/en-us/articles/115000341143-How-do-I-read-and-write-data-from-a-text-file- This didn't work for me, webbrowser or webserver could'nt find path. The file exist on path. Ofcourse works on Editor/PC.
UnityWebRequest I learned this coding from here: https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.html This didn't work for me, webbrowser or webserver could'nt find path. The file exist on path.
This is my "test game" for making this work. A inputField to collect data, a button for save and a text thing to display loaded data.
using UnityEngine.UI;
using UnityEngine;
public class Action : MonoBehaviour {
public Text theList;
public Button AddButton;
public InputField inputText;
// Use this for initialization
void Start ()
{
Button rbutton = AddButton.GetComponent<Button>();
rbutton.onClick.AddListener(TaskOnClickAdd);
Load();
}
void TaskOnClickAdd()
{
Save();
Load();
}
public void Save()
{
// Some cool scripting for saving data for global use in WebGL
}
public void Load()
{
// Some cool scripting for loading data for global use in WebGL
}
}
Please, Can anyone point me to a valid method to use for load/save data on a WebGL build?
Thanks
Answer by carenborn · Jul 21, 2018 at 11:21 PM
I have tried a fifth method.
I learned it from here: https://docs.unity3d.com/ScriptReference/WWW.html.
I have managed to load my data from a url-textfile. But I can't figure out how to save data.
I get this error message in debug:
Generic/unknown HTTP error
UnityEngine.Debug:Log(Object)
c__Iterator0:MoveNext() (at Assets/_Scripts/Action.cs:37)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
Here is my code:
using UnityEngine.UI;
using UnityEngine;
using System;
using System.Collections;
using UnityEngine.Networking;
public class Action : MonoBehaviour {
public Text theList;
public Button AddButton;
public InputField inputText;
// Use this for initialization
void Start ()
{
StartCoroutine(Load());
StartCoroutine(Save());
Button rbutton = AddButton.GetComponent<Button>();
rbutton.onClick.AddListener(TaskOnClickAdd);
Load();
}
void TaskOnClickAdd()
{
Save();
Load();
}
IEnumerator Save()
{
byte[] myData = System.Text.Encoding.UTF8.GetBytes("This is some test data");
UnityWebRequest www = UnityWebRequest.Put("https://domain.se/filer/test2/test.txt", myData);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("Upload complete!");
}
}
IEnumerator Load()
{
UnityWebRequest www = UnityWebRequest.Get("https://domain.se/filer/test2/test.txt");
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Debug.Log(www.downloadHandler.text);
theList.text = www.downloadHandler.text;
}
}
}
6th method.. ftp. Not working..
public void Save()
{
FilePath = Application.dataPath + "/test2.txt";
Debug.Log("Path: " + FilePath);
WebClient client = new System.Net.WebClient();
Uri uri = new Uri(FTPHost + new FileInfo(FilePath).Name);
client.UploadProgressChanged += new UploadProgressChangedEventHandler(OnFileUploadProgressChanged);
client.UploadFileCompleted += new UploadFileCompletedEventHandler(OnFileUploadCompleted);
client.Credentials = new System.Net.NetworkCredential(FTPUserName, FTPPassword);
client.UploadFileAsync(uri, "STOR", FilePath);
}
void OnFileUploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
Debug.Log("Uploading Progreess: " + e.ProgressPercentage);
}
void OnFileUploadCompleted(object sender, UploadFileCompletedEventArgs e)
{
Debug.Log("File Uploaded");
}