Skip to content

EUIButton ​

概览 ​

字段值
KindUnit · 单位 Unit
Realmcommon

继承关系 ​

  • Unit(4 属性 / 25 函数 / 6 事件)
    • EUINodeBase(35 属性 / 12 函数 / 4 事件)
      • [EUIButton](13 属性)

继承成员 ​

2 个来源 / 39 属性 / 36 函数 / 10 事件

EUIButton 是场景界面中的按钮控件,负责在常态、按下与禁用三种状态下分别呈现图片与配色,并支持按钮文本、字体、字号与文字颜色配置。通过 Disabled 属性可以控制按钮是否处于禁用状态,StretchArea 与 StretchAreaPercentEnabled 用于指定图片的拉伸区域及其是否按百分比计算。点击交互以 OnClicked 事件的形式对外提供。

适用场景 ​

适用于在玩家界面上放置可点击入口,例如在界面根节点下创建功能按钮,为它配置常态图片、按下图片与按钮文本,并在点击时执行对应逻辑。

使用要点 ​

先从 game:GetService("Players").LocalPlayer 取得本地玩家,再通过 localPlayer.PlayerGui.EuiManager:GetRootNode() 拿到界面根节点,然后用 game:GetService("World"):CreateUnit("EUIButton", { Parent = rootNode }) 创建按钮并挂到该节点下。创建后依次写入 ButtonText、ButtonTextFont、ButtonTextFontSize、ButtonTextColor 以及 NormalImage、PressImage、DisableImage(使用 official://image/10001 这样的资产字符串),再按需设置 ButtonNormalColor、ButtonPressColor、ButtonDisableColor 与 StretchArea。StretchAreaPercentEnabled 是只读属性,可读取当前配置,不直接赋值。需要拦截点击时用 OnClicked 订阅回调,需要暂时禁用按钮时把 Disabled 置为真。

注意事项 ​

EUIButton 是按钮控件,通过 World:CreateUnit 创建,并把 Parent 设置为当前玩家 PlayerGui.EuiManager:GetRootNode()(不能自行直接构造,也不能通过 GetService 获取 EUIManager)。按钮图片(PressImage/NormalImage/DisableImage)写入时使用 Asset 协议字符串(如 official://image/10066),不要把调试打印出的 official://image/xxx 字符串原样写回属性。点击事件通过 OnClicked 订阅。

代码示例 ​

创建按钮并设置三态图片 ID ​

lua
-- @runtime client
local Players = game:GetService('Players')
local World = game:GetService("World")
local localPlayer = Players.LocalPlayer
if localPlayer == nil then return end
local rootNode = localPlayer.PlayerGui.EuiManager:GetRootNode()
local button = World:CreateUnit("EUIButton", {
    Parent = rootNode,
    Position = Vector2.New(0, 0),
    Size = Vector2.New(100, 40),
})
button.NormalImage = "official://image/10066"
button.PressImage = "official://image/10067"
button.DisableImage = "official://image/10068"

属性 (13) ​

Name类型默认值说明
NormalImageStringofficial://image/10000按钮常态图片写入时使用 Asset 协议字符串(如 official://image/10066);不要把调试打印出的 official://image/... 字符串原样写回属性。
PressImageStringofficial://image/10001按钮按下图片写入时使用 Asset 协议字符串(如 official://image/10067);不要把调试打印出的 official://image/... 字符串原样写回属性。
DisableImageStringofficial://image/10002按钮禁用图片写入时使用 Asset 协议字符串(如 official://image/10068);不要把调试打印出的 official://image/... 字符串原样写回属性。
DisabledBoolfalse是否禁用属性,类型为 Bool,用于控制按钮是否处于禁用状态,默认值为 false。
ButtonTextString""按钮文本属性,类型为 String,用于设置按钮上显示的文字内容,默认值为空字符串。
ButtonTextFontTextFontTypeEnums.TextFontType.HKXZYT_W9_GB按钮文本字体属性,类型为 TextFontType,用于指定按钮文本所使用的字体,默认值为 hkxzyt_w9_gb.ttf。
ButtonTextFontSizeInt42按钮文本字号属性,类型为 Int,用于指定按钮文本的字体大小,默认值为 42。
ButtonTextColorColor[255, 255, 255, 255]按钮文本颜色属性,类型为 Color,用于设置按钮文本的显示颜色,默认值为 (255, 255, 255, 255)。
ButtonNormalColorColor[255, 255, 255, 255]按钮的常态颜色属性,类型为 Color,用于设置按钮在常态下使用的颜色,默认值为 (255, 255, 255, 255)。
ButtonPressColorColor[255, 255, 255, 255]按钮的按下态颜色属性,类型为 Color,用于设置按钮被按下时使用的颜色,默认值为 (255, 255, 255, 255)。
ButtonDisableColorColor[255, 255, 255, 255]按钮的禁用态颜色属性,类型为 Color,用于设置按钮在禁用状态下使用的颜色,默认值为 (255, 255, 255, 255)。
StretchAreaVector4[0, 0, 0, 0]EUIButton 的拉伸区域属性,类型为 Vector4,用于保存该按钮的拉伸区域配置。它属于公开属性,可通过按钮实例读取或写入。
StretchAreaPercentEnabledBooltrueEUIButton 的拉伸区域按百分比计算属性,类型为 Bool,用于标识拉伸区域是否按百分比方式计算。该属性为公开的只读属性。

关联类型 ​