Generate List from BinaryFormatter Deserialized Unity3D

1

Hello, I am creating a save system for games and would like to know how I can generate a list from a BinaryFormatter object? this file is formed from 3 information, and would like to generate a list with these values, follow the codes:

Create the file:

 public void Save(string userName, int HighScore)
{
   BinaryFormatter bf = new BinaryFormatter();
   FileStream _file = File.Open(Application.dataPath + "/Save" + _filename, FileMode.Append);
   PlayerData _data = new PlayerData();
   _data.playername = userName;
   _data.typeScore = "points";
   _data.highscore = HighScore;
   bf.Serialize(_file, _data);
   _file.Close();
   Debug.Log("Save Criado!");
}

and to load it:

 public void Load()
{
   BinaryFormatter bf = new BinaryFormatter();
   FileStream _file = File.Open(Application.dataPath + "/Save" + _filename, FileMode.Open);
   PlayerData _data = (PlayerData)bf.Deserialize(_file);
   infoData = _data;
   _file.Close();
   Debug.Log(_data);
}

I wanted to know how to gear a list with the information contained within the save.

PlayerData class:

using UnityEngine;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

[Serializable]
public class PlayerData {
  public string highscore;
  public string playername;
  public string typeScore;
}
    
asked by anonymous 01.06.2015 / 02:16

1 answer

1

After breaking my head, I could not solve it by the way I needed it, but the solution would be this:

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

[Serializable()]
public class Lizard
{
public string Type { get; set; }
public int Number { get; set; }
public bool Healthy { get; set; }

    public Lizard(string t, int n, bool h)
    {
        Type =    t;
        Number =  n;
        Healthy = h;
    }
}

to serialize:

 var lizards1 = new List<Lizard>();
        lizards1.Add(new Lizard("Thorny devil",                1, true));
        lizards1.Add(new Lizard("Casquehead lizard",           0, false));
        lizards1.Add(new Lizard("Green iguana",                4, true));
        lizards1.Add(new Lizard("Blotched blue-tongue lizard", 0, false));
        lizards1.Add(new Lizard("Gila monster",                1, false));

        try
        {
        using (Stream stream = File.Open("data.bin", FileMode.Create))
        {
            BinaryFormatter bin = new BinaryFormatter();
            bin.Serialize(stream, lizards1);
        }
        }
        catch (IOException)
        {

        }

to read:

using (Stream stream = File.Open("data.bin", FileMode.Open))
        {
            BinaryFormatter bin = new BinaryFormatter();

            var lizards2 = (List<Lizard>)bin.Deserialize(stream);
            foreach (Lizard lizard in lizards2)
            {
            Console.WriteLine("{0}, {1}, {2}",
                lizard.Type,
                lizard.Number,
                lizard.Healthy);
            }
        }
        }
        catch (IOException)
        {
        }

In other words I would need to create a list of the objects that would be saved: var lizards1 = new List<Lizard>(); and add lizards1.Add(new Lizard("Thorny devil",1, true)); but that way it would not be interesting for me, my solution was to get an online host, create a database create 2 files in php for sending and reading data, and send them to the server, access these files through the game and create the list. here are the codes:

string _host = "http://ramses.freewha.com/";
string phpScore = "score.php";

 public void PegarScore()
{
    WWW scoreSite = new WWW(_host + phpScore);
    StartCoroutine(BuscarScore(scoreSite));
}

IEnumerator BuscarScore(WWW w)
{
    loading.color = new Color32(255, 255, 255, 255);
    yield return w;
    loading.color = new Color32(255, 255, 255, 0);
    if (w.error == null)
    {
        resultadoRank = w.text.Split(","[0]);
        foreach (string nome in resultadoRank)
        {
            GameObject go = (GameObject)Instantiate(playerScoreEntryPrefab);
            go.transform.SetParent(this.transform, false);

            var outputName = Regex.Replace(nome, @"[0-9\-]", string.Empty);
            go.transform.Find("UserName").GetComponent<Text>().text = outputName;

            var outputPontos = Regex.Replace(nome, @"[a-z\-]", string.Empty);
            go.transform.Find("Scoore").GetComponent<Text>().text = outputPontos;
        }
    }
    else
    {
        print("Error ao Enviar: " + w.error);
    }
}  
    
20.06.2015 / 04:02