繁体   English   中英

如何通过单击按钮更改另一个对象的 animation?

[英]How to change another object's animation from button click?

我创建了这个按钮预制件,它使用内置的 Unity Button 组件、一个带有 Raycast Target 的图像组件,以及一个在按下按钮时播放缩放 animation 的 Animator 组件。 单击按钮图形的边缘并按住鼠标按钮时会出现问题。

带有正在按下的按钮的 UI 弹出窗口。当光标按下边缘上的按钮时,它会非常快速地重复播放按下动画,在按下和正常状态之间切换。

As the animation is scaling down the button size (as well as the image component with the Raycast Target) the mouse cursor is at some point no longer hovering over the button and the animation state is going back to normal. 随着按钮自动缩小到正常大小(并且仍然按住鼠标按钮),按钮再次被按下 -陷入循环

我怎样才能防止这种情况发生? 我是否需要修改组件的顺序或更改 Animator 中的某些内容?

Unity Inspector 中的按钮显示按钮游戏对象的所有组件

您可以添加计时器以检查 animation 是否正在运行。 例子:

 //your animation duration here: private float animationduration = 0f; bool isClickable = true; //set your timer with default 0 private float timer = 0f; private void Update() { timer += Time.deltaTime; if (timer > animationduration) { isClickable = true; } else { isClickable = false; } if (isClickable && Input.GetMouseButtonDown(0)) { clickFunction(); timer = 0f; } } void clickFunction() { //ur function in here }

然后点击 function can't use before animation 结束。

总结评论中所说的内容:您希望光线投射目标在 animation 播放时保持相同大小。 这就是为什么你应该将你的弹出按钮分成两个游戏对象:一个是实际的按钮(保持相同的大小),另一个是动画的,然后问题主要是“我如何通过按钮单击更改另一个对象的 animation ”。

为此,您应该在实际按钮上使用 Button 组件,在动画 object 上使用 Animator 组件,在 Button 的 onClick 列表中,您应该调用动画对象的 Animator Component -> SetTrigger 并使用触发器的名称想要作为论据。 另请注意,动画 object 不应该是光线投射目标,但实际按钮应该。

您可以通过在 animation controller 中设置转换参数来选择当 animation 在完成之前再次触发时(例如多次单击按钮时)会发生什么。 但是在这种情况下,只需将按钮与动画图像分开就可以满足您的要求,因为光线投射目标现在将保持相同的大小,并且 animation 不会再被多次触发。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM