Search Unity

How to change local SaveLoad to www?

Discussion in 'Scripting' started by Slyrfecso1, Sep 3, 2015.

  1. Slyrfecso1


    Jul 16, 2012

    I have a working SaveLoad script, but I'd like to save the to www.
    Any idea will be helpful.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Runtime.Serialization.Formatters.Binary;
    5. using System.IO;
    7. public static class SaveLoad {
    9.     public static List<Game> savedGames = new List<Game>();
    11.      public static void Save() {
    12.         SaveLoad.savedGames.Add(Game.current);
    13.         BinaryFormatter bf = new BinaryFormatter();
    14.         FileStream file = File.Create (Application.persistentDataPath + "/"); //you can call it anything you want
    15.          bf.Serialize(file, SaveLoad.savedGames);
    16.          Debug.Log (Application.persistentDataPath);
    17.         file.Close();
    18.     }  
    20.     public static void Load() {
    21.         if(File.Exists(Application.persistentDataPath + "/")) {
    22.             BinaryFormatter bf = new BinaryFormatter();
    23.             FileStream file = File.Open(Application.persistentDataPath + "/", FileMode.Open);
    24.             SaveLoad.savedGames = (List<Game>)bf.Deserialize(file);
    25.             file.Close();
    26.         }
    27.     }
    28. }
  2. lordofduct


    Oct 3, 2011
    The couple lines used to create a file... namely lines 13 thru 17, should instead save the file to the web.

    Really all those lines of code do is:

    create a BinaryFormatter
    Create a FileStream (this is the critical part, a FILE stream)
    pass the FileStream and SavedGame obejcts to the SerializeMethod of BinaryFormatter, which turns the SavedGame object into a bunch of 1's and 0's which are written to the FileStream
    Close the stream when complete

    Well... if you replace 'FileStream' with any other stream... like MemoryStream, or the sort, you'll save the game to that OTHER stream instead.

    What you need is a way to write it to a stream that is then sent up to the internet.

    How this is usually done is you write your data to a memory stream (create the representation of the data). And then open up some connection to the internet that accepts data to be sent to it. Then send that data from the memory stream to the remote connection.

    The SAVING part is easy... you already have that. Really you could use the FileStream still, and use that as the data sent across the web.

    So really... your question isn't about how to save your game.

    It's about how to save stuff on the internet.

    Which there's TONS of tutorials and help about on the internet. This isn't even a 'Unity' question, so you don't even have to search for unity related answers.

    Thing is... it's also kind of complicated. As you'll need somewhere on the internet to save to. Those aren't just free and laying around.


    where on the web do you want to save this thing?

    Do you have a server somewhere?

    Do you expect to use some existing web server managed by someone else?
  3. Slyrfecso1


    Jul 16, 2012
    Thank you for your superabundant answer.
    I try to give more detail, what I need.
    I'm making a web player application, if you have time please try it out:
    user: a
    pass: a

    My idea was, if I save things locally and when it will work, then I will change the code.
    Yes I have a domain and I'm the administrator of the server.

    In my project I have made screenshot function whit php upload and whit automatic image load gallery.
    (7th icon)

    You have right, I can find a lot of tutorial on the internet, but they aren't good for me, because in web player I don't have permission to saving locally. (1MB)

    I'm working on SaveLoad function, unfortunately I can't Load back strings now.
    (6th icon, First line: empty room, Second SaveState, Third Load.)
    (I'm saving, names of object, Vector3, materials, textures)
    This is my other post:

    I have made a MySQL database, could you help me please, how can I save Game.current to this database?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Runtime.Serialization.Formatters.Binary;
    5. using System.IO;
    7. public class SaveLoad {
    10.     private string secretKey = "mySecretKey"; // I have changed:)
    11.     public static List<Game> savedGames = new List<Game>();
    14.     public static void Save() {
    15.         SaveLoad.savedGames.Add(Game.current);
    16.         BinaryFormatter bf = new BinaryFormatter();
    17.         string serializedData = Serialize(data);
    18.         string hash = MD5Test.Md5Sum(name + score + secretKey);
    19.         WWW request = new WWW(""+id+"&data=" + WWW.EscapeURL(serializedData));
    20.         yield request;
    21.         Debug.Log("Data saved.");
    24. //        FileStream file = File.Create (Application.persistentDataPath + "/");
    25. //        bf.Serialize(file, SaveLoad.savedGames);
    26. //        Debug.Log (Application.persistentDataPath);
    27. //        file.Close();
    30.   }
    31. }

    Added to SQL:

    This is my saveADD.php
    (At the first, I would like to try with 2 strings, vezetek and osszeallitas)

    Code (CSharp):
    1. <?php
    2.         $db = mysql_connect('somethingSERVER', 'somethingUSER', 'somethingPASS') or die('Could not connect: ' . mysql_error());
    3.         mysql_select_db('somethingDATABASE') or die('Could not select database');
    4.         // Strings must be escaped to prevent SQL injection attack.
    5.         $vezetek = mysql_real_escape_string($_GET['vezetek'], $db);
    6.         $osszeallitas = mysql_real_escape_string($_GET['osszeallitas'], $db);
    7.         $hash = $_GET['hash'];
    8.         $secretKey="somethingKEY"; # Change this value to match the value stored in the client javascript below
    10.         $real_hash = md5($vezetek . $osszeallitas . $secretKey);
    11.         if($real_hash == $hash) {
    12.             // Send variables for the MySQL database class.
    13.             $query = "insert into scores values (NULL, '$vezetek', '$osszeallitas');";
    14.             $result = mysql_query($query) or die('Query failed: ' . mysql_error());
    15.         }
    16. ?>
    Last edited: Sep 4, 2015