Skip to content

Game.Prefabs.DefaultPolicies

Assembly: Assembly-CSharp.dll
Namespace: Game.Prefabs

Type: class

Base: ComponentBase

Summary:
Component used on prefab definitions to specify a list of default policy prefabs that should be applied to instances of that prefab. During prefab initialization this component: - Declares that the prefab requires a DefaultPolicyData buffer on created entities. - Adds Policy to the archetype components unless the prefab is a Waypoint or Segment (routes/road elements). - Records references to the referenced policy prefabs as DefaultPolicyData entries on the instantiated entity so runtime systems can apply or query those default policies.


Fields

  • public DefaultPolicyInfo[] m_Policies
    Array of policy references (DefaultPolicyInfo) assigned in the prefab. Each entry refers to a policy prefab that should be recorded on the instantiated entity. During LateInitialize the component resolves each referenced prefab to an Entity (via PrefabSystem) and appends a DefaultPolicyData entry to the entity's buffer.

Properties

  • None. This component exposes a public field (m_Policies) and relies on overridden prefab lifecycle methods; it does not declare additional properties.

Constructors

  • public DefaultPolicies()
    Default parameterless constructor (usual Unity/authoring behavior). There is no special construction logic beyond the base ComponentBase constructor.

Methods

  • public override void GetDependencies(System.Collections.Generic.List<PrefabBase> prefabs)
    Adds any referenced policy prefabs to the provided dependency list. This ensures the policy prefabs are loaded/serialized together with this prefab. Implementation: iterates m_Policies and adds each m_Policy to prefabs if m_Policies is not null.

  • public override void GetPrefabComponents(System.Collections.Generic.HashSet<Unity.Entities.ComponentType> components)
    Registers the runtime component types required by instances of this prefab. This component adds ComponentType.ReadWrite() so instantiated entities will have a dynamic buffer to store default policy references.

  • public override void GetArchetypeComponents(System.Collections.Generic.HashSet<Unity.Entities.ComponentType> components)
    Conditionally adds the Policy component to the prefab archetype unless the prefab already contains Waypoint or Segment components. This prevents adding Policy to route/road elements where Policy is not appropriate. Behavior: if neither Waypoint nor Segment are present in the provided set, adds ComponentType.ReadWrite().

  • public override void LateInitialize(Unity.Entities.EntityManager entityManager, Unity.Entities.Entity entity)
    Called late in the prefab initialization lifecycle to populate runtime data on the created entity. For each DefaultPolicyInfo in m_Policies it:

  • Uses the PrefabSystem (from entityManager.World.GetExistingSystemManaged()) to resolve the referenced policy prefab to an Entity.
  • Appends a new DefaultPolicyData(entryEntity) to the entity's DefaultPolicyData DynamicBuffer. This produces a buffer of DefaultPolicyData entries that other systems can read to know which policy prefabs were declared as defaults.

Usage Example

// Typical usage: the prefab author fills m_Policies in the prefab asset in the editor.
// At runtime, after prefab instantiation the LateInitialize implementation above will populate
// the DefaultPolicyData buffer on the entity. Systems can then read that buffer:

void ApplyDefaultPolicies(EntityManager em, Entity instance)
{
    if (!em.HasComponent<DefaultPolicyData>(instance))
        return;

    var buffer = em.GetBuffer<DefaultPolicyData>(instance);
    foreach (var dp in buffer)
    {
        // DefaultPolicyData wraps an Entity that comes from the referenced policy prefab.
        // Use that entity as needed (e.g., instantiate policy instances or apply settings).
        Entity policyPrefabEntity = dp.PolicyPrefabEntity; // field name may vary in actual struct
        // ... apply/inspect policyPrefabEntity ...
    }
}

Notes and remarks: - DefaultPolicyInfo and DefaultPolicyData are separate types defined elsewhere; DefaultPolicyInfo is an authoring helper that references a policy prefab, and DefaultPolicyData is the runtime buffer element that holds the resolved Entity reference. - The component ensures prefabs that need policy data will have the appropriate components and archetype layout so systems relying on Policy or DefaultPolicyData can operate reliably. - This logic runs at prefab initialization time (editor or runtime prefab instantiation), not per-frame.