调整动画剪辑的长度以匹配冷却时间



我打算有几个按钮来生成怪物,每个按钮都有不同的冷却时间。有没有办法将动画剪辑匹配为与按钮冷却时间相同的长度?

下面是一个示例:https://gyazo.com/0a2ae868e5458c701e1a258aac6dc59a

动画为 1 秒,但冷却时间为 3 秒。

这是我的代码:

private void ButtonCooldown()
{
if (GetComponent<Button>().interactable == false)
{
buttonTimer += Time.deltaTime;

if (buttonTimer >= cooldown)
{
GetComponent<Button>().interactable = true;
buttonTimer = 0;
}
}
}
public void DisableButton()
{
GetComponent<Button>().interactable = false;
myAnimatior.SetTrigger("ButtonCooldownAnimation");
}

您可以调整Animatorspeed来调整其整体播放速度。

例如,类似的东西

// Adjust in the inspector
[SerializeField] private float cooldownTime = 3;
// Already reference this via the Inspector if possible
[SerializeField] private Button button;
private void Awake ()
{
// Do this only once!
if(!button) button = GetComponemt<Button>();
}
public void DisableButton()
{
button.interactable = false;
// typo in Animator btw ;)
myAnimatior.SetTrigger("ButtonCooldownAnimation");
// Make the Animator play slower so the animation now takes 3 seconds
myAnimatior.speed = 1/cooldownTime;
// Instead of Update simply use Invoke here
// Execute the method called WhenCooldownDone after cooldownTime seconds
Invoke(nameof(WhenCooldownDone), cooldownTime);
}
private void WhenCooldownDone ()
{
button.interactable = true;
myAnimator.speed = 1;
}

就像在评论中一样,我会使用Invoke而不是不断检查Update的状态.特别是切勿在Update中重复使用GetComponent。这是非常昂贵的。始终尝试存储引用并重用它。

最新更新