gpt4 book ai didi

c# - 是否有统一的 C# 代码来识别 Button 何时被按下以及何时被释放?

转载 作者:太空宇宙 更新时间:2023-11-03 20:49:51 26 4
gpt4 key购买 nike

我目前正尝试在 C# 中设置一段代码,用于识别某个按钮何时被按下以及何时被释放

我试图让这个特定的按钮在按下时缩小,在释放时放大

public class clickshake : MonoBehaviour
{
public GameObject click;
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
transform.localScale += new Vector3(-0.1F, -0.1f, 0);
}
if (Input.GetMouseButtonUp(0))
{
transform.localScale += new Vector3(0.1F, 0.1f, 0);
}
}
}

通过使此脚本专用于其“单击”命令中的按钮它只是增加了按钮的大小,并没有缩小它

最佳答案

making this script exclusive to the button in its On Click command

这没什么意义。尤其是 Update 方法每帧都会被调用。因此,从 onClick 额外调用它会在该帧中调用它两次。同样来自 Button.onClick

Note that EventType.MouseDown and EventType.MouseUp are called prior to onClick.


例如,您可以使用 IPointerDownHandlerIPointerUpHandler实现它的接口(interface)

public class clickshake : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
{
//Detect current clicks on the GameObject (the one with the script attached)
public void OnPointerDown(PointerEventData pointerEventData)
{
transform.localScale -= new Vector3(0.1F, 0.1f, 0);
}

//Detect if clicks are no longer registering
public void OnPointerUp(PointerEventData pointerEventData)
{
transform.localScale += new Vector3(0.1F, 0.1f, 0);
}
}

注意

Ensure an EventSystem exists in the Scene to allow click detection. For click detection on non-UI GameObjects, ensure a PhysicsRaycaster is attached to the Camera.

当然,非 UI 对象也需要一个 Collider

关于c# - 是否有统一的 C# 代码来识别 Button 何时被按下以及何时被释放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56360951/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com