Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by carenborn · Jul 20, 2018 at 03:37 PM · webglloading filesave file

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
         }
     }
 
 }
 








Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image carenborn · Jul 22, 2018 at 12:28 PM 0
Share

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");
     }


Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

90 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

save files on webgl?,Do unity's save files work with web_gl 0 Answers

WebGL: Load model, code from Data Base 0 Answers

Is there a way to load images into a WebGL app? 2 Answers

Save file is not detected, persistentdatapath changed? 2 Answers

How to make Advanced save and Load Files? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges