Skip to content

Game.UI.AudioBindings

Assembly:
Assembly-CSharp

Namespace:
Game.UI

Type:
public class

Base:
Colossal.UI.Binding.CompositeBinding

Summary:
AudioBindings is a small CompositeBinding that wires a UI binding ("audio", "playSound") to playing UI sounds. On construction it loads a UISoundCollection resource at "Audio/UI Sounds" (via Unity's Resources.Load) and registers a TriggerBinding that, when invoked, calls PlayUISound to play a named sound at a specified volume. This class is intended for use by the game's UI/binding infrastructure to provide a simple play-sound binding for mod or UI code.


Fields

  • private const string kGroup = "audio"
    This constant holds the binding group name used when registering the TriggerBinding. Grouping allows the binding system to organize and invoke bindings under the "audio" category.

  • private UISoundCollection m_SoundCollection
    Holds the loaded UISoundCollection asset used to play UI sounds. The collection is loaded once in the constructor using Resources.Load("Audio/UI Sounds"). If the resource is not present in the Resources folder, this will be null and PlayUISound will be a no-op.

Properties

  • (none)
    This class exposes no public properties. The binding is registered in the constructor and the PlayUISound method is private.

Constructors

  • public AudioBindings()
    Constructor behavior:
  • Calls Resources.Load("Audio/UI Sounds") to obtain the UISoundCollection used for playback.
  • Adds a TriggerBinding to the composite binding with group "audio", key "playSound", and handler PlayUISound. Notes:
  • Because the resource load occurs in the constructor, ensure the Unity Resources asset exists at Assets/Resources/Audio/UI Sounds (type matching UISoundCollection).
  • No explicit registration/attachment to a global binding manager happens here beyond adding the binding to this composite instance — how this instance is registered with the app's binding system depends on the surrounding code/framework.

Methods

  • private void PlayUISound(string soundName, float volume)
    Plays a UI sound from the loaded UISoundCollection:
  • If m_SoundCollection is not null, calls m_SoundCollection.PlaySound(soundName, volume).
  • If the collection is null, nothing happens (safe no-op). Notes and considerations:
  • soundName should match a sound entry defined in the UISoundCollection.
  • volume is forwarded to the UISoundCollection; ensure the value is within expected range (typically 0.0 to 1.0) depending on the collection implementation.
  • This method is private and intended to be invoked via the TriggerBinding when the binding system triggers the "audio"/"playSound" event.

Usage Example

// Create an instance of the bindings. How you register this instance with your
// UI/binding system depends on the mod/framework you are using.
var audioBindings = new Game.UI.AudioBindings();

// Once an instance is registered with the game's binding system, triggering
// the binding group "audio" and key "playSound" with parameters (soundName, volume)
// will result in the corresponding sound being played via the UISoundCollection.
//
// Example (binding trigger API will vary by framework; pseudo-code):
// BindingSystem.Trigger("audio", "playSound", "ButtonClick", 1.0f);

{{ Additional information and tips: - Resource placement: Place the UISoundCollection asset under Assets/Resources/Audio/ with the name "UI Sounds" so Resources.Load("Audio/UI Sounds") succeeds. The asset type must be compatible with UISoundCollection. - Null-safety: The class gracefully does nothing if the UISoundCollection cannot be found. For debugging, log a warning if m_SoundCollection is null to help find missing resources. - Binding framework integration: This class only creates the binding(s) and holds them as part of the CompositeBinding instance. To make the binding actionable you need to register this CompositeBinding instance with whatever binding/dispatcher the UI framework uses in your mod. - Threading: Unity's resources and audio APIs should be called from the main thread. This class assumes bindings are invoked on the UI/main thread. - Extending: If you need different sound collections or dynamic loading, consider subclassing or modifying the constructor to accept a UISoundCollection or a resource path parameter. }}