liveBook · Manning

BinaryWriter

As explained in High Performance Unity Game Development

BinaryWriter is a .NET class that writes primitive types as binary data to a stream.

BinaryWriter writes typed values to a stream as a continuous sequence of bytes. Reading them back requires using the same order as writing. section 7.1.1

Saving enemy data

Write an enemy count as an integer, then write each enemy position’s x and y values. Read the values back in that same order. section 7.1.1

Listing 7.5 Writing data in binary
string fileName = Application.persistentDataPath + "/DODSurvivor/gamedata.dat";
using (FileStream fs = File.Create(fileName))
using (BinaryWriter bw = new BinaryWriter(fs))
{
    int numEnemies = gameData.EnemyPosition.Length;
    bw.Write(numEnemies);
}

A Unity save-file example creates a FileStream, wraps it in BinaryWriter, and writes the enemy count. section 7.1.1

How High Performance Unity Game Development covers it

How deep it goes
Explains byte-stream layout, type widths, matching reads, and smaller-integer trade-offs; defers backward compatibility.
Examples
Uses enemy counts and positions as examples, writing integer counts and floating-point coordinates.
What it assumes
Defines binary storage and serialization from basics; assumes a GameData structure and C# primitive values.
What you can do after
Apply C# BinaryWriter to write typed game data to a file, preserving the order needed for reading it back.
How it connects
Connects binary serialization to FileStream, BinaryReader, deserialization, and save-game systems.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of High Performance Unity Game Development. The section links point to the parts of the book it draws on.