以TextureRect控件为例,其它控件同理:
// Confirm dialog
// Created by nwhasd
// 2021/9/15
// MIT license
using Godot;
public class Node2D : TextureRect
{
public override void _Ready()
{
// 绑定gui输入事件
Connect("gui_input", this, nameof(OnGuiInput));
}
// 输入事件回调函数
private void OnGuiInput(InputEvent inputEvent)
{
if (inputEvent is InputEventMouseButton mouseInput)
{
if (mouseInput.ButtonIndex == (int) Godot.ButtonList.Left)
{
if (mouseInput.IsPressed())
GD.Print("鼠标左键按下");
else
GD.Print("鼠标左键抬起");
}
else if (mouseInput.ButtonIndex == (int) Godot.ButtonList.Right)
{
if (mouseInput.IsPressed())
GD.Print("鼠标右键按下");
else
GD.Print("鼠标右键抬起");
}
}
}
}