【问题标题】:How to refrain button input until after an animation has finished playing in unity 5?在统一 5 中播放完动画之前,如何避免按钮输入?
【发布时间】:2016-05-19 01:12:52
【问题描述】:

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

【问题讨论】:

    标签: c# unity3d unity5 animator


    【解决方案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;
    }
    

    【讨论】:

    • 所以我使用 StateInfo 延迟了它;但是,我不知道它为什么起作用。我的代码应该更新到我现在所拥有的。由于某种原因,我不得不两次使用 StateInfo。我不明白为什么会这样,但我尝试删除一个,我仍然可以通过在动画播放一次时单击鼠标 3 次来减少 waterAmnt。我觉得我的程序效率很低,但现在它并不重要。对我的代码为何有效有任何想法吗?
    • “两次使用StateInfo”是什么意思?你能显示更新的代码吗?此外,如果这解决了您的问题,请将答案标记为正确。谢谢。
    • 我相信我的代码已更新。我所做的唯一更改是在 FixedUpdate()
    • 我认为问题可能是因为您使用的是animator.SetBool。不要在 Animator 中使用 bool 参数,您应该使用 Trigger。当您使用animator.SetTrigger 时,动画将被触发,然后Trigger 参数将自动变回。当您使用 animator.SetBool 时,您需要在每个循环中将其更改回 false,这可能会干扰您的逻辑。所以尝试将SetBool 更改为SeTrigger,然后删除GetCurrentAnimationStateInfo 之一。
    【解决方案2】:

    如果小于或等于 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--;
        }
    
    
    }
    

    【讨论】:

    • 我的代码连接到动画控制器以在空闲或运行之间进行混合,因此 IsPlaying 不起作用。不过以后我可能会尝试将其用作动画。
    猜你喜欢
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多