Game.UISoundCollection
Assembly:
Assembly-CSharp
Namespace:
Game
Type:
class (ScriptableObject)
Base:
UnityEngine.ScriptableObject
Summary:
A Unity ScriptableObject that stores a collection of UI sounds and provides convenience methods to play them by index or name. Sounds are defined by the nested SoundInfo class (name, AudioClip, and per-sound volume). The asset uses a dictionary (m_SoundsDict) to enable quick lookup by name and is intended to be used with the game's AudioManager (AudioManager.instance.PlayUISound). The asset is creatable from the Unity menu via the CreateAssetMenu attribute.
Fields
-
public SoundInfo[] m_Sounds
Holds the array of SoundInfo entries configured in the ScriptableObject. Each entry contains a name, AudioClip, and per-sound volume multiplier. -
private System.Collections.Generic.Dictionary<string, SoundInfo> m_SoundsDict
Runtime dictionary that maps sound names to SoundInfo for fast lookup by name. It is (re)populated in OnEnable() and by RefreshSoundsDict().
Nested type: SoundInfo
- public class SoundInfo
- public string m_Name
The unique name/key for the sound used for name-based playback.
- public UnityEngine.AudioClip m_Clip
The AudioClip to play for this sound.
- public float m_Volume
Per-sound volume multiplier in range [0, 1]. Default is 1.0. The Range attribute clamps this in the Unity inspector.
Properties
- (none)
This class does not expose properties; it uses public fields for inspector serialization and a private dictionary for runtime lookups.
Constructors
public UISoundCollection()
Default parameterless constructor provided by Unity for ScriptableObject instances. Initialization logic for the dictionary and array happens in OnEnable(), not in a custom constructor.
Methods
-
private void OnEnable()
Called by Unity when the ScriptableObject is enabled/loaded. Ensures m_Sounds is not null (initializes to empty array if needed), creates the m_SoundsDict dictionary, and populates it by calling RefreshSoundsDict(). -
public void PlaySound(int soundIndex, float volume = 1f)
Plays the sound at the given index in m_Sounds if the index is valid. The final playback volume is multiplied by the SoundInfo.m_Volume. If the index is out of range, the call is ignored. -
public void PlaySound(string soundName, float volume = 1f)
Looks up the sound by name in m_SoundsDict and plays it if found. The final playback volume is multiplied by the SoundInfo.m_Volume. If the name is not found, the call is ignored. -
private void PlaySound([NotNull] UnityEngine.AudioClip clip, float volume)
Internal helper that plays the provided AudioClip via AudioManager.instance.PlayUISound if Camera.main exists. The NotNull annotation indicates the clip should not be null. If Camera.main is not available or AudioManager.instance is null, nothing is played. -
public void RefreshSoundsDict()
Clears and rebuilds m_SoundsDict from the current m_Sounds array. Call this if you modify m_Sounds at runtime or after deserialization changes to ensure the name->SoundInfo mapping is up-to-date.
Usage Example
// Example usages from game code or a MonoBehaviour:
// Assume you have a reference to a UISoundCollection asset (assigned in inspector).
public UISoundCollection uiSounds;
// Play by index (uses per-sound volume multiplier)
uiSounds.PlaySound(0); // Play first sound at full volume
uiSounds.PlaySound(2, 0.8f); // Play third sound at 80% * sound's own volume
// Play by name (safer if indices may change)
uiSounds.PlaySound("ButtonClick"); // Play sound named "ButtonClick"
uiSounds.PlaySound("CloseWindow", 0.5f);
// If you modify uiSounds.m_Sounds at runtime (e.g. added/removed entries),
// call RefreshSoundsDict() to rebuild the name lookup dictionary:
uiSounds.RefreshSoundsDict();
Additional notes: - The ScriptableObject is created via the Unity menu thanks to [CreateAssetMenu(menuName = "Colossal/UI/UISoundCollection")]. - Playback depends on Camera.main existing and on AudioManager.instance being available. If either is missing, PlaySound will not produce audio. - Use unique m_Name values for sounds to avoid collisions in the dictionary; later entries overwrite earlier ones with the same key.