Game.Prefabs.PrefabData
Assembly:
Game
Namespace: Game.Prefabs
Type:
struct
Base:
System.ValueType
Summary:
PrefabData is a lightweight ECS component that stores an integer index referencing a game prefab. It is intended for use with Unity's Entities (ECS) in Cities: Skylines 2 modding. The struct implements serialization interfaces so the prefab index can be persisted and restored. It also implements IEnableableComponent semantics via ISerializeAsEnabled, allowing the component to be enabled/disabled and serialized accordingly.
Fields
public int m_Index
Stores the prefab index (an integer identifier) associated with the entity. This typically maps to a prefab table or manager elsewhere in the game code; the exact meaning of the index depends on the surrounding systems (for example, it might index into a prefab array or registry). The field is written and read during serialization by the implemented Serialize/Deserialize methods.
Properties
- None.
This struct exposes only a public field; there are no C# properties.
Constructors
public PrefabData()
Structs in C# have an implicit parameterless constructor that initializes m_Index to 0. You can also construct with an object initializer, e.g.new PrefabData { m_Index = 5 }
.
Methods
-
public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
Writes the m_Index value to the provided writer. Intended for saving component state. The TWriter is expected to implement the IWriter interface from the Colossal.Serialization.Entities system. -
public void Deserialize<TReader>(TReader reader) where TReader : IReader
Reads an integer from the provided reader into m_Index. Intended for restoring component state. The TReader is expected to implement the IReader interface from the Colossal.Serialization.Entities system.
Remarks: - Because these methods are generic over IWriter/IReader, they integrate with the game's serialization infrastructure and will be used when saving/loading worlds or prefabs. - The component also implements IEnableableComponent and ISerializeAsEnabled, so serialization code may consider the enabled/disabled state of the component when persisting it.
Usage Example
// Create and attach the component to an entity (using Unity.Entities API):
var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
var entity = entityManager.CreateEntity();
entityManager.AddComponentData(entity, new PrefabData { m_Index = 42 });
// Example of serializing the component (pseudo-code; depends on actual writer implementation):
public void SaveComponent<TWriter>(ref PrefabData data, TWriter writer) where TWriter : IWriter
{
data.Serialize(writer);
}
// Example of deserializing the component (pseudo-code; depends on actual reader implementation):
public void LoadComponent<TReader>(ref PrefabData data, TReader reader) where TReader : IReader
{
data.Deserialize(reader);
}
Additional notes: - Treat m_Index as an identifier — avoid making assumptions about stable values across different game versions or session runs unless documented by the modding APIs. - If you need to support enabling/disabling the component, use the ECS enable/disable APIs in combination with ISerializeAsEnabled support to ensure correct save/load behavior.