Roblox - 创建一个自定义事件来检测鼠标点击?

首先,我不想使用Mouse对象,所以MouseButton1Click不是一个选择。我想使用UserInputService,但我的解决方案似乎还不够干净。

ModuleScript:

function GuiModule.Click(Input)
    if Input.UserInputType == Enum.UserInputType.MouseButton1 or Input.UserInputType == Enum.UserInputType.Touch then
        return true
    else
        return false
    end
end

LocalScript:

local GuiModule = require(game.Players.LocalPlayer.PlayerGui.GuiModule)

Button.InputEnded:Connect(function(Input)
    if GuiModule.Click(Input) then --我真的必须为每个InputEnded事件编写if语句吗?
        print("Button clicked")
    end
end)

理想情况下,我想要像这样,**.Clicked**是一个在模块脚本中利用面向对象编程的自定义事件。我已经研究过元表和元函数,但是我无法完全理解它们的功能来实现这一点,如果可能的话。

local GuiModule = require(game.Players.LocalPlayer.PlayerGui.GuiModule)

Button.Clicked:Connect(function(Input)
    print("Button clicked")
end)
点赞
用户2860267
用户2860267

尝试使用 Activated event。它会自动处理不同类型的输入,因此您无需检测它是鼠标还是触摸输入。

Button.Activated:Connect(function()
    print("按钮被点击或轻触")
end)
2020-08-02 01:40:36