Commit d08aaab3 authored by Tommé Nöll's avatar Tommé Nöll

added fade possibility to backgroundmusic.cs

parent 16d68473
...@@ -14,6 +14,7 @@ public class BackgroundMusic : MonoBehaviour { ...@@ -14,6 +14,7 @@ public class BackgroundMusic : MonoBehaviour {
private bool inTransition = false; private bool inTransition = false;
private int preTransitionSample = 0; private int preTransitionSample = 0;
private double lastUpdate = 0; private double lastUpdate = 0;
private float fadePerSecond = -1f;
// Use this for initialization // Use this for initialization
void Start () { void Start () {
...@@ -34,10 +35,18 @@ public class BackgroundMusic : MonoBehaviour { ...@@ -34,10 +35,18 @@ public class BackgroundMusic : MonoBehaviour {
if (AS[current].timeSamples != preTransitionSample) if (AS[current].timeSamples != preTransitionSample)
{ {
inTransition = false; inTransition = false;
AS[other].volume = 1;
fadePerSecond = -1;
Debug.Log("Music transition complete, playing " + AS[current].clip.name + Debug.Log("Music transition complete, playing " + AS[current].clip.name +
"\nSample new: " + AS[current].timeSamples + ", Sample old: " + AS[1-current].timeSamples + "\nSample new: " + AS[current].timeSamples + ", Sample old: " + AS[1-current].timeSamples +
"\nCurrent global time: " + AudioSettings.dspTime + ", Last update at " + lastUpdate); "\nCurrent global time: " + AudioSettings.dspTime + ", Last update at " + lastUpdate);
} }
if (fadePerSecond >= 0 && AS[1 - current].isPlaying)
{
AS[1 - current].volume -= fadePerSecond * Time.deltaTime;
}
/*else /*else
{ {
lastUpdate = AudioSettings.dspTime; lastUpdate = AudioSettings.dspTime;
...@@ -72,7 +81,7 @@ public class BackgroundMusic : MonoBehaviour { ...@@ -72,7 +81,7 @@ public class BackgroundMusic : MonoBehaviour {
"\nCurrent Global Time: " + AudioSettings.dspTime); "\nCurrent Global Time: " + AudioSettings.dspTime);
} }
public void TransitionTo(MusicObject target) public void TransitionTo(MusicObject target, float fadeDuration = -1)
{ {
if (inTransition) if (inTransition)
{ {
...@@ -86,6 +95,12 @@ public class BackgroundMusic : MonoBehaviour { ...@@ -86,6 +95,12 @@ public class BackgroundMusic : MonoBehaviour {
} }
inTransition = true; inTransition = true;
if (fadeDuration >= 0)
{
fadePerSecond = 1 / fadeDuration;
Play(target, AS[current].clip.samples + (int)(AS[current].clip.frequency * fadeDuration), 0);
}
int iTarget; int iTarget;
for (iTarget = 0; iTarget < currentMusic.targets.Length; iTarget++) //search for TransitionTarget that matches target for (iTarget = 0; iTarget < currentMusic.targets.Length; iTarget++) //search for TransitionTarget that matches target
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment