Skip to content

Game.Prefabs.PrefabRef

Assembly: Assembly-CSharp.dll
Namespace: Game.Prefabs

Type: struct

Base: IComponentData, IQueryTypeParameter, IStrideSerializable, ISerializable

Summary:
A small ECS component wrapper that holds a reference to a prefab Entity. PrefabRef is serializable (via Colossal.Serialization) and provides a fixed stride (4 bytes) for serialization. It can be used as a component in Unity.Entities-based systems and as a query type parameter. An implicit conversion to Entity is provided for convenient use in code.


Fields

  • public Unity.Entities.Entity m_Prefab
    Holds the Entity reference to the prefab. This is the actual serialized value written/read by the Serialize/Deserialize implementations.

Properties

  • None.
    PrefabRef exposes its data via the public field and an implicit conversion operator rather than properties.

Constructors

  • public PrefabRef(Unity.Entities.Entity prefab)
    Creates a new PrefabRef wrapping the provided prefab Entity.

Methods

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes the wrapped Entity to the provided writer. Uses Colossal.Serialization.Entities writer semantics to persist the Entity reference.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads an Entity from the provided reader into m_Prefab. Complement of Serialize.

  • public int GetStride(Context context)
    Returns the stride (size in bytes) used for serialization. This implementation returns 4, indicating the serialized representation occupies 4 bytes (typically an integer identifier).

  • public static implicit operator Unity.Entities.Entity(PrefabRef prefabRef)
    Implicitly converts the PrefabRef to the underlying Entity (returns prefabRef.m_Prefab). Useful for passing PrefabRef values directly where an Entity is required.

Usage Example

// create a PrefabRef component from an existing prefab entity
var prefabEntity = /* obtain prefab Entity somehow */;
var prefabRef = new Game.Prefabs.PrefabRef(prefabEntity);

// implicit conversion to Entity
Unity.Entities.Entity e = prefabRef;

// add as a component to an entity (EntityManager usage example)
entityManager.AddComponentData(someEntity, prefabRef);

// custom serialization usage (writer/reader provided by Colossal.Serialization)
writer.Write(prefabRef); // uses PrefabRef.Serialize internally
reader.Read(out Game.Prefabs.PrefabRef loadedPrefabRef); // uses Deserialize

{{ PrefabRef is intended for use in Cities: Skylines 2 modding where prefabs are represented by Entities. Because it implements IStrideSerializable and ISerializable, it integrates with the game's custom serialization pipeline. The fixed GetStride value (4) means the serialized form is compact; ensure any bespoke reader/writer used in custom tools supports the Colossal serialization conventions for Entity types. }}