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

猜你喜欢

转载自blog.csdn.net/u013404885/article/details/120307375