我几乎有一个工作的收音机,玩家可以走到它,按一个按钮并循环播放歌曲。除了每个按钮按下之外,一切都有效,按下新歌将播放,但原始歌曲将继续播放。对于播放的每首新歌曲,都会创建一个新对象,但它们都被称为“One Shot Audio”,#39;所以我不知道如何摧毁它们。如果我能解决这个问题,那么对于想要使用它的人来说,这应该是一个有用的无线电脚本。 更新,这是我修改过的无线电代码,现在正在工作,借助以下答案:
void OnTriggerEnter2D(Collider2D target) {
if (target.gameObject.tag == "radio") {
radioEnter = true;
}
}
void OnTriggerExit2D(Collider2D target) {
if (target.gameObject.tag == "radio") {
radioEnter = false;
}
}
public void radioUse(){
if ((Input.GetKeyDown (KeyCode.M)) && song3on == true && radioEnter == true) {
TurnOn ();
song1on = true;
song2on = false;
song3on = false;
}
else if ((Input.GetKeyDown (KeyCode.M)) && song1on == true && radioEnter == true) {
TurnOff ();
song1on = false;
song2on = true;
song3on = false;
TurnOn();
}
else if ((Input.GetKeyDown (KeyCode.M)) && song2on == true && radioEnter == true) {
TurnOff ();
song1on = false;
song2on = false;
song3on = true;
TurnOn ();
}
}
public void Update() {
raidoUse();
}
答案 0 :(得分:1)
由于PlayClipAtPoint未返回要管理的AudioSource,因此不应将其用于此类声音。我建议在阵列中使用一个AudioSource和多个AudioClip设置您的收音机。只需将所需剪辑拖动到检查器,然后使用公共方法控制收音机。这样您就可以将它重复用于不同的歌曲。
以下代码未经过测试。
Public class Radio : MonoBehaviour
{
AudioSource output;
public AudioClip[] songs;
int songIndex = 0;
void Start(){
output = gameObject.AddComponent<AudioSource>();
}
public void ToggleSong(){
songIndex++;
output.clip = songs[songIndex % songs.Length];
output.Play();
}
public void TurnOn(){
ToggleSong();
}
public void TurnOff(){
output.Stop();
}