我正在使用Vuforia和Unity 4.6开发增强现实钢琴。该应用程序将是一个游戏,手机会说一个音符,例如:“C”,用户将有5秒钟在带有虚拟按钮的纸钢琴中弹奏音符。
我遇到的问题是,当我按下按钮时,我没有听到钢琴键的声音。我在switch语句中输入了所需的大小写,但没有播放声音。更新函数或OnButtonPressed()上的play()方法都在播放。但是关于方法generarNota()。我总是听着电话里的声音说出来。
这是我的代码:
public class ButtonControl : MonoBehaviour,IVirtualButtonEventHandler {
public AudioSource c;
public AudioSource cSharp;
public AudioSource d;
public AudioSource dSharp;
public AudioSource e;
public AudioSource f;
public AudioSource fSharp;
public AudioSource g;
public AudioSource gSharp;
public AudioSource a;
public AudioSource aSharp;
public AudioSource b;
public AudioSource correctSound;
public AudioSource incorrectSound;
public AudioClip cv;
public AudioClip cSharpv;
public AudioClip dv;
public AudioClip dSharpv;
public AudioClip ev;
public AudioClip fv;
public AudioClip fSharpv;
public AudioClip gv;
public AudioClip gSharpv;
public AudioClip av;
public AudioClip aSharpv;
public AudioClip bv;
public AudioSource c2;
public bool gameOn;
string currentNote; //Nota genereada por el sistema
string inputNote; //Nota presionada por el usuario
public float waitTime; //Tiempo de espera entre notas
public float resetWait; // Para reiniciar el tiempo de espera
public int totalNotes;
public int correctNotes;
public int incorrectNotes;
bool firstNote;
bool beingHandled;
public bool blackNotes;
bool notePressed;
//Use this for initialization
void Start () {
c.PlayOneShot(c.clip);
blackNotes = false;
this.notePressed = false;
beingHandled = false;
currentNote="NONE";
waitTime = 0;
firstNote = true;
VirtualButtonBehaviour[] vbs = GetComponentsInChildren<VirtualButtonBehaviour>();
for (int i = 0; i < vbs.Length; ++i) {
// Register with the virtual buttons TrackableBehaviour
vbs[i].RegisterEventHandler(this);
}
}
void generarNota(){
if (blackNotes) {
int num = Random.Range (1, 13);
//StartCoroutine ("waitTimeDelay");
switch (num) {
case 1:
Debug.Log ("Se genero C");
int a = 0;
currentNote = "C";
audio.clip = this.cv;
audio.PlayDelayed (0.5f);
//this.aSource.PlayOneShot(this.cv);
break;
case 2:
Debug.Log ("Se genero C#");
currentNote = "C#";
audio.clip = this.cSharpv;
audio.PlayDelayed (0.5f);
break;
case 3:
Debug.Log ("Se genero D");
currentNote = "D";
audio.clip = this.dv;
audio.PlayDelayed (0.5f);
break;
case 4:
Debug.Log ("Se genero D#");
currentNote = "D#";
audio.clip = this.dSharpv;
audio.PlayDelayed (0.5f);
break;
case 5:
Debug.Log ("Se genero E");
currentNote = "E";
audio.clip = this.ev;
audio.PlayDelayed (0.5f);
break;
case 6:
Debug.Log ("Se genero F");
currentNote = "F";
audio.clip = this.fv;
audio.PlayDelayed (0.5f);
break;
case 7:
Debug.Log ("Se genero F#");
currentNote = "F#";
audio.clip = this.fSharpv;
audio.PlayDelayed (0.5f);
break;
case 8:
Debug.Log ("Se genero G");
currentNote = "G";
audio.clip = this.gv;
audio.PlayDelayed (0.5f);
break;
case 9:
Debug.Log ("Se genero G#");
currentNote = "G#";
audio.clip = this.gSharpv;
audio.PlayDelayed (0.5f);
break;
case 10:
Debug.Log ("Se genero A");
currentNote = "A";
audio.clip = this.av;
audio.PlayDelayed (0.5f);
break;
case 11:
Debug.Log ("Se genero A#");
currentNote = "A#";
audio.clip = this.aSharpv;
audio.PlayDelayed (0.5f);
break;
case 12:
Debug.Log ("Se genero B");
currentNote = "B";
audio.clip = this.bv;
audio.PlayDelayed (0.5f);
break;
}
this.totalNotes++;
}
else {
int num = Random.Range (1, 8);
//StartCoroutine ("waitTimeDelay");
switch (num) {
case 1:
Debug.Log ("Se genero C");
int a = 0;
currentNote = "C";
audio.clip = this.cv;
audio.PlayDelayed (0.5f);
//this.aSource.PlayOneShot(this.cv);
break;
case 2:
Debug.Log ("Se genero D");
currentNote = "D";
audio.clip = this.dv;
audio.PlayDelayed (0.5f);
break;
case 3:
Debug.Log ("Se genero E");
currentNote = "E";
audio.clip = this.ev;
audio.PlayDelayed (0.5f);
break;
case 4:
Debug.Log ("Se genero F");
currentNote = "F";
audio.clip = this.fv;
audio.PlayDelayed (0.5f);
break;
case 5:
Debug.Log ("Se genero G");
currentNote = "G";
audio.clip = this.gv;
audio.PlayDelayed (0.5f);
break;
case 6:
Debug.Log ("Se genero A");
currentNote = "A";
audio.clip = this.av;
audio.PlayDelayed (0.5f);
break;
case 7:
Debug.Log ("Se genero B");
currentNote = "B";
audio.clip = this.bv;
audio.PlayDelayed (0.5f);
break;
}
this.totalNotes++;
}
}
void waitTimeDelay(){
}
// Update is called once per frame
void Update () {
if (this.gameOn) {
if(this.waitTime>0){
this.waitTime-= Time.deltaTime;
//Verificamos el boton. Si esta presionado el correcto sumamos a las notas correctas
if(inputNote==this.currentNote){
//audio.clip = this.correctSound;
//audio.Play();
correctSound.Play();
this.correctNotes++;
inputNote ="NONE.";
this.waitTime = resetWait;
this.generarNota();
}
else if(inputNote!= "NONE." && inputNote != this.currentNote){
//Si la nota es incorrecta, agregamos uno a las incorrectas y generamos una nueva nota
//audio.clip = this.incorrectSound;
//audio.Play();
incorrectSound.Play();
this.incorrectNotes++;
inputNote ="NONE.";
this.generarNota();
this.waitTime = resetWait;
}
//Generamos nueva nota
//Reiniciamos
}
else{
//Reiniciamos el tiempo
this.waitTime = this.resetWait;
if(!this.firstNote){
// Como no presiono nada, nota erronea
this.incorrectNotes++;
incorrectSound.Play();
//audio.clip = this.incorrectSound;
//audio.Play();
//this.aSource.PlayOneShot(this.incorrectSound);
}
else{
this.firstNote = false;
}
//generamos una nueva nota
this.generarNota();
}
}
}
public void OnButtonPressed(VirtualButtonAbstractBehaviour vb){
if (blackNotes&&!notePressed) {
switch (vb.VirtualButtonName) {
case "buttonC":
this.inputNote = "C";
//audio.PlayOneShot (c);
break;
case "buttonCSharp":
this.inputNote = "C#";
//audio.PlayOneShot (cSharp);
break;
case "buttonD":
this.inputNote = "D";
//audio.PlayOneShot (d);
break;
case "buttonDSharp":
this.inputNote = "D#";
//audio.PlayOneShot (dSharp);
break;
case "buttonE":
this.inputNote = "E";
//audio.PlayOneShot (e);
break;
case "buttonF":
this.inputNote = "F";
//audio.PlayOneShot (f);
break;
case "buttonFSharp":
this.inputNote = "F#";
//audio.PlayOneShot (fSharp);
break;
case "buttonG":
this.inputNote = "G";
//audio.PlayOneShot (g);
break;
case "buttonGSharp":
this.inputNote = "G#";
//audio.PlayOneShot (gSharp);
break;
case "buttonA":
this.inputNote = "A";
//audio.PlayOneShot (a);
break;
case "buttonASharp":
this.inputNote = "A#";
//audio.PlayOneShot (aSharp);
break;
case "buttonB":
this.inputNote = "B";
//audio.PlayOneShot (b);
break;
case "buttonC2":
this.inputNote = "C";
//audio.PlayOneShot (c2);
break;
}
notePressed=true;
}
else if(blackNotes==false && notePressed==false){
switch (vb.VirtualButtonName) {
case "buttonC":
this.inputNote = "C";
Debug.Log ("Se Presiono C");
c.Play();
break;
case "buttonD":
this.inputNote = "D";
d.Play();
Debug.Log ("Se Presiono D");
break;
case "buttonE":
this.inputNote = "E";
e.Play();
Debug.Log ("Se Presiono E");
break;
case "buttonF":
this.inputNote = "F";
f.Play();
Debug.Log ("Se Presiono F");
break;
case "buttonG":
this.inputNote = "G";
g.Play();
Debug.Log ("Se Presiono G");
break;
case "buttonA":
this.inputNote = "A";
a.Play();
Debug.Log ("Se Presiono A");
break;
case "buttonB":
this.inputNote = "B";
b.Play();
Debug.Log ("Se Presiono B");
break;
case "buttonC2":
this.inputNote = "C";
c2.Play();
Debug.Log ("Se Presiono C2");
break;
}
notePressed=true;
}
}
//If para cada tecla del piano
//audio.PlayOneShot(myClip);
//Comparamos si las notas son iguales.
//Si son iguales, sonido correcto+generar una nueva nota+sumar contador de puntos
//Si no son iguales--> sonido de error+ terminar juego
public void OnButtonReleased(VirtualButtonAbstractBehaviour vb){
notePressed = false;
this.inputNote = "NONE.";
Debug.Log ("BUTTON UNPRESSED");
}
}
这也是我的编辑器截图,因此您可以检查我是否有附加到每个音频源的音频剪辑。
有谁能帮我找到问题所在?
答案 0 :(得分:3)
创建一个数组,而不是多个AudioSource。 2关于代码结构的主要问题。
1 - 仅适用于使用可以使用多个分接头的移动设备。你没有使用Touches
2 - 因为你没有使用Array。您总是必须使用新的音频源删除当前正在播放的音频源,而不是从AudioSource阵列进行迭代。 audio.clip将始终删除正在播放的当前播放。
AudioSource[] myNotes;
myNotes[0].PlayOnce();
myNotes[3].PlayOnce();
//Don't do this.
// audio.clip = <---- NO!!!