Skip to content

Game.Prefabs.Effects.CitizenSelectedSound

Assembly:
Assembly-CSharp

Namespace:
Game.Prefabs.Effects

Type:
class

Base:
ComponentBase

Summary:
Component that binds a list of selectable citizen sound options to a prefab. When a prefab with this component is initialized, the component registers a DynamicBuffer on the entity and fills it with entries derived from the configured CitizenSelectedSoundInfo array. It also reports the required component types and declares prefab dependencies for any referenced sound prefabs so those prefabs are loaded/created as entities.


Fields

  • public CitizenSelectedSoundInfo[] m_CitizenSelectedSounds
    Array of configuration entries describing which sound prefab to play for different citizen selection conditions. Each CitizenSelectedSoundInfo contains at least: m_IsSickOrInjured, m_Age, m_Happiness and m_SelectedSound (a PrefabBase reference). Used during LateInitialize to populate the entity's CitizenSelectedSoundData buffer.

Properties

  • (none)

Constructors

  • (default) public CitizenSelectedSound()
    Uses the default parameterless constructor inherited from System.Object. Initialization of data is typically done via the inspector (serialized m_CitizenSelectedSounds) and during LateInitialize at prefab instantiation.

Methods

  • public override void GetPrefabComponents(System.Collections.Generic.HashSet<Unity.Entities.ComponentType> components)
    Adds the runtime component type required by this component to the provided set. This component calls: components.Add(ComponentType.ReadWrite()); This ensures prefab archetypes include a DynamicBuffer for entities created from the prefab.

  • public override void GetDependencies(System.Collections.Generic.List<PrefabBase> prefabs)
    Adds referenced sound prefabs (m_SelectedSound from each m_CitizenSelectedSounds entry) to the provided prefabs list. This ensures those prefabs are treated as dependencies and are created/available as entities when needed.

  • public override void LateInitialize(Unity.Entities.EntityManager entityManager, Unity.Entities.Entity entity)
    Called after the prefab entity is created. This method:

  • Gets the PrefabSystem from the world.
  • If m_CitizenSelectedSounds is not null, obtains the DynamicBuffer for the entity.
  • Iterates the configured infos and adds new CitizenSelectedSoundData entries constructed from each info (m_IsSickOrInjured, m_Age, m_Happiness) and the entity representing the referenced sound prefab (via PrefabSystem.GetEntity(m_SelectedSound)).

  • public override void GetArchetypeComponents(System.Collections.Generic.HashSet<Unity.Entities.ComponentType> components)
    Empty override. This component does not declare additional archetype components here (the buffer type is declared in GetPrefabComponents).

Usage Example

// This shows how the DynamicBuffer<CitizenSelectedSoundData> created by this component
// can be accessed during runtime (for example, by a system that plays a sound when a citizen is selected).

protected void OnCitizenSelected(EntityManager entityManager, Entity prefabEntity)
{
    if (entityManager.HasComponent<CitizenSelectedSoundData>(prefabEntity))
    {
        DynamicBuffer<CitizenSelectedSoundData> buffer = entityManager.GetBuffer<CitizenSelectedSoundData>(prefabEntity);
        for (int i = 0; i < buffer.Length; i++)
        {
            CitizenSelectedSoundData data = buffer[i];
            // data contains fields populated from CitizenSelectedSoundInfo (isSick, age, happiness, soundEntity)
            // Use data.soundEntity (an Entity obtained via PrefabSystem) to play the appropriate audio.
        }
    }
}

Notes and implementation details: - The component is annotated with [ComponentMenu("Effects/", new Type[] { typeof(CitizenPrefab) })], meaning it's intended to be attached to citizen-related prefabs in the editor. - The component relies on PrefabSystem.GetEntity(PrefabBase) to turn referenced PrefabBase (m_SelectedSound) into an Entity used at runtime. - The type CitizenSelectedSoundData and CitizenSelectedSoundInfo are related types: - CitizenSelectedSoundInfo is a serialized editor struct holding selection criteria and a PrefabBase reference for the sound. - CitizenSelectedSoundData is the runtime data stored in the entity's DynamicBuffer, containing the resolved sound Entity and the selection metadata. - Ensure referenced sound prefabs are valid PrefabBase instances so GetDependencies and LateInitialize can resolve them correctly.