Unity data persistence-serialization and deserialization

Serialization: The process of converting an object into a transmittable byte sequence is called serialization. In layman's terms, it is to save the state (each attribute) of an object, and then obtain it at an appropriate time.
Insert picture description here

Deserialization: The process of restoring a byte sequence to an object is called deserialization.

Serialization purpose: to persist custom objects in a certain form of storage, to transfer objects from one place to another, so that the objects can be stored across platforms, and for network transmission (where "cross-platform storage" and "networking" are required "Transfer" data needs to be serialized)

Common serialization methods are: JDK (does not support cross-language), JSON, XML, Hessian, Kryo (does not support cross-language), Thrift, Protostuff, FST (does not support cross-language)

C#'s operations on files:
A file is a collection of data stored on a disk with a specified name and directory path. When a file is opened for reading and writing, it becomes a stream.
There are two main streams: input stream and output stream. The input stream is used to read data from the file (read operation), and the output stream is used to write data to the file (write operation)
Insert picture description here

Usage:
general usage process: create a binary formatting object, create a new file stream (specify the path, where to write it), serialize the object, close the file stream
Create a serializable class: add features
Insert picture description here

Assignment:
Insert picture description here

Serialization: (here on the desktop, it is convenient to observe whether the file is created, it is recommended to use Appication.persistentDataPath)
Appication.persistentDataPath:
[Window]%userprofile%\AppData\Local\Packages<productname>\LocalState
[IOS]/var/mobile /Containers/Data/Application//Documents
[Andriod]/storage/emulated/0/Android/data//files The old device will be in the SD card
Insert picture description here

Run, open file
Insert picture description here

Try to load:
Insert picture description here

Summary: The core method
[Save] Use the serialization method of binary format to serialize the Save object
BinaryFormatter.Serialize(FileStream _fileStream, Object _object)
[Read] The deserialization method of the binary formatter, convert the file stream into one save object
Save save = (Save)BinaryFormatter.Deserialize(FileStream _fileStream);

Guess you like

Origin blog.csdn.net/euphorias/article/details/108400943