[英]How to refrain button input until after an animation has finished playing in unity 5?
我是Unity新手和初学者。 我尝试了Unity的GUI(画布,按钮,标签等)。
对于该按钮,我创建了一个ButtonClickScript类,因此,当我点击该按钮时,变量waterBucketAmnt会一直增加到3。然后我决定进一步进行实验,因此我创建了一个非常简单的男生动画。倾倒一桶水。 我想限制该家伙可以使用waterBucketAmnt倒水的次数。
我可以点击GUI按钮3次,一切正常。 int waterBucketAmnt =3。然后,我开始游戏,并在键盘上按配置的“ Fire3”操作,该操作完全符合预期。 问题是我可以在不到一秒钟的时间内连续点击“ Fire3”动作,因此,waterBucketAmnt = 0,动画只播放一次。 如何延迟输入直到动画完成?
using UnityEngine;
using System.Collections;
public class waterBucketAction : MonoBehavior {
Animator waterAnim;
// a simple player controller using unity's RigidBody2D feature. I added PlayerControllerScript so that I could make sure the player had to remain on the ground in order to dump the bucket of water. checkGround is so that I could inherit the grounded gameObject I created.
PlayerControllerScript checkGround;
public ButtonClickScript checkWaterAmnt;
void Start () {
checkGround = FindObjectOfType <playerControllerScript> ();
waterAnim = GetComponent<Animator> ();
}
void FixedUpdate() {
waterAnim.SetBool ("isPlaying", false);
if (checkWaterAmnt.waterBucketAmnt > 0 && !waterAnim.GetCurrentAnimatorStateInfo(0).IsName("waterBucketAnimation")) {
if (checkGround.grounded == true && Input.GetButtonDown ("Fire3")) {
waterAnim.SetBool("isPlaying", true);
//I need to wait for the animation to complete or delay the input. I searched multiple places and the best answer I could find was Animation.IsAnimating, but that seems to be deprecated? The API says that Animator replaced Animation, but I couldn't find an equivalent method.
if(waterAnim.GetCurrentAnimatorStateInfo(0).IsName("waterBucketAnimation"))
checkWaterAmnt.waterBucketAmnt -=1;
}
}
我猜你的ButtonClickScript
看起来像这样:
public class ButtonClickScript : MonoBehavior, IPointerClickHandler
{
public int waterBucketAmnt;
void OnPointerClick()
{
if (waterBucketAmnt < 3)
waterBucketAmnt++;
}
}
因此,需要延迟输入的操作是通过添加以下行来更改单击按钮后的按钮的可Interactable
状态:
if (waterBucketAmnt < 3)
{
waterBucketAmnt++;
this.GetComponent<Button>().Interactable = false;
//If Interactable is not accessible from code you could also use
//this.gameObject.SetActive(false);
}
然后在waterBucketAction
脚本中,一旦动画结束,您需要将按钮的Interactable
状态更改回true
:
void FixedUpdate(}
{
//The 0 is the Layer Index
if (this.animator.GetCurrentAnimatorStateInfo(0).IsName("YourWaterAnimationName"))
checkWaterAmnt.GetComponent<Button>().Interactable = true;
}
您可以创建一种方法来记录按下“ fire3”的时间(如果它小于或等于waterBucketAmnt),然后将waterAnim循环到等于所记录数字的时间。 播放动画时,它不会增加fireCount,而只会在动画完成时增加。 这方面的东西
void Update () {
if (!animation.IsPlaying("yourWaterAnimationName"))
{
if (Input.GetButtonDown("Fire3"))
{
if (fireCount < waterBucketAmnt)
{
fireCount++;
}
}
}
for (int i = 0; i < fireCount; i++)
{
animation.PlayQueued("yourWaterAnimationName");
waterBucketAmnt--;
fireCount--;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.