Skip to content

EUIButton ​

概览 ​

字段值
KindUnit · 单位 Unit
Realmcommon

继承关系 ​

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

继承成员 ​

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

EUIButton 是可脚本控制的界面按钮控件,以场景单位形式挂接到界面根节点上,支持常态、按下、禁用三套图片与配色,并可设置按钮文本、字体、字号和文本颜色。通过拉伸区域与百分比拉伸开关,可让按钮底图按尺寸自适应缩放,同时用禁用开关统一控制交互状态与视觉表现。

适用场景 ​

常用于在游戏界面中创建带文本标签的交互按钮,例如把按钮挂到当前玩家的界面根节点后响应点击事件,并随按钮状态切换不同图片与颜色。

使用要点 ​

先从 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)写入时使用 official://image/... 或项目支持的 Asset 协议字符串(如 official://image/10066);不要写入裸数字或其它协议。点击事件通过 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)。
PressImageStringofficial://image/10001按钮按下图片写入时使用 Asset 协议字符串(如 official://image/10067)。
DisableImageStringofficial://image/10002按钮禁用图片写入时使用 Asset 协议字符串(如 official://image/10068)。
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,用于保存该按钮的拉伸区域配置。它属于公开属性,可通过按钮实例读取或写入。
StretchAreaPercentEnabledBooltrueStretchAreaPercentEnabled 是 EUIButton 的布尔属性,用于控制按钮拉伸区域是否按百分比方式计算,默认值为 true。

关联类型 ​