如何在场景之间淡出 audio/music?
How to fade out audio/music between scenes?
我正在尝试让一些音乐在我开始 android 游戏时淡出。音乐在主菜单中播放,然后在玩家点击播放时淡出。我可以让音乐停止,只是不会淡出。
我试图用这个淡出:
using UnityEngine;
using System.Collections;
public class MusicScript : MonoBehaviour
{
static MusicScript instance;
bool doneFading;
void Start()
{
if(instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else if (instance != this)
{
Destroy(gameObject);
}
}
void Update()
{
if (Application.loadedLevelName == "Flappy Drone Gameplay Screen")
{
FadeMusic();
if (doneFading == true)
{
Destroy(gameObject);
}
}
}
IEnumerator FadeMusic()
{
for (int i = 9; i > 0; i--)
{
Debug.Log("here");
instance.audio.volume = i * 0.1f;
yield return new WaitForSeconds(0.5f);
Debug.Log("lowered volume");
}
doneFading = true;
}
}
感谢任何帮助!
经过一番尝试 Time.deltatime 我发现了一种简单的渐变方法 :)
适用于遇到相同问题的任何人的代码(此代码在特定场景中淡出音乐,但在所有其他场景中保持音乐播放):
using UnityEngine;
using System.Collections;
public class MusicScript : MonoBehaviour
{
static MusicScript instance;
bool doneFading;
float timer = 5f;
void Start()
{
if(instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else if (instance != this)
{
Destroy(gameObject);
}
}
void Update()
{
if (Application.loadedLevelName == "Flappy Drone Gameplay Screen")
{
FadeMusic();
if (doneFading == true)
{
Destroy(gameObject);
Debug.Log ("Music destroyed.");
}
}
}
void FadeMusic()
{
if (timer > 0)
{
instance.audio.volume -= 0.015f;
timer -= Time.deltaTime;
}
if (instance.audio.volume == 0)
{
doneFading = true;
}
}
}
我正在尝试让一些音乐在我开始 android 游戏时淡出。音乐在主菜单中播放,然后在玩家点击播放时淡出。我可以让音乐停止,只是不会淡出。
我试图用这个淡出:
using UnityEngine;
using System.Collections;
public class MusicScript : MonoBehaviour
{
static MusicScript instance;
bool doneFading;
void Start()
{
if(instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else if (instance != this)
{
Destroy(gameObject);
}
}
void Update()
{
if (Application.loadedLevelName == "Flappy Drone Gameplay Screen")
{
FadeMusic();
if (doneFading == true)
{
Destroy(gameObject);
}
}
}
IEnumerator FadeMusic()
{
for (int i = 9; i > 0; i--)
{
Debug.Log("here");
instance.audio.volume = i * 0.1f;
yield return new WaitForSeconds(0.5f);
Debug.Log("lowered volume");
}
doneFading = true;
}
}
感谢任何帮助!
经过一番尝试 Time.deltatime 我发现了一种简单的渐变方法 :)
适用于遇到相同问题的任何人的代码(此代码在特定场景中淡出音乐,但在所有其他场景中保持音乐播放):
using UnityEngine;
using System.Collections;
public class MusicScript : MonoBehaviour
{
static MusicScript instance;
bool doneFading;
float timer = 5f;
void Start()
{
if(instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else if (instance != this)
{
Destroy(gameObject);
}
}
void Update()
{
if (Application.loadedLevelName == "Flappy Drone Gameplay Screen")
{
FadeMusic();
if (doneFading == true)
{
Destroy(gameObject);
Debug.Log ("Music destroyed.");
}
}
}
void FadeMusic()
{
if (timer > 0)
{
instance.audio.volume -= 0.015f;
timer -= Time.deltaTime;
}
if (instance.audio.volume == 0)
{
doneFading = true;
}
}
}