Game.Prefabs.CompanyObject
Assembly: Assembly-CSharp
Namespace: Game.Prefabs
Type: class
Base: ComponentBase
Summary:
Component used on object prefabs to attach one or more CompanyPrefab references as object requirements. On prefab late-initialization this component appends ObjectRequirementElement entries to the object's DynamicBuffer
Fields
-
public bool m_SelectCompany
Controls the ObjectRequirementType used when adding requirement entries. When true the code uses ObjectRequirementType.SelectOnly for every added requirement. Default false. -
public CompanyPrefab[] m_Companies
Array of CompanyPrefab references that will be added as requirements for this object. These are also registered as prefab dependencies (see GetDependencies). Note: the code does not null-check this array, so it must be set (or treated as empty) to avoid NullReferenceException.
Properties
- This class does not expose any public properties.
Constructors
public CompanyObject()
Default constructor (Unity/serialization constructed). No custom construction logic in the source.
Methods
-
public override void GetDependencies(System.Collections.Generic.List<PrefabBase> prefabs)
Adds every CompanyPrefab from m_Companies to the provided prefabs list. This ensures the referenced company prefabs are included as dependencies of the object prefab. -
public override void GetPrefabComponents(System.Collections.Generic.HashSet<Unity.Entities.ComponentType> components)
Adds ComponentType.ReadWrite() to the components set. This indicates the prefab requires a DynamicBuffer on the instantiated entity. -
public override void GetArchetypeComponents(System.Collections.Generic.HashSet<Unity.Entities.ComponentType> components)
Empty implementation — this component does not add archetype components beyond what GetPrefabComponents declares. -
public override void LateInitialize(Unity.Entities.EntityManager entityManager, Unity.Entities.Entity entity)
Implementation details: - Obtains the PrefabSystem from the EntityManager.World.
- Retrieves the DynamicBuffer
for the object entity. - Records the current buffer length (used as the index parameter when creating new entries).
- Determines ObjectRequirementType: ObjectRequirementType.SelectOnly if m_SelectCompany == true, otherwise 0 (no special type).
- For each CompanyPrefab in m_Companies it:
- Resolves the company prefab's entity via PrefabSystem.GetEntity(m_Companies[i]).
- Adds a new ObjectRequirementElement(companyEntity, length, type) to the buffer.
Behavioral notes and caveats: - If m_Companies is null the method will throw. Ensure the array is initialized (empty or populated). - PrefabSystem must exist in the world; if it doesn't, GetExistingSystemManaged will return null and GetEntity calls will fail. - The meaning of the ObjectRequirementElement constructor arguments (entity, index, type) is: reference to the company entity, the slot/index (buffer length captured before additions), and the requirement type (e.g., SelectOnly). - This method mutates the DynamicBuffer attached to the entity — it is intended to run during prefab initialization.
Usage Example
// Example: prepare a CompanyObject (editor or code) and inspect buffer after LateInitialize
// Create/assign in editor:
// - Add CompanyObject to your ObjectPrefab
// - Assign CompanyPrefab references to m_Companies
// - Toggle m_SelectCompany if you want SelectOnly requirements
// Runtime: after prefab instantiation / LateInitialize ran:
DynamicBuffer<ObjectRequirementElement> buffer = entityManager.GetBuffer<ObjectRequirementElement>(objectEntity);
for (int i = 0; i < buffer.Length; i++)
{
ObjectRequirementElement elem = buffer[i];
// elem.entity is the company entity returned from PrefabSystem.GetEntity(companyPrefab)
// elem.index is the original buffer length (slot index)
// elem.type indicates ObjectRequirementType.SelectOnly if m_SelectCompany was true
UnityEngine.Debug.Log($"Requirement[{i}] -> companyEntity={elem.m_Entity} index={elem.m_Index} type={elem.m_Type}");
}