[英]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.
单击按钮图形的边缘并按住鼠标按钮时会出现问题。
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 中的某些内容?
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.