如何用键盘控制Unity UI按钮?

问题描述 投票:0回答:1

我在测验应用程序中有五个 UI 按钮 A、B、C、D 和 E,我想为其分配键盘上的不同键,例如 1、2、3、4 和 5。

OnClick
方法是用名为 QuizUI 的单独脚本编写的,并且它不能是静态的。但我不断收到“非静态字段、方法或属性需要对象引用”的错误消息。我应该写什么呢?

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using Microsoft.MixedReality.Toolkit.Input;
using Microsoft.MixedReality.Toolkit.UI;

public class Key : MonoBehaviour
{
    private GameObject A;

    private void Update()
    {      
        if (Input.GetKeyDown("1"))
        {
            QuizUI.OnClick(A);
        }
    }
}

QuizUI 代码如下所示:

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using Microsoft.MixedReality.Toolkit.Input;
using Microsoft.MixedReality.Toolkit.UI;

public class QuizUI : MonoBehaviour
{
    [SerializeField] private QuizManager quizManager;
    [SerializeField] private GameObject startButton;
    [SerializeField] private GameObject imageHolder;
    [SerializeField] private Image questionImage;
    [SerializeField] private GameObject questionModel;
    [SerializeField] private GameObject optionHolder;
    [SerializeField] public List<GameObject> options;
    [SerializeField] private GameObject endInfo;

    private Question question;
    private bool answered = false;
    private GameObject clone;
    public Stopwatch stopwatch;

    private void Awake()
    {
        imageHolder.gameObject.SetActive(false);
        optionHolder.gameObject.SetActive(false);
        endInfo.gameObject.SetActive(false);
    }

    private void OnEnable()
    {
        var interactable = startButton.GetComponent<Interactable>();
        interactable.OnClick.AddListener(() => Prepare());
    }

    private void OnDisable()
    {
        var interactable = startButton.GetComponent<Interactable>();
        interactable.OnClick.RemoveListener(() => Prepare());
    }

    private void Prepare()
    {
        startButton.gameObject.SetActive(false);
        imageHolder.gameObject.SetActive(true);
        optionHolder.gameObject.SetActive(true);
        
        //add the listner to all the buttons
        for (int i = 0; i < options.Count; i++)
        {
            GameObject localBtn = options[i];
            var interactable = options[i].GetComponent<Interactable>();
            interactable.OnClick.AddListener(() => OnClick(localBtn));
        }
        
        quizManager.StartGame();
    }

    

    public void SetQuestion(Question question)
    {
        //set the question
        this.question = question;
        //check for questionType
        switch (question.questionType)
        {
            case QuestionType.IMAGE:
                questionImage.transform.parent.gameObject.SetActive(true);
                questionImage.transform.gameObject.SetActive(true);    //activate image holder
                questionModel.gameObject.SetActive(false);
                questionImage.sprite = question.questionImg;           //set the image sprite
                break;

            case QuestionType.MODEL:
                questionImage.transform.parent.gameObject.SetActive(true);
                questionImage.transform.gameObject.SetActive(false);
                questionModel.gameObject.SetActive(true);
                clone = Instantiate(question.questionMdl, new Vector3(0, 0, 0.8f), Quaternion.Euler (0f, -90f, 0f)) as GameObject;
                clone.transform.SetParent(questionModel.transform);
                break;
        }

        //assign options to respective option buttons
        for (int i = 0; i < options.Count; i++)
        {
            options[i].transform.GetChild(0).GetComponentInChildren<TextMesh>().text = question.options[i];
        }

        answered = false;

        //stopwatch to start
        stopwatch = new Stopwatch();
        stopwatch.Start();
    }

    public void OnClick(GameObject btn)
    {
        if (!answered)
        {
            answered = true;
            bool val = quizManager.Answer(btn.name);
            UnityEngine.Debug.Log(val);

            DestroyImmediate(clone, true);
            stopwatch.Stop();   //stopwatch to stop
            UnityEngine.Debug.Log(stopwatch.Elapsed);
        }
    }

    public void EndGame()
    {
        imageHolder.gameObject.SetActive(false);
        optionHolder.gameObject.SetActive(false);
        endInfo.gameObject.SetActive(true);
    }
}

我想到了PhysicalPressEventRouter而不是

OnClick
但我不清楚它是如何工作的。

c# unity-game-engine button keyboard mrtk
1个回答
0
投票

如果你想触发 mrtk 按钮只需写 不要忘记在检查器中添加可按下的按钮

刚刚提到你无法访问私有变量,当它们是私有的时你想如何使用你的游戏对象

public Pressablebutton A

private void Update()
{      
    `if (Input.GetKeyDown("1"))`
    {
        A.invoke();
    }
 }
© www.soinside.com 2019 - 2024. All rights reserved.