移动/游戏
在 Roblox 中构建、动画或调试 GUI(HUD、菜单…
适用于 Roblox 的 ScreenGui、SurfaceGui、BillboardGui 布局、缩放、动画、LocalScript 放置和常见错误修复。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 38/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 60/100 ×0.15
适用场景
在 Roblox 中构建、动画或调试 G特定
解决思路
- 根据 UI 类型选择容器:ScreenGui 的 Parent 是 PlayerGui,用于 HUD、菜单、overlays,始终面向屏幕。
- 根据 UI 类型选择容器:SurfaceGui 的 Parent 是 BasePart,用于部件表面的世界空间 UI(signs、screens)。
- 根据 UI 类型选择容器:BillboardGui 的 Parent 是 BasePart 或 Model,浮在 3D 空间中部件上方(name tags、health bars)。
- 在 StarterGui 或 StarterPlayerScripts 的 LocalScript 中,获取 game:GetService("Players").LocalPlayer 和 player:WaitForChild("PlayerGui")。
- 用 Instance.new("ScreenGui") 创建 ScreenGui,设置 Name=HUD,ResetOnSpawn=false,ZIndexBehavior=Enum.ZIndexBehavior.Sibling,Parent=playerGui。
- 创建 SurfaceGui:设置 Face=Enum.NormalId.Front,SizingMode=Enum.SurfaceGuiSizingMode.PixelsPerStud,PixelsPerStud=50,Parent=workspace.ScreenPart。
- 在 SurfaceGui 下创建 TextLabel:Size=UDim2.fromScale(1,1),Text=Hello World,Parent=surfaceGui。
- 创建 BillboardGui:Size=UDim2.fromOffset(200,50),StudsOffset=Vector3.new(0,2.5,0),AlwaysOnTop=false,Parent=character:WaitForChild("Head")。
- 在 BillboardGui 下创建 TextLabel:Size=UDim2.fromScale(1,1),BackgroundTransparency=1,Text=player.DisplayName,Parent=billboard。
- 使用 UDim2.new(xScale,xOffset,yScale,yOffset) 设置尺寸/位置,其中 scale 是 0–1 相对于父级,offset 是像素。
- 设置 frame.Size=UDim2.new(1,0,0,50) 实现全宽、50px 高。
- 设置 frame.Position=UDim2.new(0,0,0,0) 定位到左上角。
- 设置 frame.Size=UDim2.fromScale(0.6,0.4) 实现 60% 宽、40% 高。
- 设置 frame.Position=UDim2.new(0.2,0,0.3,0) 实现居中(0.2=(1-0.6)/2)。
- 使用 UDim2.fromScale(0.5,0.5) 只设置 scale。
- 使用 UDim2.fromOffset(300,150) 只设置像素。
- 设置 AnchorPoint 来移动元素轴心(每轴 0–1):frame.AnchorPoint=Vector2.new(0.5,0.5) 将轴心设在中心。
- 配合 frame.Position=UDim2.fromScale(0.5,0.5) 实现屏幕真正居中。
- 响应式设计优先使用 scale 而不是 offset,使 UI 适配所有屏幕尺寸。
- 设置 button.Size=UDim2.fromScale(0.2,0.07),button.Position=UDim2.new(0.4,0,0.85,0)。
- 用 UIAspectRatioConstraint 防止变形:创建 arc=Instance.new("UIAspectRatioConstraint"),设置 arc.AspectRatio=4(宽:高=4:1),arc.Parent=button。
- 获取 TweenService=game:GetService("TweenService")。
- 定义 tweenInfo=TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)。
- 设置 menuFrame=script.Parent。
- openMenu:TweenService:Create(menuFrame, tweenInfo, {Position=UDim2.new(0.05,0,0.1,0)}):Play()。
- closeMenu:TweenService:Create(menuFrame, tweenInfo, {Position=UDim2.new(-0.5,0,0.1,0)}):Play()。
- setProgress(bar,pct):TweenService:Create(bar, TweenInfo.new(0.2), {Size=UDim2.new(pct,0,1,0)}):Play()。
- LocalScript 放在 StarterGui:加入时克隆到 PlayerGui;用 ResetOnSpawn=false 持久化。
- LocalScript 放在 StarterPlayerScripts:只运行一次,重生不重置;适合持久管理器。
- LocalScript 放在 StarterCharacterScripts:每次重生重新运行;适合依赖角色的 UI。
- 安全等待角色:player=game:GetService("Players").LocalPlayer;character=player.Character or player.CharacterAdded:Wait();humanoid=character:WaitForChild("Humanoid")。
- 连接 humanoid.HealthChanged:Connect(function(health) -- update health bar end) 更新血条。
- 设置 screenGui.ResetOnSpawn=false 跨重生持久化(inventory、settings)。
- 设置 screenGui.ResetOnSpawn=true 在重生时重新创建(respawn timer)——默认。
- Full-screen overlay:Size=UDim2.fromScale(1,1),Position=UDim2.fromScale(0,0)。
- Bottom-center HUD bar:AnchorPoint=(0.5,1),Position=UDim2.new(0.5,0,1,-10)。
- Padded list:在 Frame 内使用 UIPadding + UIListLayout。
- Scrollable list:使用 ScrollingFrame + UIListLayout;根据 UIListLayout.AbsoluteContentSize 设置 CanvasSize。
- Rounded corners:UICorner 设置 CornerRadius=UDim.new(0,8)。
- Scaled text:在 TextLabel/TextButton 上设置 TextScaled=true,使字体随容器增长。
- Dynamic frame height:设置 AutomaticSize=Enum.AutomaticSize.Y,使 Frame 扩展以容纳子项。
- Health bar:嵌套 Frame:外层=背景,内层通过 Size.X.Scale 进行 tween。
- Name tag:在 Head 上使用 BillboardGui,StudsOffset=Vector3.new(0,2.5,0)。
- GUI 重生后消失:设置 ResetOnSpawn=false 或使用 StarterPlayerScripts。
- 移动端 UI 看起来不对:使用 UDim2.fromScale + UIAspectRatioConstraint。
- 脚本找不到 PlayerGui:使用 player:WaitForChild("PlayerGui")。
- Tween 不运行:确保属性可 tween;Text 不可,Position 和 Size 可以。
- BillboardGui 穿墙可见:确认 AlwaysOnTop=false。
- AbsoluteSize 第一帧为零:在 task.defer 内或首个渲染步骤后读取。
- 点击穿透重叠 Frame:添加透明的输入阻挡 Frame 或设置 Modal=true。
- SurfaceGui 闪烁:设置 LightInfluence=0;确保部件不太薄。
- 移动端文字太小:设置 TextScaled=true——固定 TextSize 不适应屏幕尺寸。
- 移动端难以测试 UI:使用 Studio 的 Device Emulator(Test 标签 → Device)预览布局。
适用边界 · 注意事项
- 不适用于非 Roblox 引擎或非 Luau 的 GUI 开发。
- 不适用于不使用 LocalScript/PlayerGui 生命周期的场景。
- 若无法使用 Roblox Studio 的 Device Emulator,移动端布局验证会受限。