如何解决需要一种在协程中使用按钮单击功能与 GetKeyDown 的方法
这个动画脚本可以工作,但是,代替 WHILE LOOP 中的 Keycode 输入,我需要使用移动设备的 UI 按钮,我一直无法弄清楚。我找到了一个关于在它周围放置一个包装器以使方法可用于单击事件的答案,但不知道它应该如何在更新函数中工作。我花了很长时间才走到这一步,作为 unity 和 c# 的新手,所以如果您能提供详细的答案或建议,我可以恢复我的生活,如果可以,请提供帮助。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using SWS;
public class goat_question : MonoBehaviour
{
private Animator anim;
public GameObject player;
public Text ResultText;
public Text AnswerText;
public Text AnswerText2;
public Button GoatButton;
void Start()
{
anim = GetComponent<Animator>();
Button btn = GoatButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
}
void TaskOnClick()
{
Debug.Log("You have clicked the button!");
}
// Update is called once per frame
void Update()
{
if (AnswerText.text.Equals(AnswerText2.text))
{
StartCoroutine(GoatWalkPathCoroutine());
}
IEnumerator GoatWalkPathCoroutine()
{
while (true)
{
if (Input.GetKeyDown(KeyCode.K))
{
anim.Play("goat_hi_walk");
player.GetComponent<splineMove>().enabled = true;
yield return new WaitForSeconds(27);
anim.Play("goat_hi_licking");
}
yield return null;
}
}
}
}
解决方法
在一个单独的 UI 按钮脚本中,有一个名为 isClicked 或其他东西的 bool,当按钮被点击时,将其设置为 true。在这个主脚本中,您可以引用刚刚创建的脚本,而不是 Input.GetKey,您可以说 if(otherScript.isClicked)
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。