liveBook · Manning

Class

As explained in High Performance Unity Game Development

A language construct that defines the data and behavior of objects created from that type.

A survival game needs to update many enemies. One approach is to keep their positions, directions, and velocities in separate arrays held by a single class. The class groups long-lived game data; the arrays let each update loop work through the data it needs. section 2.1 section 2.7

Listing 2.1 C# class holding all the data for all enemies
public class EnemyData
{
    public Vector2[] Position;
    public Vector2[] Direction;
    public float[] Velocity;
}

The class holds three arrays rather than one separate object for every enemy. section 2.1

Choose a class for data containing arrays or strings, data whose fields are not always used together, or data too large to make a convenient small value. A small struct is a better fit when its fixed-size fields are used together. An array of structs stores its elements contiguously; an array of classes holds references to objects that may be spread across the heap. That difference can affect cache misses during iteration. section 2.1 section 2.8

Passing a class object to a function copies its reference, not the whole object. The function can therefore change the original object's data. This is convenient, but a parameter typed as a broad game-data class does not reveal which fields the function might modify. A struct field inside that class behaves differently when passed on its own: passing it by value copies the field, so changes to the parameter do not update the original. Pass it with `ref`, or return and assign the changed value, when the original must change. section 2.4.1

Listing 2.16 Correct example of passing a class
public void MovePlayerHorizontally(GameData gameData, float amount)
{
    gameData.PlayerPosition.x += amount;
}
 
GameData gameData = new GameData();
MovePlayerHorizontally(gameData, 10.0f);

Passing the game-data class lets the movement function update its player-position field through the shared object. section 2.4.1

How High Performance Unity Game Development covers it

How deep it goes
Compares class and struct storage, array locality, and function passing through C# examples; does not develop class methods or inheritance.
Examples
Shows an enemy-data class holding arrays and a game-data class passed to a player-movement function.
What it assumes
Assumes familiarity with C# fields, arrays, and functions while explaining class-versus-struct storage and passing choices.
What you can do after
Apply it by choosing classes for suitable game data and anticipating how class references behave in functions and arrays.
How it connects
Connects classes to structs, heap allocation, data locality, arrays, and pass-by-value behavior.

Also explained in

Compare how 27 books explain class →

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.