liveBook · Manning

Parsing

As explained in High Performance Unity Game Development

Analyzing encoded or structured input and converting it into program-readable values.

A survival game’s balance controls values such as enemy counts, movement speeds, and collision distances. Keeping those values on a scene GameObject makes them awkward for designers to edit. Parsing moves the editable balance into a separate asset and produces binary data the game can load. section 9.0 section 9.3

Do the conversion at tool time, not during gameplay. Validate missing or incorrect values before generating the file, then runtime code can trust that data instead of repeating avoidable checks. The binary layout can also turn enemy entries edited individually into arrays suited to data-oriented processing. section 9.0 section 9.8

A practical Unity workflow is:

  • Let designers edit balance in an external asset. ScriptableObjects make required data clear to designers and engineers; Google Sheets supports simultaneous editing but can be slow to parse into binary for builds. section 9.8
  • At tool time, parse the balance into a byte array and write it to a local binary file included with the build. section 9.2
  • At load time, read the bytes into the balance fields. If the format contains arrays, allocate them when loading, once their sizes are known. section 9.3
Listing 9.2 A class to parse our balance
public class BalanceParser : MonoBehaviour
{
#if UNITY_EDITOR
    [MenuItem("DOD/Balance/Parse Local")]
    public static void ParseLocal()
    {
        Debug.Log("Parse balance started!");
 
        if (!Directory.Exists(Application.dataPath + "/Resources"))
           Directory.CreateDirectory(Application.dataPath + "/Resources"); #A
 
        byte[] balanceArray = parse(); #B
 
        string path = Application.dataPath + "/Resources/balance.bytes"; #C
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
 
        using (FileStream fs = File.Create(path))
        using (BinaryWriter bw = new BinaryWriter(fs))
            bw.Write(balanceArray); #D
 
        Debug.Log("Parse balance finished!");
 
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }
 
    static byte[] parse()
    {
        using (MemoryStream stream = new MemoryStream())
        {
            using (BinaryWriter bw = new BinaryWriter(stream))
            {
                BalanceSO balanceSO = 
                    (BalanceSO)AssetDatabase.LoadAssetAtPath("Assets/Data/Balance.asset", 
                    typeof(BalanceSO)); #E
 
                bw.Write(balanceSO.NumEnemies);
                bw.Write(balanceSO.EnemyVelocity);
                bw.Write(balanceSO.EnemyRadius);
                bw.Write(balanceSO.SpawnRadius);
                bw.Write(balanceSO.PlayerVelocity);
                bw.Write(balanceSO.MinCollisionDistance);
                bw.Write(balanceSO.SpawnTime);
 
                return stream.ToArray(); #F
            }
        }
    }
#endif
}

The Unity Editor menu action runs the parser, obtains a byte array, and writes balance.bytes under Resources. The intermediate array can also be saved elsewhere. section 9.2

Listing 9.3 Loading the balance from the binary file
[Serializable]
public class Balance
{
    public int NumEnemies;
    public float EnemyVelocityMin;
    public float EnemyVelocityMax;
    public float PlayerVelocity;
    public float MinCollisionDistance;
 
    public void LoadBalance()
    {
        TextAsset asset = Resources.Load("balance") as TextAsset; #A
        LoadBalance(asset.bytes);
    }
 
    public void LoadBalance(byte[] array)
    {
        Stream s = new MemoryStream(array);
        using (BinaryReader br = new BinaryReader(s))
        {
            NumEnemies = br.ReadInt32();
            EnemyVelocity = br.ReadSingle();
            EnemyRadius = br.ReadSingle();
            SpawnRadius = br.ReadSingle();
            PlayerVelocity = br.ReadSingle();
            MinCollisionDistance = br.ReadSingle();
            SpawnTime = br.ReadSingle();
        }
    }
}

The loader reads a Resources TextAsset as bytes, then uses a BinaryReader to fill fields such as the enemy count and player velocity. section 9.3

Keep format changes and live updates separate from the simple parse-and-load path. A balance bundled with a build matches that build’s loading code; a downloaded balance must remain readable by the installed version. Retain older balance versions when needed to reproduce what players experienced, especially when testing variants. section 9.0 section 9.8

How High Performance Unity Game Development covers it

How deep it goes
Walks through byte-array output and binary field loading, while treating validation and versioning as adjacent concerns.
Examples
Uses a Unity Editor balance parser, a Resources binary file, and a BinaryReader-based balance loader.
What you can do after
Build it from scratch as a tool-time balance conversion and runtime loading workflow.
How it connects
Links parsing to ScriptableObjects, binary serialization, data-oriented arrays, tool-time validation, and balance versioning.

Also explained in

Compare how 17 books explain parsing →

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.