Game.Prefabs.Effects.SelectedSound
Assembly: Game (Assembly-CSharp)
Namespace: Game.Prefabs.Effects
Type: class
Base: ComponentBase
Summary:
SelectedSound is a prefab component used to link a sound effect prefab (EffectPrefab) to the spawned ECS entity. During prefab initialization it resolves the referenced EffectPrefab to an Entity via the PrefabSystem and writes a SelectedSoundData component (containing that entity reference) onto the runtime entity. It also registers the referenced EffectPrefab as a dependency so the sound prefab is included when the parent prefab is processed.
Fields
public EffectPrefab m_SelectedSound
This is the editor/serializable reference to the sound effect prefab that should be played/used when the entity is selected. It must be assigned on the prefab asset. At runtime this reference is converted to an Entity and stored in the SelectedSoundData component.
Properties
- (none)
This component exposes no runtime properties; it only contains a serialized prefab reference and writes a separate ECS component (SelectedSoundData) during initialization.
Constructors
public SelectedSound()
Default parameterless constructor (provided by C#). No special construction logic is implemented in the class itself — initialization happens in LateInitialize.
Methods
-
public override void GetArchetypeComponents(HashSet<ComponentType> components)
This method is intentionally empty. SelectedSound does not add archetype-specific components at authoring time; it writes a prefab component (SelectedSoundData) instead. -
public override void GetPrefabComponents(HashSet<ComponentType> components)
Adds the runtime prefab component type required by this authoring component: components.Add(ComponentType.ReadWrite());
This ensures entities created from the prefab will have SelectedSoundData available to receive the resolved Entity reference to the sound. -
public override void LateInitialize(EntityManager entityManager, Entity entity)
Called during prefab-to-entity conversion. Behavior: - Calls base.LateInitialize(entityManager, entity).
- Obtains the PrefabSystem from the EntityManager's World.
- Resolves the EffectPrefab (m_SelectedSound) to an Entity via PrefabSystem.GetEntity(m_SelectedSound).
-
Creates a SelectedSoundData instance with m_selectedSound set to that resolved Entity and writes it to the runtime entity via entityManager.SetComponentData(entity, componentData). Note: This is the critical step that converts the serialized prefab reference into an ECS Entity reference for runtime usage.
-
public override void GetDependencies(List<PrefabBase> prefabs)
Registers dependencies by adding m_SelectedSound to the provided list. This ensures the referenced sound prefab is included/processed when the parent prefab is prepared. The method calls base.GetDependencies(prefabs) before adding the sound prefab.
Usage Example
// The component itself already performs conversion in LateInitialize.
// Example showing what happens during LateInitialize (same logic as the component).
[Preserve]
public override void LateInitialize(EntityManager entityManager, Entity entity)
{
base.LateInitialize(entityManager, entity);
var prefabSystem = entityManager.World.GetOrCreateSystemManaged<PrefabSystem>();
var soundEntity = prefabSystem.GetEntity(m_SelectedSound);
entityManager.SetComponentData(entity, new SelectedSoundData { m_selectedSound = soundEntity });
}
Notes and Modding Tips: - Ensure m_SelectedSound is assigned on the prefab asset in the editor or via code; otherwise SelectedSoundData will contain an invalid/Null Entity. - SelectedSoundData is the runtime ECS component consumers/systems should read to know which sound entity to play when the parent entity is selected. - Because this component writes a prefab component (SelectedSoundData), systems that run during entity initialization or first-update should expect that component to be present on entities created from this prefab. - If adding dependencies dynamically, make sure the referenced prefab is available to the mod load order to avoid missing references at runtime.