M
M
Mui2018-11-28 16:46:10
Game development
Mui, 2018-11-28 16:46:10

How to save data into one binary file?

There is a Cell class:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class Cell
{

  public List<int> neighbors = new List<int>();
  public int index;

}

To save data:
public void SaveData()
{
  
  Cell data = CreateDataGameObject(); // инициализирует данные ячейки

  BinaryFormatter bf = new BinaryFormatter();
  FileStream file = File.Create(Application.persistentDataPath + "/celldate.dat");
  bf.Serialize(file, data);
  file.Close();
}

And how to save List cells - in one file?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
freeExec, 2018-11-28
@Mui

Create a new class with a list of cells and serialize it. Or you can manually manage the data and send those bytes for serialization that include only one cell.
P.S. But I would recommend not to reinvent the wheel, but to use the assets built into the unit on ScriptableObject.

S
Sergey Kikhno, 2018-11-29
@AGlassOfWhiskey

As part of this task, I suggest using Newtonsoft.
You can install the package via NuGet in Vizhla.

class Program
    {
        const string pathData = @"c:\data.dat";
        static void Main(string[] args)
        {
            WriteData(GenerateData());
            DisplayListData(ReadData());
            Console.ReadLine();
        }

        static List<Cell> GenerateData()
        {
            List<Cell> dataCell = new List<Cell>();
            for (int i = 1; i <= 5; i++)
            {
                int val = i * 10;
                Cell cell = new Cell()
                {
                    neighbors = new List<int>() {val, val + 1, val + 2},
                    index = i
                };
                dataCell.Add(cell);
            }
            return dataCell;
        }

        static void WriteData(List<Cell> dataCell)
        {
            using (StreamWriter file = File.CreateText(pathData))
            {
                JsonSerializer serializer = new JsonSerializer();
                serializer.Serialize(file, dataCell);
            }
            Console.WriteLine("Save Data in PATH: " + pathData);
        }

        static List<Cell> ReadData()
        {
            using (StreamReader file = File.OpenText(pathData))
            {
                JsonSerializer serializer = new JsonSerializer();
                return (List<Cell>) serializer.Deserialize(file, typeof(List<Cell>));
            }
        }

        static void DisplayListData(List<Cell> dataList)
        {
            Console.WriteLine(new String('-', 5));
            foreach (var cell in dataList)
            {
                Console.WriteLine("Cell index: " + cell.index);
                Console.WriteLine(new String('-', 5));
                for (int i = 0; i < cell.neighbors.Count; i++)
                {
                    Console.WriteLine("Value index: " + i + " = " + cell.neighbors[i].ToString());
                }
                Console.WriteLine(new String('-', 5));
            }
        }
    }

    [Serializable]
    public class Cell
    {
        public List<int> neighbors { get; set; }
        public int index { get; set; }
    }

I think the general principle is clear.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question