Game.City.CityModifier
Assembly: Game
Namespace: Game.City
Type: struct
Base: IBufferElementData, ISerializable
Summary:
CityModifier is a small buffer element used to store a two-component modification delta (float2) for city data. It is a Unity ECS IBufferElementData so it can be attached as a dynamic buffer on an entity, and it implements a custom serialization contract (ISerializable) to support versioned save/load compatibility. The struct includes backwards compatibility handling: older data formats stored only the Y component and are read accordingly.
Fields
public float2 m_Delta
Stores the modifier delta as a float2 (x, y). Used to represent the change/offset applied by this modifier. In older saved formats only the y component was present; current format stores the full float2.
Properties
- None
Constructors
- Default parameterless constructor (generated for the struct)
No explicit constructors are declared in the source file; the default struct constructor initializes m_Delta to (0,0).
Methods
-
public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
Writes the m_Delta value to the provided writer. Uses the writer.Write(m_Delta) call which serializes both components in the current format. -
public void Deserialize<TReader>(TReader reader) where TReader : IReader
Reads data from the provided reader, with version-aware handling: - If reader.context.version >= Version.modifierRefactoring, the code reads the full float2 into m_Delta.
- Otherwise (older versions), it reads only the y component and writes that into m_Delta.y, preserving compatibility with legacy serialized data that only recorded the Y value.
The method uses ref locals to obtain a reference to the destination field before calling reader.Read to avoid unnecessary copies.
Usage Example
// Example: add a CityModifier buffer element to an entity and set its delta:
var buffer = entityManager.AddBuffer<Game.City.CityModifier>(entity);
buffer.Add(new Game.City.CityModifier { m_Delta = new float2(1.0f, 2.5f) });
// Serialization is automatic via the ISerializable implementation when the game's save system invokes it.
// Deserialization handles older save versions where only the Y component was stored.