Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检测用户界面按钮上的点击?_C#_Unity3d_Game Engine - Fatal编程技术网

C# 如何检测用户界面按钮上的点击?

C# 如何检测用户界面按钮上的点击?,c#,unity3d,game-engine,C#,Unity3d,Game Engine,我正在unity中制作一个钢琴游戏/应用程序,我希望能够录制玩家的笔记,然后回放,我找到了一种使用ArrayList的方法,但我通过按键激活录制,我想通过单击unity中的UI按钮来实现。我不知道该怎么做。我的另一个问题是,游戏在录制后不会播放音符,我希望能够在录制时播放音符,如果我没有录制,也可以播放音符。我的代码在下面,谢谢你的帮助 using System.Collections; using System.Collections.Generic; using UnityEngine; u

我正在unity中制作一个钢琴游戏/应用程序,我希望能够录制玩家的笔记,然后回放,我找到了一种使用ArrayList的方法,但我通过按键激活录制,我想通过单击unity中的UI按钮来实现。我不知道该怎么做。我的另一个问题是,游戏在录制后不会播放音符,我希望能够在录制时播放音符,如果我没有录制,也可以播放音符。我的代码在下面,谢谢你的帮助

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

public class NotePlay: MonoBehaviour
{


    Button record;
    public bool recordMode = 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;

    // Use this for initialization

    public ArrayList notes;

    void Start()
    {
        anim = gameObject.GetComponent<Animator>();
        audio = GetComponent<AudioSource>();
        notes = new ArrayList();

    }

    void Playback()
    {
        print(notes.Count);
        for (int i = 0; i < notes.Count; i++)
        {

            char c = (char)notes[i];
            System.Threading.Thread.Sleep(1000);
            PlayNote(c);

        }


    }

    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");

        }
        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);

        }
        if (note == 'd')
        {

            //anim.SetTrigger("D");
            GameObject.Find("Sphere_D").GetComponent<AudioSource>().PlayOneShot(noteD);

        }
        if (note == 'e')
        {

            //anim.SetTrigger("E");
            GameObject.Find("Sphere_E").GetComponent<AudioSource>().PlayOneShot(noteE);

        }
        else if (note == 'f')
        {

            // anim.SetTrigger("F");
            GameObject.Find("Sphere_F").GetComponent<AudioSource>().PlayOneShot(noteF);

        }
        else if (note == 'g')
        {

            //anim.SetTrigger("G");
            GameObject.Find("Sphere_G").GetComponent<AudioSource>().PlayOneShot(noteG);

        }


    }

    // Update is called once per frame
    void Update()
    {

        if (Input.GetKeyDown(KeyCode.R))
        {

            recordMode = !recordMode;


        }

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


        }
        else
        {

            if (Input.GetKeyDown(KeyCode.P))
            {

                Playback();

            }
        }

    }
    }
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共课堂笔记游戏:单一行为
{
按钮记录;
public bool recordMode=false;
动画师;
公共音频剪辑noteA;
公共音频剪辑记事本;
公共音频剪辑noteC;
注意到公共音频剪辑;
公共录音短片;
公共音频剪辑noteF;
公共音频剪辑记事本;
公共音频源音频;
//用于初始化
公共阵列列表注释;
void Start()
{
anim=gameObject.GetComponent();
audio=GetComponent();
notes=新的ArrayList();
}
无效播放()
{
打印(备注、计数);
for(int i=0;i
对于问题的第一部分,您需要在按钮中添加一个侦听器。将此添加到您的
Start()
方法:

record.OnClick.AddListener(()=>{ recordMode = !recordMode; });
这将使其在按下
记录
字段上的按钮时执行操作

对于问题的第二部分,您可以在每次记录笔记时调用
PlayNote()
,如下所示:

if (Input.GetKeyDown(KeyCode.G)){
 notes.Add('g');
 PlayNote('g');
}