主题
EUIAspectRatioConstraint
概览
| 字段 | 值 |
|---|---|
| Kind | Unit · 单位 Unit |
| Realm | common |
继承关系
- Unit(5 属性 / 28 函数 / 6 事件)
- [EUIAspectRatioConstraint](3 属性)
继承成员
1 个来源 / 5 属性 / 28 函数 / 6 事件
- 来自 Unit(5 属性 / 28 函数 / 6 事件)
- 属性:
AssetId、Name、Desc、Parent、Tags - 函数:
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、HasTag、AddTag、RemoveTag - 事件:
ChildAdded、ChildRemoved、DescendantAdded、DescendantRemoving、AncestryChanged、Destroying
- 属性:
界面宽高比约束节点用于给具体的界面控件附加固定宽高比规则,避免控件在父容器尺寸变化时被拉伸变形。它通过宽高比、适配方式与主轴三个属性共同决定控件尺寸如何被计算。创建后作为子节点挂在目标控件下参与布局计算。
适用场景
适合用在需要保持固定比例的界面元素上,例如头像框、图片、底板或按钮背景,当父容器尺寸或屏幕分辨率变化时仍维持既定比例。
使用要点
先在玩家 EUI 根节点下创建一个具体 EUI 控件(如 EUIImage、EUIButton 或 EUITextLabel),再调用 World:CreateUnit("EUIAspectRatioConstraint", { Parent = 目标控件, AspectRatio = 1.5 }) 将约束挂到该控件下,并按需配置 AspectRatio、AspectType 与 DominantAxis。
注意事项
EUIAspectRatioConstraint 不能直接挂载在 EUIRootNode 或 EUISceneNode 下,必须挂载在继承自 EUINodeBase 的具体 EUI 控件节点下,否则会抛出异常。AspectType 与 DominantAxis 须使用 Enums.EUIAspectType 与 Enums.EUIDominantAxis 枚举值。
代码示例
为 EUI 控件挂载宽高比约束
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 panel = World:CreateUnit("EUIImage", { Parent = rootNode, Position = Vector2.New(0, 0), Size = Vector2.New(200, 120) })
if panel == nil then return end
local constraint = World:CreateUnit("EUIAspectRatioConstraint", { Parent = panel, AspectRatio = 1.5, AspectType = Enums.EUIAspectType.FitWithinMaxSize, DominantAxis = Enums.EUIDominantAxis.Width })
if constraint == nil then return end
print("EUIAspectRatioConstraint 已创建:", constraint.Name, constraint.AspectRatio)属性 (3)
| Name | 类型 | 默认值 | 说明 |
|---|---|---|---|
AspectRatio | Float | 1.0 | 宽高比属性,类型为 Float,用于描述 EUIAspectRatioConstraint 约束所采用的宽高比数值,默认值为 1.0。 |
AspectType | EUIAspectType | Enums.EUIAspectType.FitWithinMaxSize | 适配方式属性,类型为 Enums.EUIAspectType 枚举,用于描述宽高比约束采用的适配方式,默认值为枚举第一项。 |
DominantAxis | EUIDominantAxis | Enums.EUIDominantAxis.Width | 主轴属性,类型为 Enums.EUIDominantAxis 枚举,用于描述宽高比约束所依据的主轴,默认值为枚举第一项。 |
