0

我是 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;
        }
    }
4

2 回答 2

2

我猜你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;
}
于 2016-05-19T01:48:20.043 回答
0

如果它小于或等于 waterBucketAmnt,您可以创建一个方法来记录您按下“fire3”的次数,然后将 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--;
    }


}
于 2016-05-19T02:28:11.333 回答