Godot控件响应鼠标点击事件

以TextureRect控件为例,其它控件同理:

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

你可能感兴趣的:(godot,c#)