Audio Manager
The Genies Dev Kit offers an audio management tools that can pool configured audio sources and can also play 2D and 3D audio one shots and loops.
Audio Prefabs
There are two audio management prefabs in the Dev Kit. In the Project window, open the Assets > GeniesSdk > Prefabs > Audio Prefabs folder.
Audio Manager Prefab
The AudioManager prefab pools all 2D and 3D audio sources. It is required to have one of these prefabs in the scene to start using the audio management tools.
3D Audio Node Prefab
The 3D Audio Node prefab is an Audio Source game object. The main difference between this prefab and the default Audio Source is the Spatial Blend property is set to 1
which is the configuration required for 3D sounds.
Audio Source is a type of game object in Unity that has a component for sourcing audio clips and settings for how to play them. You can find it in the Hierarchy create menu under Audio > Audio Source.
Configuring a Scene
Here are the steps to configure a scene with the Audio Manager component:
- Add Audio Sources dedicated to 2D sounds (default Audio Source)
- Add Audio Sources dedicated to 3D sounds (3D Audio Node prefab)
- Add an Audio Manager prefab
- Add the newly created Audio Sources to the Audio Manager component
Make sure you have an Audio Listener component in the scene. This is by default added to the Main Camera game object.
Play On Frame Component
The PlayOnFrame component allows you to play sounds on a given frame of an AnimationClip.
How to Use
Add Component
The first step is to add an instance of PlayOnFrame to a GameObject that has an Animator component or on a GameObject that is a child of another GameObject that has an Animator component.
Disable Component
Make sure your new PlayOnFrame component is disabled by clicking the checkmark at the top left of the component box.
As when enabled, it will trigger the configured SoundEvent.
Trigger from Animation
To trigger your configured SoundEvent from your new PlayOnFrame component, enable it on the Animation window timeline of the given AnimationClip.
TypeScript Usage
Package
To use the Audio Manager API, it is required to import the Audio Manager class like so:
import { AudioManager } from "RootNamespace";
Methods
Here are a list of methods available in the Audio Manager class:
//returns a boolean
IsPlayingAudio(clip: AudioClip);
//plays specific audio clips
PlayLoopedAudio(clip: AudioClip);
PlayLoopedAudioAtWorldLocation(newLocation: Vector3, clip: AudioClip);
PlayOneShot(clip: AudioClip);
PlayOneShotAtWorldLocation(newLocation: Vector3, clip: AudioClip);
//stops specific audio clips
StopAudio(clip: AudioClip);
StopLoopingAudio(clip: AudioClip);
StopAllAudio();
StopAllLoopingAudio();
StopAllNonLoopingAudio();
Example
Here is an example script to play a 2D intro music audio source and a 3D audio source for player footsteps:
import { AudioManager } from "RootNamespace";
import { AudioClip, GameObject, MonoBehaviour, Rigidbody } from "UnityEngine";
export default class MyScript extends MonoBehaviour {
@SerializeField private myAudio: AudioManager;
@SerializeField private introClip: AudioClip;
@SerializeField private footstepClip: AudioClip;
@SerializeField private player: GameObject;
private Start() : void {
if(this.myAudio.IsPlayingAudio(this.introClip) == false) {
this.myAudio.PlayLoopedAudio(this.introClip);
}
}
private Update() : void {
if(this.player.GetComponent<Rigidbody>().velocity.magnitude > 0) {
this.myAudio.PlayOneShotAtWorldLocation(
this.player.transform.position,
this.footstepClip
);
}
}
}