Godot control responds to mouse click events

Take the TextureRect control as an example. The same applies to other controls:

// Mouse Click Event
// 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("鼠标右键抬起");
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/u013404885/article/details/120307375