Course contentsShow
← Unity Game Development
Lesson 796 of 82812. Multiplayer and DeploymentFree lesson

Optimizing Network Traffic with Variable Delta Compression

You will configure synchronization settings to minimize bandwidth usage by sending only the necessary data changes rather than full object states.

Optimizing Network Traffic with Variable Delta Compression

In this lesson, you will learn how to drastically reduce your game's bandwidth consumption by transmitting only the specific changes (deltas) between consecutive network updates rather than replicating the entire object state.

The Problem: Redundant Data

In a basic synchronization setup, you might be tempted to send every variable of a NetworkBehaviour every time the server ticks. If an object has a state containing health, position, rotation, ammo, and inventory, sending that entire struct 20 or 60 times per second is incredibly wasteful. Most of the time, the player's health or inventory won't change, meaning you are sending "stale" data over and over.

The Solution: Variable Delta Compression

Delta compression works by comparing the current state of an object against the last state the client acknowledged receiving. You only serialize and transmit the data fields that have actually changed since the last update.

Instead of a bulky struct PlayerState that you send in its entirety, you use a bitmask or a conditional flag system. Imagine a simplified health synchronization:

public override void NetworkSerialize<T>(BufferSerializer<T> serializer)
{
    // We only send the value if it has changed
    if (serializer.IsReader || CurrentHealth != LastSentHealth)
    {
        serializer.SerializeValue(ref CurrentHealth);
        LastSentHealth = CurrentHealth;
    }
}

Implementing Efficient Deltas

To scale this, define an "update mask" for your NetworkObject. The server keeps track of what the client already knows. If the client’s LastKnownState matches the current ServerState, no packet is sent. If they differ, the server calculates the difference and sends only the modified fields.

When you implement this, ensure your NetworkVariable updates are prioritized. For high-frequency data like position, you might use lossy delta compression (rounding values to reduce precision), whereas for critical state changes like "Player Died," you ensure zero loss by forcing a full sync only when that specific value changes. By moving away from "send everything" to "send what changed," you free up significant bandwidth for more players and more complex interactions.

Key Takeaway: Variable Delta Compression slashes bandwidth usage by transmitting only the modified subsets of your data, ensuring your network traffic scales efficiently as your game’s complexity grows.