GuiButton

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

无法创建
不可浏览

GUIButton是一个抽象类,继承自 GuiObject 。它是 ImageButtonTextButton 的对象。它

GuiButton 的最重要事件是 GuiButton.Activated,这是一个 多平台事件,发生在按钮激活时。当使用鼠标时,这意味着单击按钮并释放鼠标仍然位于 UI 对象

概要

属性

  • 读取并联

    确定是否在鼠标悬停或单击时自动更改按钮的颜色。

  • 读取并联

    如果 GUI 元素可见,鼠标不会锁定,除非右键点按。

  • 读取并联

    一个Boolean属性,表示对象是否已选择。

  • 根据预定义风格列设置图形按钮的风格。

继承自GuiObject属性继承自GuiBase2d属性

方法

继承自GuiObject方法

活动

继承自GuiObject活动继承自GuiBase2d活动

属性

AutoButtonColor

读取并联

自动按钮颜色确定是否在用户的 Mouse 悬停或单击时自动更改按钮的颜色。

如果是,按钮会在鼠标悬停在其上或单击时自动更改颜色。 如果是,按钮不会更改。

如果您想自定义按钮在用户的鼠标悬停或单击时变更时间,请考虑使用 ImageButton 图形用户界面,并改变元素的 ImageButton.HoverImageImageButton.PressedImage

请注意,这个属性不会对 ImageButton 有效,如果它的属性设置为 ImageButton.Image 图像,并且不是 null 。此外,该属性不会在鼠标悬停时对 ImageButton.HoverImage 元素在鼠标悬停时或鼠标单击时不会影响。

代码示例

The code below enables a button's GuiButton.AutoButtonColor as long as it it not a ImageButton with ImageButton.HoverImage and ImageButton.PressedImage properties set to images.

In order for the code to work as expected, it must be placed in a LocalScript that is a child of the button.

Changing a GUI Button's Color on Hover and Click

local button = script.Parent
if button:IsA("ImageButton") then
if button.HoverImage and button.ClickImage then
return
end
end
button.AutoButtonColor = true
读取并联

如果 GUI 元素可见,鼠标不会锁定,除非右键点按。

Selected

读取并联

一个Boolean属性,表示对象是否已选择。

读取并联

根据预定义风格列设置图形按钮的风格。

方法

活动

Activated

按钮激活时发射。 作为此事件在服务器上不会发射,因此它只能在 LocalScriptScript 中使用,其中 RunContext 包含 2>Class.Script2> 的 5>Class.Script.RunContext5> 。

参数

inputObject: InputObject
clickCount: number

MouseButton1Click

MouseButton1Click 事件触发时,当用户的 Mouse 完全离开点击 GUI 按钮时。

通过单击,鼠标必须位于按钮的边界之内,并且必须再次按下和向上再次按下才能触发此事件。如果鼠标离开边界,它将不会发射。如果您想避免此限制,您可以使用 GuiButton.MouseButton1DownMouseButton1Up 。这些事件都很相似,但

这个事件与 GuiButton.MouseButton2Click 相似,它的行为与 Class.GuiButton.MouseButton2Click 相同,只是连接到用户的右键。

注意,此事件仅对 GUI 按钮,包括 TextButtonsImageButton 。它不会发射其他 GuiObjects


代码示例

The code sample below demonstrates how to use the GuiButton.MouseButton1Click and GuiButton.MouseButton2Click events to handle user left and right mouse button input on a GuiButton.

For the example to work expected, the LocalScript containing the code should be a child of the GUI button.

Handling User Clicks on a GUI Button

local button = script.Parent
local function leftClick()
print("Left mouse click")
end
local function rightClick()
print("Right mouse click")
end
button.MouseButton1Click:Connect(leftClick)
button.MouseButton2Click:Connect(rightClick)

MouseButton1Down

MouseButton1Down 事件触发时,当用户按下 GUI 对象上的左侧 Mouse 按钮时。

这个事件与 GuiButton.MouseButton2Down 相似,它的行为与 Class.GuiButton.MouseButton2Down 相同,只是连接到用户的右键。

如果您正在寻找需要用户按下并释放左键鼠标在 GUI 上才能触发事件的事件,请考虑使用 GuiButton.MouseButton1Click

注意,此事件仅对 GUI 按钮,包括 TextButtonsImageButton 。它不会发射其他 GuiObjects

参数

鼠标的 X 屏幕坐标在像素。

鼠标的 y 屏幕坐标在像素。


代码示例

The code sample below demonstrates how to use the GuiButton.MouseButton1Up and GuiButton.MouseButton1Down events to handle user input when the left mouse button is pressed down and released up on a GuiButton.

For the example to work expected, the LocalScript containing the code should be a child of the GUI button.

Handling Right Mouse Button Up/Down on a GUI Button

local button = script.Parent
local function leftMouseButtonUp(x, y)
print("Left mouse up at", x, y)
end
local function leftMouseButtonDown(x, y)
print("Left mouse down at", x, y)
end
button.MouseButton1Up:Connect(leftMouseButtonUp)
button.MouseButton1Down:Connect(leftMouseButtonDown)

MouseButton1Up

MouseButton1Up 事件触发时,当用户释放左侧 Mouse 从 GUI 对象上。

这个事件与 GuiButton.MouseButton2Up 相似,它的行为与 Class.GuiButton.MouseButton2Up 相同,只是连接到用户的右键。

如果您正在寻找需要用户按下并释放左键鼠标在 GUI 上才能触发事件的事件,请考虑使用 GuiButton.MouseButton1Click

注意,此事件仅对 GUI 按钮,包括 TextButtonsImageButton 。它不会发射其他 GuiObjects

参数

鼠标的 X 屏幕坐标在像素。

鼠标的 y 屏幕坐标在像素。


代码示例

The code sample below demonstrates how to use the GuiButton.MouseButton1Up and GuiButton.MouseButton1Down events to handle user input when the left mouse button is pressed down and released up on a GuiButton.

For the example to work expected, the LocalScript containing the code should be a child of the GUI button.

Handling Right Mouse Button Up/Down on a GUI Button

local button = script.Parent
local function leftMouseButtonUp(x, y)
print("Left mouse up at", x, y)
end
local function leftMouseButtonDown(x, y)
print("Left mouse down at", x, y)
end
button.MouseButton1Up:Connect(leftMouseButtonUp)
button.MouseButton1Down:Connect(leftMouseButtonDown)

MouseButton2Click

MouseButton2Click 事件触发时,当用户的 Mouse 完全点击 GUI 按钮时。

通过单击,鼠标必须位于按钮的边界之内,并且必须再次按下和向上再次按下才能触发此事件。如果鼠标离开边界,它将不会发射。如果您想避免此限制,您可以使用 GuiButton.MouseButton2DownMouseButton2Up 。这些事件都很相似,但

这个事件与 GuiButton.MouseButton1Click 相似,它的行为与其他 Class.GuiButton.MouseButton1Click 相同,只是它连接到用户的左键。

注意,此事件仅对 GUI 按钮,包括 TextButtonsImageButton 。它不会发射其他 GuiObjects


代码示例

Demonstrates how to use the GuiButton.MouseButton2Click event to detect when a user right clicks a GuiButton.

The mouse has to be in bounds of the button and has to be pressed down and up again before this event fires.

Note: place the "RightClickScreenGui" within StarterGui for this sample to work properly.

GUI Button Right Click

local button = script.Parent.Button
local function rightClick()
print("Right click pressed down and up on button.")
end
button.MouseButton2Click:Connect(rightClick)

MouseButton2Down

MouseButton2Down 事件触发时,当用户按下 GUI 对象上的右键 Mouse 时,鼠标将向下移动。

这个事件与 GuiButton.MouseButton1Down 相似,它的行为与其他没有区别,它与用户的左键鼠标按钮连接在一起。

如果您正在寻找需要用户按下并释放鼠标右键以便在 GUI 上发射事件的事件,请考虑使用 GuiButton.MouseButton2Click

注意,此事件仅对 GUI 按钮,包括 TextButtonsImageButton 。它不会发射其他 GuiObjects

参数

鼠标的 X 屏幕坐标在像素。

鼠标的 y 屏幕坐标在像素。


代码示例

The code sample below demonstrates how to use the GuiButton.MouseButton2Up and GuiButton.MouseButton2Down events to handle user input when the right mouse button is pressed down and released up on a GuiButton.

For the example to work expected, the LocalScript containing the code should be a child of the GUI button.

Handling Left Mouse Button Up/Down on a GUI Button

local gui = script.Parent
local button = gui.Button
function rightMouseButtonUp(x, y)
print("Right mouse up", x, y)
end
function rightMouseButtonDown(x, y)
print("Right mouse down", x, y)
end
button.MouseButton2Up:Connect(rightMouseButtonUp)
button.MouseButton2Down:Connect(rightMouseButtonDown)

MouseButton2Up

MouseButton2Up 事件触发时,当用户释放他们的右键 Mouse 以外的 GUI 对象时。

这个事件与 GuiButton.MouseButton1Up 相似,它的行为与其他没有区别,它与用户的左键点连接。

如果您正在寻找需要用户按下并释放鼠标右键以便在 GUI 上发射事件的事件,请考虑使用 GuiButton.MouseButton2Click

注意,此事件仅对 GUI 按钮,包括 TextButtonsImageButton 。它不会发射其他 GuiObjects

参数

鼠标的 X 屏幕坐标在像素。

鼠标的 y 屏幕坐标在像素。


代码示例

The code sample below demonstrates how to use the GuiButton.MouseButton2Up and GuiButton.MouseButton2Down events to handle user input when the right mouse button is pressed down and released up on a GuiButton.

For the example to work expected, the LocalScript containing the code should be a child of the GUI button.

Handling Left Mouse Button Up/Down on a GUI Button

local gui = script.Parent
local button = gui.Button
function rightMouseButtonUp(x, y)
print("Right mouse up", x, y)
end
function rightMouseButtonDown(x, y)
print("Right mouse down", x, y)
end
button.MouseButton2Up:Connect(rightMouseButtonUp)
button.MouseButton2Down:Connect(rightMouseButtonDown)