🪟 Widget
控件类支持通过脚本生成虚幻引擎的 Widget 类,并能像蓝图一样对它们进行操作!

Client OnlyAPI Source
👪Inheritance
This class shares methods and events from Base Entity.
Widget 类提供了一种非常灵活的方式,可以在游戏内创建和使用虚幻控件作为 UI。
备注
Widget 类中未公开大部分参数,应使用 CallBlueprintEvent() 方法进行调用,该方法会调用底层特定的 Widget 方法。
🎒 示例
创建一个简单的原生文本并将其添加到屏幕
Client/Index.lua
local my_text = Widget(NativeWidget.Text)
my_text:CallBlueprintEvent("SetText", "Hello World!")
my_text:AddToViewport()
提示
SetText 是来自 UTextBlock 的方法,它是与 NativeWidget.Text 关联的 Widget。
创建带有子项的控件
Client/Index.lua
local my_vertical_box = Widget(NativeWidget.VerticalBox)
my_vertical_box:AddToViewport()
local my_text = Widget(NativeWidget.Text)
my_text:CallBlueprintEvent("SetText", "Hello World!")
local my_button = Widget(NativeWidget.Button)
my_vertical_box:AddChild(my_text)
my_vertical_box:AddChild(my_button)
将 WebUI 用作图像笔刷
Client/Index.lua
local webui = WebUI("mywebui", "https://google.com", WidgetVisibility.Hidden)
local my_image = Widget(NativeWidget.Image)
my_image:CallBlueprintEvent("SetBrushFromMaterial", webui)
my_image:AddToViewport()
提示
SetBrushFromMaterial 是来自 UImage 的方法,并且接收一个 MaterialInstance 作为参数。传入 WebUI、SceneCapture 或 Canvas 作为参数时,会自动将其转换为其内部材质!
订阅调度器
Client/Index.lua
local my_button = Widget(NativeWidget.Button)
-- 在内部添加文本
local my_text = Widget(NativeWidget.Text)
my_text:CallBlueprintEvent("SetText", "Press Me!")
my_button:AddChild(my_text)
-- 绑定原生 OnClicked 调度器
my_button:BindBlueprintEventDispatcher("OnClicked", function()
Console.Log("clicked!")
end)
-- 将按钮添加到视口(将填满整个屏幕)
my_button:AddToViewport()
📚 库和框架
以下是社区创建的使用 Widget 扩展其可能性的库和框架列表:
🛠 构造函数
UserWidget Constructor
传入一个 UserWidget 蓝图来生成一个控件
local my_widget = Widget(blueprint_path)
Parameters
| Type | Name | Default | Description |
|---|---|---|---|
| Blueprint Reference | blueprint_path | Required parameter | 要生成的自定义 UserWidget 蓝图路径 |
Native Widget Constructor
传入一个 NativeWidget 枚举来生成一个控件
local my_widget = Widget(native_widget)
Parameters
| Type | Name | Default | Description |
|---|---|---|---|
| NativeWidget | native_widget | Required parameter | 要生成的原生控件之一 |
🦠 函数
Inherited Entity Functions
Widget inherits from Base Entity Class, sharing it's methods and functions:
Base Entityscripting-reference/classes/base-classes/entity
Base Entityscripting-reference/classes/base-classes/entity
| Returns | Name | Description | |
|---|---|---|---|
BroadcastRemoteEvent | 直接在此实体上向所有玩家调用自定义远程事件 | ||
BroadcastRemoteInRadiusEvent | 直接针对此实体调用自定义远程事件,并将该事件传递给半径范围内的所有玩家 | ||
CallRemoteEvent | 直接在此实体上向特定玩家调用自定义远程事件 | ||
CallRemoteEvent | 直接在此实体上调用自定义远程事件 | ||
CallRemotePlayersEvent | 直接在此实体上调用自定义远程事件,并将事件传递给玩家列表 | ||
Destroy | 销毁该实体 | ||
FinishSpawn | 如果此实体是以延迟 spawn_mode 生成的,则完成生成过程并将该实体发送至客户端 | ||
| table of string | GetAllValuesKeys | 获取所有存储值的键名列表 | |
| table | GetClass | 获取该实体的类 | |
| integer | GetID | 获取该实体的全网通用网络 ID(在客户端和服务器上相同) | |
| any | GetValue | 获取此实体上存储在给定键处的 值 | |
| boolean | HasAuthority | 获取本地上下文是否对此实体拥有主控(如果由客户端生成则为 true,由服务器生成则为 false) | |
| boolean | IsA | 递归检查此实体是否继承自某个类 | |
| boolean | IsBeingDestroyed | 如果该实体正在被销毁,则返回 true | |
| boolean | IsSpawned | 获取此实体是否已完成生成 | |
| boolean | IsValid | 如果该实体有效(即未被销毁且指向一个有效的实体),则返回 true | |
SetSpawnMode | 从继承类的构造函数中覆盖此实体完成生成的时间点 | ||
SetValue | 在此实体中设置一个值 | ||
| function | Subscribe | 在这个特定实体上订阅一个事件 | |
| function | SubscribeRemote | 在这个特定实体上订阅一个从服务器调用的自定义远程事件 | |
Unsubscribe | 退订此包内该实体中此 事件 的所有回调,或者仅取消传入的特定回调 |
| Returns | Name | Description | |
|---|---|---|---|
AddChild | 如果此控件为 Panel,则向该控件容器中添加一个新的子控件 | ||
AddToViewport | 将其添加到游戏视口中,填满整个屏幕 | ||
| function | BindBlueprintEventDispatcher | 分配并绑定一个蓝图事件分发器 | |
BringToFront | 将此控件置于所有 WebUI 和控件的最前面 | ||
| varargs of any | CallBlueprintEvent | 调用一个蓝图事件或函数 | |
| any | GetBlueprintPropertyValue | 获取蓝图属性/变量的值 | |
| WidgetVisibility | GetVisibility | 返回当前 WebUI 的可见性 | |
SetBlueprintPropertyValue | 直接设置蓝图属性/变量的值 | ||
SetContentForSlot | 如果此控件为 UserWidget,则按名称为指定槽位设置控件内容 | ||
SetFocus | 启用此控件的焦点 | ||
SetSlotAlignment | 设置水平和垂直定位的插槽对齐方式 | ||
SetSlotCanvasLayout | 将布局设为屏幕上的画布 | ||
SetSlotPadding | 设置水平和垂直间距的插槽内边距 | ||
SetSlotSize | 设置插槽大小和尺寸规则 | ||
SetVisibility | 设置其在屏幕上的可见性 | ||
| Widget3D | SpawnWidget3D | 生成一个在世界中渲染此控件的 3D 控件 Actor | |
UnbindBlueprintEventDispatcher | 解绑一个蓝图事件分发器 |
Moderate

AddChild
如果此控件为 Panel,则向该控件容器中添加一个新的子控件
my_widget:AddChild(other)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| WebUI or Widget | other | Required parameter | 要添加的 WebUI 或 Widget |
Moderate

AddToViewport
将其添加到游戏视口中,填满整个屏幕
my_widget:AddToViewport()
Slow

BindBlueprintEventDispatcher
分配并绑定一个蓝图事件分发器
local ret = my_widget:BindBlueprintEventDispatcher(dispatcher_name, callback)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | dispatcher_name | Required parameter | 事件分发器名称 |
| function | callback | Required parameter | 要调用的回调函数 with this format |
Returns
| Type | Description |
|---|---|
| function | 回调函数本身 |
Optimal

BringToFront
将此控件置于所有 WebUI 和控件的最前面。
注:只有在控件的父级是视口时才能调用此方法!
my_widget:BringToFront()
Slow

CallBlueprintEvent
调用一个蓝图事件或函数
在客户端返回所有函数返回值
local ret_01, ret_02, ... = my_widget:CallBlueprintEvent(event_name, arguments...?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | event_name | Required parameter | 事件或函数名称 |
| any | arguments...? | nil | 要传递给该事件的参数序列 |
Slow

GetBlueprintPropertyValue
获取蓝图属性/变量的值
local ret = my_widget:GetBlueprintPropertyValue(property_name)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | property_name | Required parameter | 蓝图变量的名称 |
Returns
| Type | Description |
|---|---|
| any | 值 |
See also SetBlueprintPropertyValue.
Optimal

GetVisibility
返回当前 WebUI 的可见性
local visibility = my_widget:GetVisibility()
Returns
| Type | Description |
|---|---|
| WidgetVisibility | 当前 WebUI 的可见性 |
See also SetVisibility.
Slow

SetBlueprintPropertyValue
直接设置蓝图属性/变量的值
my_widget:SetBlueprintPropertyValue(property_name, value)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | property_name | Required parameter | 蓝图变量的名称 |
| any | value | Required parameter | 要设置的值 |
See also GetBlueprintPropertyValue.
Moderate

SetContentForSlot
如果此控件为 UserWidget,则按名称为指定槽位设置控件内容
my_widget:SetContentForSlot(slot_name, widget?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | slot_name | Required parameter | 命名槽位的名称 |
| Widget | widget? | nil | 传入 nil 以将其移除 |
Optimal

SetFocus
启用此控件的焦点。
启用此控件的焦点(即可以接收键盘输入并触发输入事件)
注:同一时间只能有一个控件拥有焦点。
my_widget:SetFocus()
Moderate

SetSlotAlignment
设置水平和垂直定位的插槽对齐方式。
注:该方法仅在此控件是以下任一控件的子级时才有效:HorizontalBoxSlot、VerticalBoxSlot、OverlaySlot、SizeBoxSlot、ScrollBoxSlot、BackgroundBlurSlot、BorderSlot、ButtonSlot、ScaleBoxSlot、UniformGridSlot、GridSlot。
my_widget:SetSlotAlignment(horizontal_alignment?, vertical_alignment?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| WidgetHorizontalAlignment | horizontal_alignment? | WidgetHorizontalAlignment.Fill | 插槽的水平对齐方式 |
| WidgetVerticalAlignment | vertical_alignment? | WidgetVerticalAlignment.Fill | 插槽的垂直对齐方式 |
Optimal

SetSlotCanvasLayout
将布局设为屏幕上的画布。
注:该方法仅在此控件是画布面板的子级时才有效。
锚点:
my_widget:SetSlotCanvasLayout(location?, size?, anchors_min?, anchors_max?, alignment?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| Vector2D | location? | Vector(0, 0) | 屏幕位置,或者当锚点拉伸时的左侧和顶部偏移量 |
| Vector2D | size? | Vector(0, 0) | 尺寸,或者当锚点拉伸时的右侧和底部偏移量 |
| Vector2D | anchors_min? | Vector(0, 0) | 最小锚点,从 (0, 0) 左上角到 (1, 1) 右下角 |
| Vector2D | anchors_max? | Vector(1, 1) | 最大锚点,从 (0, 0) 左上角到 (1, 1) 右下角 |
| Vector2D | alignment? | Vector(0.5, 0.5) | 控件的轴心点,(0.5, 0.5) 为中心 |
Moderate

SetSlotPadding
设置水平和垂直间距的插槽内边距。
注:该方法仅在此控件是以下任一控件的子级时才有效:HorizontalBoxSlot、VerticalBoxSlot、OverlaySlot、SizeBoxSlot、ScrollBoxSlot、BackgroundBlurSlot、BorderSlot、ButtonSlot、GridSlot。
my_widget:SetSlotPadding(padding_horizontal?, padding_vertical?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| Vector2D | padding_horizontal? | Vector2D(0, 0) | 插槽的水平内边距 |
| Vector2D | padding_vertical? | Vector2D(0, 0) | 插槽的垂直内边距 |
Moderate

SetSlotSize
设置插槽大小和尺寸规则。
注:该方法仅在此控件是以下任一控件的子级时才有效:HorizontalBoxSlot、VerticalBoxSlot、ScrollBoxSlot。
my_widget:SetSlotSize(size?, slate_size_rule?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| float | size? | 0.0 | 插槽的尺寸值 |
| WidgetSizeRule | slate_size_rule? | WidgetSizeRule.Automatic | 插槽的尺寸规则 |
Moderate

SetVisibility
设置其在屏幕上的可见性
my_widget:SetVisibility(visibility)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| WidgetVisibility | visibility | Required parameter | 屏幕中的新可见性状态 |
See also GetVisibility.
Slow

SpawnWidget3D
生成一个在世界中渲染此控件的 3D 控件 Actor
local widget_3D = my_widget:SpawnWidget3D(location?, rotation?, widget_space?, auto_size?, size?, auto_repaint_rate?, pivot?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| Vector | location? | Vector(0, 0, 0) | 世界位置 |
| Rotator | rotation? | Rotator(0, 0, 0) | 世界旋转 |
| WidgetSpace | widget_space? | WidgetSpace.World | 在世界空间中渲染还是始终面向屏幕 |
| boolean | auto_size? | false | 是否根据其内容自动调整尺寸 |
| Vector2D | size? | Vector2D(500, 500) | 仅在 auto_size = false 时生效 |
| float | auto_repaint_rate? | -1 | 保持 -1 以在每一帧都重新绘制 |
| Vector2D | pivot? | Vector2D(0.5, 0.5) | 控件的轴心点,(0.5, 0.5) 为中心 |
Returns
| Type | Description |
|---|---|
| Widget3D | 生成的 Widget3D |
Optimal

UnbindBlueprintEventDispatcher
解绑一个蓝图事件分发器
my_widget:UnbindBlueprintEventDispatcher(dispatcher_name, callback?)
Parameters
| Type | Parameter | Default | Description |
|---|---|---|---|
| string | dispatcher_name | Required parameter | 事件分发器名称 |
| function | callback? | nil | 可选的要解绑的回调函数 |
🚀 事件
Inherited Entity Events
Widget inherits from Base Entity Class, sharing it's events:
Base Entityscripting-reference/classes/base-classes/entity
Base Entityscripting-reference/classes/base-classes/entity
| Name | Description | |
|---|---|---|
ClassRegister | 当使用 继承系统 注册一个新类时触发 | |
Destroy | 当实体被销毁时触发 | |
Spawn | 当实体被生成/创建时触发 | |
ValueChange | 当实体通过 :SetValue() 改变了值时触发 |
This class doesn't have own events.
✅ NativeWidgets 与虚幻控件的关系
虚幻原生 Widget ↔ NativeWidget 枚举的对应关系列表:
| 枚举 | 虚幻类 | 是否为面板 |
|---|---|---|
NativeWidget.Border | Border | ✅ |
NativeWidget.Button | Button | ✅ |
NativeWidget.CheckBox | CheckBox | ✅ |
NativeWidget.Image | Image | ❌ |
NativeWidget.ProgressBar | ProgressBar | ❌ |
NativeWidget.RichTextBlock | RichTextBlock | ❌ |
NativeWidget.Slider | Slider | ❌ |
NativeWidget.Text | TextBlock | ❌ |
NativeWidget.ComboBox | ComboBox | ❌ |
NativeWidget.EditableText | EditableText | ❌ |
NativeWidget.EditableTextMultiLine | MultiLineEditableText | ❌ |
NativeWidget.SpinBox | SpinBox | ❌ |
NativeWidget.TextBox | EditableTextBox | ❌ |
NativeWidget.TextBoxMultiLine | MultiLineEditableTextBox | ❌ |
NativeWidget.CanvasPanel | CanvasPanel | ✅ |
NativeWidget.GridPanel | GridPanel | ✅ |
NativeWidget.HorizontalBox | HorizontalBox | ✅ |
NativeWidget.Overlay | Overlay | ✅ |
NativeWidget.ScaleBox | ScaleBox | ✅ |
NativeWidget.ScrollBox | ScrollBox | ✅ |
NativeWidget.SizeBox | SizeBox | ✅ |
NativeWidget.UniformGridPanel | UniformGridPanel | ✅ |
NativeWidget.VerticalBox | VerticalBox | ✅ |
NativeWidget.WrapBox | WrapBox | ✅ |
NativeWidget.BackgroundBlur | BackgroundBlur | ✅ |
✅ 支持的参数类型列表
请在蓝图页面查看所有支持的参数类型。




