主题
EUIButton
概览
| 字段 | 值 |
|---|---|
| Kind | Unit · 单位 Unit |
| Realm | common |
继承关系
- Unit(4 属性 / 25 函数 / 6 事件)
- EUINodeBase(34 属性 / 8 函数 / 4 事件)
- [EUIButton](13 属性)
- EUINodeBase(34 属性 / 8 函数 / 4 事件)
继承成员
2 个来源 / 38 属性 / 33 函数 / 10 事件
- 来自 EUINodeBase(34 属性 / 8 函数 / 4 事件)
- 属性:
Visible、Position、Anchor、FlippedX、FlippedY、Size、Rotation、Opacity、TouchBeginAudio、TouchEndAudio、TouchClickAudio、TouchbeginEvent、ClickEvent、TouchendEvent、LongtouchEvent、ShowEvent、HideEvent、TouchEnabled、SwallowTouchEnabled、TopAdaptMode、TopAdaption、BottomAdaptMode、BottomAdaption、LeftAdaptMode、LeftAdaption、RightAdaptMode、RightAdaption、AnchorXAdaptMode、AnchorXAdaption、AnchorYAdaptMode、AnchorYAdaption、LocalZOrder、LayoutOrder、Scale - 函数:
RemoveFromParent、GetFullPath、TweenPosition、TweenSize、TweenSizeAndPosition、TweenOpacity、Retain、Release - 事件:
OnTouchBegan、OnTouchMoved、OnTouchEnded、OnClicked
- 属性:
- 来自 Unit(4 属性 / 25 函数 / 6 事件)
- 属性:
AssetId、Name、Desc、Parent - 函数:
ClearAllChildren、GetChildren、GetDescendants、IsAncestorOf、IsDescendantOf、FindFirstAncestor、FindFirstAncestorWhichIsA、FindFirstAncestorOfClass、FindFirstChild、FindFirstChildWhichIsA、FindFirstChildOfClass、HasChildren、GetChildCount、GetChildAtIndex、SetAttribute、GetAttribute、GetAllProps、Clone、IsA、Destroy、GetPropertyChangedSignal、GetAttributeChangedSignal、FindFromPath、GetFullPath、GetDebugId - 事件:
ChildAdded、ChildRemoved、DescendantAdded、DescendantRemoving、AncestryChanged、Destroying
- 属性:
EUIButton 是 UI 按钮控件,用于在界面上创建可交互的按钮。它支持设置常态、按下和禁用三种状态的图片与颜色,并能显示文本。点击事件通过 OnClicked 订阅,可响应玩家操作。
适用场景
常用于制作游戏主菜单、设置面板或确认对话框中的按钮,例如开始游戏、确认购买等交互元素。
使用要点
通过 World:CreateUnit 创建实例,将 Parent 设为 PlayerGui.EuiManager:GetRootNode() 以显示在玩家界面上。设置 NormalImage、PressImage 等属性指定按钮外观,然后通过 OnClicked 事件连接处理函数响应点击。
注意事项
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 | 类型 | 默认值 | 说明 |
|---|---|---|---|
NormalImage | String | official://image/10000 | 按钮常态图片写入时使用 Asset 协议字符串(如 official://image/10066);不要把调试打印出的 official://image/... 字符串原样写回属性。 |
PressImage | String | official://image/10001 | 按钮按下图片写入时使用 Asset 协议字符串(如 official://image/10067);不要把调试打印出的 official://image/... 字符串原样写回属性。 |
DisableImage | String | official://image/10002 | 按钮禁用图片写入时使用 Asset 协议字符串(如 official://image/10068);不要把调试打印出的 official://image/... 字符串原样写回属性。 |
Disabled | Bool | false | 控制按钮是否处于禁用状态。 |
ButtonText | String | "" | 按钮上显示的文本内容。 |
ButtonTextFont | TextFontType | Enums.TextFontType.HKXZYT_W9_GB | 按钮文本的字体。 |
ButtonTextFontSize | Int | 42 | 按钮文本的字体大小。 |
ButtonTextColor | Color | [255, 255, 255, 255] | 按钮上文本的颜色。 |
ButtonNormalColor | Color | [255, 255, 255, 255] | 按钮处于正常状态时的背景颜色。 |
ButtonPressColor | Color | [255, 255, 255, 255] | 按钮被按下时的背景颜色。 |
ButtonDisableColor | Color | [255, 255, 255, 255] | 按钮处于禁用状态时的背景颜色。 |
StretchArea | Vector4 | [0, 0, 0, 0] | 按钮图片的拉伸区域,用于九宫格拉伸。 |
StretchAreaPercentEnabled | Bool | true | 控制拉伸区域是否按百分比计算。 |
