团结钢琴游戏与回放录制的音符有关

时间:2018-05-11 00:37:27

标签: unity3d game-engine piano

我有一个统一的钢琴应用程序,我使用ArrayList添加注释然后回放它主要工作但是回放方法,因为它在playMode为真时激活,它继续循环,我需要知道我如何使用if语句检查是否已经播放了所有录制的音符,如果有,则循环应该中断。我真的需要帮助。

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class NotePlay: MonoBehaviour
{


public Button record, play;

public bool recordMode = false;
public bool playMode = false;
Animator anim;
public AudioClip noteA;
public AudioClip noteB;
public AudioClip noteC;
public AudioClip noteD;
public AudioClip noteE;
public AudioClip noteF;
public AudioClip noteG;
public AudioSource audio;
public int count = 0;
// Use this for initialization

public ArrayList notes;

void Start()
{
    anim = gameObject.GetComponent<Animator>();
    audio = GetComponent<AudioSource>();
    notes = new ArrayList();
    Button rec = record.GetComponent<Button>();
    Button pl = play.GetComponent<Button>();
    record.onClick.AddListener(()=>{ recordMode = !recordMode; });
    play.onClick.AddListener(() => { playMode = !playMode; recordMode = 
  false; });


}

void Playback()
{
    bool flag = true;
    for (int i = 0; i < notes.Count && flag; i++)
    {


        char c = (char)notes[i];

        System.Threading.Thread.Sleep(1000);
        PlayNote(c);
        print(c);

         // i want the recorded notes to only play once 
        if ()
        {



        }

    }


}

void PlayNote(char note)
{

    if (note == 'a')
    {

        //anim.SetTrigger("A");
        GameObject.Find("Sphere_A").GetComponent<AudioSource> 
    ().PlayOneShot(noteA);
        GameObject.Find("Sphere_A").GetComponent<Animator> 
    ().SetTrigger("A");
        print("a");
    }
    if (note == 'b')
    {

        //anim.SetTrigger("B");
        GameObject.Find("Sphere_B").GetComponent<AudioSource> 
   ().PlayOneShot(noteB);
        GameObject.Find("Sphere_B").GetComponent<Animator> 
   ().SetTrigger("B");
        print("b");

    }
    if (note == 'c')
    {

        ///anim.SetTrigger("C");
         GameObject.Find("Sphere_C").GetComponent<AudioSource> 
    ().PlayOneShot(noteC);
            GameObject.Find("Sphere_C").GetComponent<Animator> 
   ().SetTrigger("C");
    }
    if (note == 'd')
    {

        //anim.SetTrigger("D");
        GameObject.Find("Sphere_D").GetComponent<AudioSource> 
   ().PlayOneShot(noteD);
        GameObject.Find("Sphere_D").GetComponent<Animator> 
   ().SetTrigger("D");
    }
    if (note == 'e')
    {

        //anim.SetTrigger("E");
        GameObject.Find("Sphere_E").GetComponent<AudioSource> 
   ().PlayOneShot(noteE);
        GameObject.Find("Sphere_E").GetComponent<Animator> 
   ().SetTrigger("E");
    }
    else if (note == 'f')
    {

        // anim.SetTrigger("F");
        GameObject.Find("Sphere_F").GetComponent<AudioSource> 
   ().PlayOneShot(noteF);
         GameObject.Find("Sphere_F").GetComponent<Animator> 
   ().SetTrigger("F");
    }
    else if (note == 'g')
    {

        //anim.SetTrigger("G");
        GameObject.Find("Sphere_G").GetComponent<AudioSource> 
   ().PlayOneShot(noteG);
        GameObject.Find("Sphere_G").GetComponent<Animator> 
   ().SetTrigger("G");
    }
}
// Update is called once per frame
void Update()
{



    if (recordMode == true)
    {
        if (Input.GetKeyDown(KeyCode.A)) {notes.Add('a'); PlayNote('a'); }
        if (Input.GetKeyDown(KeyCode.B)) {notes.Add('b'); PlayNote('b'); }
        if (Input.GetKeyDown(KeyCode.C)) {notes.Add('c'); PlayNote('c'); }
        if (Input.GetKeyDown(KeyCode.D)) {notes.Add('d'); PlayNote('d'); }
        if (Input.GetKeyDown(KeyCode.E)) {notes.Add('e'); PlayNote('e'); }
        if (Input.GetKeyDown(KeyCode.F)) {notes.Add('f'); PlayNote('f'); }
        if (Input.GetKeyDown(KeyCode.G)) {notes.Add('g'); PlayNote('g'); }         
    }
    else
    {
        if (Input.GetKeyDown(KeyCode.A)) {  PlayNote('a'); }
        if (Input.GetKeyDown(KeyCode.B)) {  PlayNote('b'); }
        if (Input.GetKeyDown(KeyCode.C)) {  PlayNote('c'); }
        if (Input.GetKeyDown(KeyCode.D)) {  PlayNote('d'); }
        if (Input.GetKeyDown(KeyCode.E)) {  PlayNote('e'); }
        if (Input.GetKeyDown(KeyCode.F)) {  PlayNote('f'); }
        if (Input.GetKeyDown(KeyCode.G)) {  PlayNote('g'); }
    }
    if (playMode == true )
    {
        Playback();
    }

}

}

1 个答案:

答案 0 :(得分:0)

您可能需要指定AudioSource不必循环播放声音。将此行添加到Start方法:

void Start()
{
    anim = gameObject.GetComponent<Animator>();
    audio = GetComponent<AudioSource>();
    audio.loop = false;        // <--------------- HERE
    notes = new ArrayList();
    Button rec = record.GetComponent<Button>();
    Button pl = play.GetComponent<Button>();
    record.onClick.AddListener(()=>{ recordMode = !recordMode; });
    play.onClick.AddListener(() => { playMode = !playMode; recordMode = false; });    
}

您可以在上面的代码中执行此操作,也可以直接在AudioSource的检查器中执行此操作。我认为loop属性的默认值设置为true,这就是它播放音符时循环的原因。