简体   繁体   English

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

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

I created this button prefab that is using the built-in Unity Button component, an image component with Raycast Target, and an Animator component that is playing a scaling animation when pressing the button.我创建了这个按钮预制件,它使用内置的 Unity Button 组件、一个带有 Raycast Target 的图像组件,以及一个在按下按钮时播放缩放 animation 的 Animator 组件。 The problem is occurring when clicking on the edge of the button graphic and holding the mouse button down.单击按钮图形的边缘并按住鼠标按钮时会出现问题。

带有正在按下的按钮的 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. 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. As the button scales back to normal size automatically (and while still holding the mouse button pressed) the button is getting pressed again - getting stuck in a loop .随着按钮自动缩小到正常大小(并且仍然按住鼠标按钮),按钮再次被按下 -陷入循环

How can I prevent that from happening?我怎样才能防止这种情况发生? Do I need to modify the order of my components or maybe change something in the Animator?我是否需要修改组件的顺序或更改 Animator 中的某些内容?

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

You can add timer for check animation is running.您可以添加计时器以检查 animation 是否正在运行。 example:例子:

 //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 }

then click function can't use before the animation end.然后点击 function can't use before animation 结束。

To sum up what was said in the comments: you want the raycast target to stay the same size while animation plays.总结评论中所说的内容:您希望光线投射目标在 animation 播放时保持相同大小。 That's why you should probably split your Popup Button into two GameObjects: one will be the actual Button (that stays the same size) and the other will be animated, and then the question is mostly "how do I change another object's animation from button click".这就是为什么你应该将你的弹出按钮分成两个游戏对象:一个是实际的按钮(保持相同的大小),另一个是动画的,然后问题主要是“我如何通过按钮单击更改另一个对象的 animation ”。

To do that, you should have the Button component on the actual button, Animator component on the animated object, and in the Button's onClick list you should have the call to the animated objects' Animator Component -> SetTrigger with the name of the trigger you want as an argument.为此,您应该在实际按钮上使用 Button 组件,在动画 object 上使用 Animator 组件,在 Button 的 onClick 列表中,您应该调用动画对象的 Animator Component -> SetTrigger 并使用触发器的名称想要作为论据。 Also note that the animated object shouldn't be the raycast target, but the actual button should.另请注意,动画 object 不应该是光线投射目标,但实际按钮应该。

You can pick what happens when animation is triggerred again before it finishes (for example when button is clicked multiple times) by setting transition parameters in the animation controller.您可以通过在 animation controller 中设置转换参数来选择当 animation 在完成之前再次触发时(例如多次单击按钮时)会发生什么。 But in this case just separating the button from the animated image should do what you want, because the raycast target will now stay the same size and the animation won't get triggered multiple times anymore.但是在这种情况下,只需将按钮与动画图像分开就可以满足您的要求,因为光线投射目标现在将保持相同的大小,并且 animation 不会再被多次触发。

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

相关问题 如何从另一个类更改按钮的属性? - How to change a button's properties from another class? 如何通过单击另一个表单的按钮来更改一个表单中的按钮的文本? - How to change the Text of a Button in One form with a Button click of another Form? 在另一个按钮的单击事件中如何访问一个按钮的发送者对象 - How access sender object of one button in click event of another button 如何从另一个表单中检测按钮单击 - how to detect a button click from another form 如何通过单击另一个表单的按钮并关闭第二个表单来还原最小化的表单 - how to restore a minimized form from another form's button click and closing the second form too 从另一个表单的按钮单击事件访问后台工作者 - Accessing background worker from another form's button click event 如何在按钮单击事件中更改另一个表单名称 - how to change another form name in button click event 如何通过单击另一页面的链接按钮来调用按钮的单击事件,而无需在c#ASP.NET中回发 - How to call button's click event from click of linkbutton of another page without post back in c# ASP.NET 单击按钮时如何更改网格面板的可见性? - How to change grid panel`s visibility while i click on a button? 如何从另一个 window 中单击的按钮更改标签的内容? - How can I change label's content from a button being clicked in another window?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM