在动画在 unity 5 中播放完毕之前,如何避免按钮输入?
How to refrain button input until after an animation has finished playing in unity 5?
我是 Unity 的新手,也是一名初级程序员。我试验了 Unity 的 GUI(canvas、按钮、标签等...)。
对于按钮,我制作了一个 class 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--;
}
}
我是 Unity 的新手,也是一名初级程序员。我试验了 Unity 的 GUI(canvas、按钮、标签等...)。
对于按钮,我制作了一个 class 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--;
}
}