需要一种方法来使用按钮单击功能与协程中的 GetKeyDown
Need a way to use button click functionality vs GetKeyDown in Coroutine
这个动画脚本可以工作,但是,代替 WHILE LOOP 中的键码输入,我需要使用移动设备的 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 之类的布尔值,当按钮被单击时将其设置为 true。在此主脚本中,您可以引用刚刚制作的脚本,而不是 Input.GetKey,您可以说 if(otherScript.isClicked)
.
这个动画脚本可以工作,但是,代替 WHILE LOOP 中的键码输入,我需要使用移动设备的 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 之类的布尔值,当按钮被单击时将其设置为 true。在此主脚本中,您可以引用刚刚制作的脚本,而不是 Input.GetKey,您可以说 if(otherScript.isClicked)
.