Unity无法同步节奏游戏的音频和视觉效果



我正试图在Unity中制作一款节奏游戏,但在同步游戏的音频和视觉元素时遇到了问题。你应该点击节拍的对象在歌曲开始时与音乐完美同步,但在歌曲结束时,它们落后于歌曲大约半个节拍。我正在使用AudioSettings.dspTime来安排我所有对象的生成,这是建议的,所以我不知道出了什么问题。以下是我用来生成对象的代码:

//void Update() {
//...
if (playing && !finished)
{
string[] nextNote = notes[currentNote];
float noteBeat = float.Parse(nextNote[0]);
int notePos = int.Parse(nextNote[1]);
float currentTime = (float)AudioSettings.dspTime - startTime - offset + approachTime;
float currentBeats = currentTime / (60 / bpm);
while (currentBeats - (noteBeat - 1) >= 0 && !finished)
{
if (nextNote.Length == 2)
{
GameObject go = Instantiate(noteObject, noteField.transform);
go.GetComponent<RectTransform>().anchoredPosition = new Vector2(Mathf.Sign(notePos) * 30 + (notePos - Mathf.Sign(notePos)) * 62.5f, 0);
go.transform.SetAsFirstSibling();
go.GetComponent<Note>().SetLane(notePos);
}
else
{
GameObject go = Instantiate(lyricObject, lyricField.transform.GetChild(notePos - 1));
go.GetComponent<Lyric>().SetLyric(nextNote[2], notePos);
}
if (currentNote < notes.Length - 1)
{
nextNote = notes[++currentNote];
noteBeat = float.Parse(nextNote[0]);
notePos = int.Parse(nextNote[1]);
}
else
{
finished = true;
playing = false;
Invoke("EndSong", startDelay + approachTime);
}
}
}

如果我应该澄清代码中的任何内容或添加更多内容,请告诉我。提前感谢!

无论如何,我解决了自己的问题。事实证明,我使用的歌曲并没有以恒定的BPM播放。

最新更新