Game.Prefabs.ServiceObject
Assembly: Assembly-CSharp.dll
Namespace: Game.Prefabs
Type: class
Base: ComponentBase
Summary:
ServiceObject is a prefab component that links a ServicePrefab asset to a runtime entity by writing a ServiceObjectData component on the entity. It participates in prefab construction by declaring the prefab components required for instantiated entities, resolving the referenced ServicePrefab to its entity using the PrefabSystem during LateInitialize, and reporting the prefab dependency so that the referenced ServicePrefab is loaded/created first.
Fields
public ServicePrefab m_Service
Holds the author-assigned ServicePrefab reference (typically set in editor). At runtime this is resolved to an entity reference and stored inside the ServiceObjectData component on the instantiated entity.
Properties
- (none declared on this class)
This class does not expose additional properties. It uses ComponentBase overrides to describe runtime components and initialization behavior.
Constructors
public ServiceObject()
Default parameterless constructor (implicit). No additional initialization is performed in the class constructor — prefab field values come from the serialized prefab data.
Methods
-
public override void GetArchetypeComponents(HashSet<ComponentType> components)
Empty override. This implementation does not add any component types to an archetype at this stage. (Left blank intentionally — the class instead adds its runtime component in GetPrefabComponents.) -
public override void GetPrefabComponents(HashSet<ComponentType> components)
Adds the runtime component type required by this prefab to the provided set. Specifically: -
Adds ComponentType.ReadWrite
() so instantiated entities include ServiceObjectData. -
public override void LateInitialize(EntityManager entityManager, Entity entity)
Resolves the m_Service prefab reference to the entity that represents that prefab and writes a ServiceObjectData component to the entity: - Calls base.LateInitialize(entityManager, entity).
- Obtains the PrefabSystem from the world (entityManager.World.GetExistingSystemManaged
()). - Uses existingSystemManaged.GetEntity(m_Service) to get the entity representing the ServicePrefab.
-
Sets the entity's ServiceObjectData with the resolved entity reference: entityManager.SetComponentData(entity, new ServiceObjectData { m_Service = resolvedEntity });
-
public override void GetDependencies(List<PrefabBase> prefabs)
Reports dependencies for prefab loading. Calls base.GetDependencies(prefabs) then adds m_Service to the dependency list so the referenced ServicePrefab is considered a dependency of this prefab.
Usage Example
// Typical runtime flow (already implemented inside ServiceObject):
// - In the editor you assign m_Service to a ServicePrefab asset.
// - When the prefab is instantiated, the prefab system will:
// 1) Ensure the ServicePrefab dependency is created first (GetDependencies).
// 2) Create entity archetype that includes ServiceObjectData (GetPrefabComponents).
// 3) Call LateInitialize on the ServiceObject instance to resolve m_Service to its entity:
[Preserve]
public class MyCustomPrefabHandler : PrefabBase
{
// Example: you could inspect or rely on the ServiceObjectData on spawned entities:
public void OnPrefabSpawned(EntityManager em, Entity entity)
{
if (em.HasComponent<ServiceObjectData>(entity))
{
ServiceObjectData sod = em.GetComponentData<ServiceObjectData>(entity);
Entity serviceEntity = sod.m_Service;
// Now you can query or use the serviceEntity (e.g., read its components)
}
}
}
Notes: - ServiceObject assumes a ServiceObjectData component type exists with a field (m_Service) that stores an Entity reference. - PrefabSystem.GetEntity(ServicePrefab) is used to resolve a prefab asset to its runtime entity; this requires the PrefabSystem to be available on the world. - Because GetArchetypeComponents is empty, this class relies on GetPrefabComponents to ensure entities get the needed data component.