Generate List from Binaryformatter Deserialized Unity3d

Asked

Viewed 144 times

1

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

Create the archive:

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

wanted to know how to list the information contained within 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;
}
  • Have you tried using? using System.Collections.Generic; public static List<Game> savedGames = new List<Game>(); By the way, you want to create a list with multiple birds, or you want to put the save data inside a list?

  • is a scoreboard save, I want to put the save data inside the list.

  • Are your save and load working as you expected? If yes, then now just take the values you received in the load and assign to a list. public static List<Placar> resultados and resultados.add(Placar valor)

  • yes it is working normally, I can recover only the last values I have tried, however it is not possible to convert an object of type PlayerData for a List<PlayerData> directly, I needed to list the internal save items with HasTable to then pass them to the list and from what I understood this occurs because my Save does not have an indexing formatting, it is not a list;

1 answer

1


After breaking my head, I couldn’t solve it in the form I needed, 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)); however this way 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 come 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);
    }
}  

Browser other questions tagged

You are not signed in. Login or sign up in order to post.