The script below demonstrates the basic steps required to synchronize game events with NSLSong information.
NSLSongManager.allEvents += **YourEventHandler**; subscribes the **YourEventHandler** handler to the allEvents event. allEvents sends all track information as a set of arrays (theVolume[] contains a float for each track, thePitch[] contains a float for each track, fireClip[] is set true if a track is firing a sample this beat, stopClip[] is set true is a track is stopping playback this beat).
Each array has a length corresponding to the max number of tracks present in any song in the playlist. In the example below, the light will flash when Audio Track 2 plays (array index 0 = Track 1, 1 = Track 2, etc).
public class LightFlasher : MonoBehaviour {
Light theLight;
Color lightColor = Color.white;
//Sets which audio track the light will respond to
public int songTrack = 1;
void Awake () {
theLight = gameObject.GetComponent<Light> ();
//Subscribe to allEvents
NSLSongManager.allEvents += HandleLightFlash;
}
//When a step event is recieved, increase light brightness and adjust color based on pitch
void HandleLightFlash(float[] theVolume, float[] thePitch, bool[] fireClip, bool[] stopClip)
{
if (fireClip[songTrack])
{
theLight.intensity = theVolume [songTrack] * 3;
lightColor.b = 1.0f – (thePitch [songTrack] / 2);
lightColor.g = 0.0f + (thePitch [songTrack] / 2);
theLight.color = lightColor;
}
}
void Update () {
//Fade Light
if (light.intensity > 0)
{
theLight.intensity -= 0.05f;
}
}
}