Game.Prefabs.ResourceConsumer
Assembly: Assembly-CSharp
Namespace: Game.Prefabs
Type: public class
Base: ComponentBase
Summary:
Component added to building prefabs that consume resources. Exposes an optional notification icon prefab (shown when the building lacks required resources) and participates in the prefab → ECS conversion by registering required component types and writing ResourceConsumerData to the entity during LateInitialize. The class is decorated with ComponentMenu (for editor menu placement) and RequireComponent(typeof(CityServiceBuilding)) meaning it is intended for city service-type building prefabs.
Fields
public NotificationIconPrefab m_NoResourceNotificationPrefab
[CanBeNull] Optional reference to a NotificationIconPrefab used to display a "no resource" notification for the building. If set, the prefab is added to dependencies so it can be converted to an entity and its entity reference stored in ResourceConsumerData during LateInitialize.
Properties
- None declared on this class.
(Inherited properties from ComponentBase are available but not listed here.)
Constructors
public ResourceConsumer()
Default parameterless constructor (no explicit initialization performed in the source). The class relies on Unity's serialized field assignment for m_NoResourceNotificationPrefab and on LateInitialize to write ECS component data.
Methods
-
public override void GetDependencies(System.Collections.Generic.List<PrefabBase> prefabs)
Adds m_NoResourceNotificationPrefab to the provided prefabs list if it is not null, ensuring the notification prefab is included in conversion/dependency processing. -
public override void GetPrefabComponents(System.Collections.Generic.HashSet<Unity.Entities.ComponentType> components)
Registers the managed prefab-side component type that will be present on converted entities: ComponentType.ReadWrite(). -
public override void GetArchetypeComponents(System.Collections.Generic.HashSet<Unity.Entities.ComponentType> components)
Registers the runtime archetype ECS component that corresponds to this prefab: ComponentType.ReadWrite(). -
public override void LateInitialize(Unity.Entities.EntityManager entityManager, Unity.Entities.Entity entity)
During late-initialization of the prefab-to-entity conversion, this method resolves the m_NoResourceNotificationPrefab to an entity (via PrefabSystem.GetEntity) and writes a ResourceConsumerData instance to the target entity. If m_NoResourceNotificationPrefab is null, the field is set to Entity.Null.
Notes:
- Uses PrefabSystem (retrieved via entityManager.World.GetOrCreateSystemManaged
Usage Example
public override void GetDependencies(List<PrefabBase> prefabs)
{
base.GetDependencies(prefabs);
if (m_NoResourceNotificationPrefab != null)
{
prefabs.Add(m_NoResourceNotificationPrefab);
}
}
public override void LateInitialize(EntityManager entityManager, Entity entity)
{
base.LateInitialize(entityManager, entity);
var prefabSystem = entityManager.World.GetOrCreateSystemManaged<PrefabSystem>();
entityManager.SetComponentData(entity, new ResourceConsumerData
{
m_NoResourceNotificationPrefab = (m_NoResourceNotificationPrefab != null)
? prefabSystem.GetEntity(m_NoResourceNotificationPrefab)
: Entity.Null
});
}
Additional info: - This component is intended for use on building prefabs (BuildingPrefab / BuildingExtensionPrefab) that require resource consumption behavior. - Ensure any NotificationIconPrefab assigned is also a valid prefab asset so it can be converted to an entity during build/time conversion.