Click or drag to resize

UISoundPlayer Class

Helper class to play sounds in the game GUI. Such sounds are not 3D aligned.
Inheritance Hierarchy
SystemObject
  UnityEngineObject
    UnityEngineComponent
      UnityEngineBehaviour
        UnityEngineMonoBehaviour
          KSPDev.GUIUtilsUISoundPlayer

Namespace:  KSPDev.GUIUtils
Assembly:  KSPDev_Utils.2.5 (in KSPDev_Utils.2.5.dll) Version: 2.5 for KSP v1
Syntax
C#
public sealed class UISoundPlayer : MonoBehaviour
Request Example View Source

The UISoundPlayer type exposes the following members.

Constructors
  NameDescription
Public methodUISoundPlayer
Initializes a new instance of the UISoundPlayer class
Top
Properties
  NameDescription
Public propertyStatic memberinstance
Returns the instance of the player in the current scene.
Top
Methods
  NameDescription
Public methodCode exampleCacheSound
Loads the sound into cache but doesn't play it.
Public methodCode examplePlay
Plays the specified sound.
Top
Remarks

Use this player when the source of the sound is a GUI object (e.g. a GUI control). This class implements all the boilerplate to load and play the sound resources. All the sounds are cached within the scene, so repeating requests to the same sound won't add extra latency.

If latency is ciritcal for the caller, then the sound resource can be pre-cached via the CacheSound(String) method. It will increase the scene loading time, though.

This module is initialized from the KSPDev Utils loader.

Examples
public class UISoundPlayerDemo1 : PartModule {
  public override void OnAwake() {
    // To not loose the latency on the sound play pre-cache it.
    UISoundPlayer.instance.CacheSound("ooo.ogg");
  }

  public override void OnUpdate() {
    if (Input.GetKeyDown("O")) {
       UISoundPlayer.instance.Play("ooo.ogg");  // Played from cache. No delay.
    }
    if (Input.GetKeyDown("P")) {
      UISoundPlayer.instance.Play("ppp.ogg");  // May delay the game while loading the resource.
    }
  }
}
See Also