我正在尝试在我的项目中选择一首歌。每次你点击游戏中的一个按钮,它就会发送int给这个函数。有没有更好的方法来构建这个代码,所以我不必为每首歌编写IF语句?
public void SongPick(int song)
{
if (song == 0)
{
audioSource.clip = musicArray[0];
}
if (song == 1)
{
audioSource.clip = musicArray[1];
}
if (song == 2)
{
audioSource.clip = musicArray[2];
}
audioSource.Play();
string currentMusic = musicArray[song].name;
songName.text = "PLAYING: " + currentMusic;
}
答案 0 :(得分:0)
假设song
始终引用musicArray
中的正确索引,您绝对不需要一个不断增长的if语句。
public void SongPick(int song) {
if (song < musicArray.length) {
audioSource.clip = musicArray[song];
}
audioSource.Play();
string currentMusic = musicArray[song].name;
songName.text = "PLAYING: " + currentMusic;
}