Game.Prefabs.UtilityObject
Assembly: Game
Namespace: Game.Prefabs
Type: class
Base: ComponentBase
Summary:
UtilityObject is a prefab component used to create utility-related ECS entities (for example water pipes, marker objects, etc.). It exposes editor-serializable fields for the utility type and position, declares which ECS components the prefab requires, defines the archetype components used when the prefab is instantiated, and writes initial UtilityObjectData into the entity during initialization. The class is annotated with a ComponentMenu attribute so it appears under "Objects/" in the editor and is associated with StaticObjectPrefab and MarkerObjectPrefab types.
Fields
-
public UtilityTypes m_UtilityType = UtilityTypes.WaterPipe
Specifies the kind of utility this prefab represents (enum UtilityTypes). Default value is UtilityTypes.WaterPipe. This value is copied into the ECS UtilityObjectData when the prefab is initialized. -
public float3 m_UtilityPosition
Local utility position (Unity.Mathematics.float3). This value is copied into UtilityObjectData.m_UtilityPosition during Initialize and represents the utility's offset/position used by runtime systems.
Properties
- (none declared in this class)
Constructors
public UtilityObject()
No explicit constructor is declared; the default parameterless constructor is used. Initialization of component fields happens via field initializers (m_UtilityType defaulting to WaterPipe) or in the editor.
Methods
public override void GetPrefabComponents(HashSet<ComponentType> components)
Adds the component types that the prefab requires at the "prefab" level. This implementation adds:-
ComponentType.ReadWrite
() This ensures the ECS component UtilityObjectData is part of the prefab definition and will be present on instantiated entities. -
public override void GetArchetypeComponents(HashSet<ComponentType> components)
Adds component types to the entity archetype used for instantiated prefab entities. This implementation adds: - ComponentType.ReadWrite
() -
ComponentType.ReadWrite
() These are the runtime components that entities created from this prefab will include. -
public override void Initialize(EntityManager entityManager, Entity entity)
Called when the prefab is instantiated into an entity. This method: - Invokes base.Initialize(entityManager, entity)
- Constructs a UtilityObjectData struct, sets its m_UtilityTypes to m_UtilityType and m_UtilityPosition to m_UtilityPosition
- Writes that data into the entity with entityManager.SetComponentData(entity, componentData) This populates the runtime ECS component with the prefab-configured values.
Additional notes: - The class is decorated with [ComponentMenu("Objects/", new Type[] { typeof(StaticObjectPrefab), typeof(MarkerObjectPrefab) })], which registers the prefab component in the editor menu and associates it with those prefab categories.
Usage Example
// Example: after the prefab is instantiated, UtilityObject.Initialize will
// set UtilityObjectData on the created entity. You can inspect it like this:
protected override void Initialize(EntityManager entityManager, Entity entity)
{
base.Initialize(entityManager, entity); // UtilityObject already does this in its implementation
// Read back the component data to verify values (for debugging)
var data = entityManager.GetComponentData<UtilityObjectData>(entity);
UnityEngine.Debug.Log($"Utility type: {data.m_UtilityTypes}, position: {data.m_UtilityPosition}");
}
// Example: set up a UtilityObject in code (or via the editor) before instantiation:
var utility = new Game.Prefabs.UtilityObject();
utility.m_UtilityType = UtilityTypes.Electric;
utility.m_UtilityPosition = new Unity.Mathematics.float3(1.5f, 0f, 2.0f);
// When this prefab is instantiated, Initialize will copy these values into the ECS component.