Skip to content

Game.Prefabs.BrandObject

Assembly: Game
Namespace: Game.Prefabs

Type: Class

Base: ComponentBase

Summary:
Represents a prefab component that associates a prefab entity with a BrandPrefab. When a prefab containing this component is initialized, it records a requirement entry (ObjectRequirementElement) linking the prefab entity to the brand entity provided by the PrefabSystem. This is used by the prefab system to track object requirements and brand relationships for instantiated prefabs (e.g., objects that require or belong to a brand).


Fields

  • public BrandPrefab m_Brand
    Reference to the BrandPrefab asset this BrandObject requires/represents. During LateInitialize this reference is resolved to an entity via PrefabSystem.GetEntity and an ObjectRequirementElement is appended to the object's buffer. Note: the implementation does not null-check m_Brand, so a null reference may result in runtime errors when prefabs are initialized.

Properties

  • This class does not declare any properties.

Constructors

  • public BrandObject()
    No explicit constructor is declared in the source; the default parameterless constructor inherited from the base class is used. Initialization of m_Brand is expected to be performed via the editor / prefab asset.

Methods

  • public override void GetDependencies(System.Collections.Generic.List<PrefabBase> prefabs)
    Adds the referenced BrandPrefab to the provided dependency list so the prefab system knows that this prefab depends on the BrandPrefab (ensures the brand prefab is also loaded/created). Calls base.GetDependencies(prefabs) before adding m_Brand.

  • public override void GetPrefabComponents(System.Collections.Generic.HashSet<Unity.Entities.ComponentType> components)
    Declares which components will be present on the prefab entity at runtime. This method adds:

  • ComponentType.ReadWrite()
  • ComponentType.ReadWrite() These indicate the prefab will contain a dynamic buffer of ObjectRequirementElement and a BrandObjectData component.

  • public override void GetArchetypeComponents(System.Collections.Generic.HashSet<Unity.Entities.ComponentType> components)
    Empty override. No additional archetype components are declared here; archetype modifications (if needed) are not performed by this component.

  • public override void LateInitialize(Unity.Entities.EntityManager entityManager, Unity.Entities.Entity entity)
    Performed late in prefab/entity initialization. Implementation:

  • Calls base.LateInitialize(entityManager, entity).
  • Obtains the PrefabSystem instance from the entityManager.World.
  • Retrieves the DynamicBuffer on the entity.
  • Appends a new ObjectRequirementElement constructed with the entity corresponding to m_Brand (via PrefabSystem.GetEntity(m_Brand)) and the current buffer length (used as an index/slot). This effectively registers the brand requirement for the entity.

Notes: - This method assumes that the buffer already exists on the entity (GetPrefabComponents declared it) and that PrefabSystem.GetEntity(m_Brand) returns a valid entity. - No error handling is present for missing buffer, missing system, or null m_Brand.

Usage Example

// Typical prefab component usage: the BrandObject is placed on a prefab and
// the m_Brand field is assigned in the editor to a BrandPrefab asset.
// At runtime, when the prefab is converted to an entity, LateInitialize runs
// and records the brand requirement:

[ComponentMenu("Objects/", new Type[] { typeof(ObjectPrefab) })]
public class BrandObject : ComponentBase
{
    public BrandPrefab m_Brand;

    public override void LateInitialize(EntityManager entityManager, Entity entity)
    {
        base.LateInitialize(entityManager, entity);

        // Resolve BrandPrefab to an entity and add an ObjectRequirementElement to the buffer
        PrefabSystem prefabSystem = entityManager.World.GetExistingSystemManaged<PrefabSystem>();
        DynamicBuffer<ObjectRequirementElement> buffer = entityManager.GetBuffer<ObjectRequirementElement>(entity);
        int index = buffer.Length;
        buffer.Add(new ObjectRequirementElement(prefabSystem.GetEntity(m_Brand), index));
    }
}

{{ Additional details: - Dependencies: PrefabBase, BrandPrefab, ObjectRequirementElement, BrandObjectData, PrefabSystem, EntityManager, Entity, DynamicBuffer, ComponentType. - Purpose: used by prefab conversion/initialization to link instantiated objects to brand prefabs and to populate object requirement buffers for later game systems to consume. - Implementation considerations: consider adding null checks for m_Brand and existence checks for the PrefabSystem and buffer if runtime robustness is desired. }}