Non-spatialized audio
Non-spatialized audio sounds the same throughout the scene, regardless of the position of entities (such as the player camera). It's stereo and moves along a single axis (usually the X-axis). Unlike , the volume, pitch (frequency), and other parameters of spatialized audio don't change. This is useful, for example, for background music and menu sound effects.
Non-spatialized audio requires no audio emitters or .
Make sure the audio asset is a root asset. Root assets are assets that Xenko includes in the build so they can be used at runtime.
In the Asset View, right-click the asset and select Include in build as root asset:
To play non-spatialized audio at runtime, create an instance of it and define its behavior in the code.
The controls audio at runtime with the following properties:
For more details, see the SoundInstance API documentation.
Note
If the sound is already playing, Xenko ignores all additional calls to SoundInstance.Play.The same goes for (when a sound is already paused) and SoundInstance.Stop (when a sound is already stopped).
For example, the following code:
- instantiates non-spatialized audio
- sets the audio to loop
- sets the volume
- plays the audio
Create a public variable for each audio asset you want to use. You can use the same properties listed above.
For example:
{
private SoundInstance musicInstance;
public bool PlayMusic;
public override void Start()
{
}
public override void Update()
{
// If music isn't playing but should be, play the music.
if (PlayMusic & musicInstance.PlayState != SoundPlayState.Playing)
{
// If music is playing but shouldn't be, stop the music.
else if (!PlayMusic)
{
musicInstance.Stop();
}
}
}
- In the Scene view, select the entity you want to add the script to:
- In the Property Grid, click Add component and select your script:
The script is added to the entity.
If you added public variables to the script, you need to tie them to audio assets.
Drag and drop an asset from the Asset View to each variable:
Alternatively, click (Select an asset):