为了在代码中创建UIGridLayout我缺少什么?

问题描述 投票:1回答:1

这是有关创建UIGridLayout的问题。我知道如何使用Studio菜单执行此操作,但是我尝试仅使用代码来执行此操作。这是我的代码:

local layout = Instance.new("UIGridLayout")
layout.Name = "UIGridLayout"
layout.Parent = script.Parent
layout.FillDirection = Enum.FillDirection.Horizontal
layout.CellPadding = UDim2.new(0, 100, 0, 5)
layout.CellSize = UDim2.new(0, 200, 0, 200)
layout.HorizontalAlignment = Enum.HorizontalAlignment.Center
layout.SortOrder = Enum.SortOrder.LayoutOrder
layout.StartCorner = Enum.StartCorner.TopLeft
layout.VerticalAlignment = Enum.VerticalAlignment.Bottom

local teamFrame = Instance.new("Frame")
teamFrame.BackgroundTransparency = 0
teamFrame.LayoutOrder = 0
teamFrame.Parent = script.Parent



local lblTeamTag = Instance.new("TextLabel", teamFrame)
lblTeamTag.TextTransparency = 0
lblTeamTag.TextStrokeTransparency = 0
lblTeamTag.Name = "TeamTag"
lblTeamTag.Text = "Team"
lblTeamTag.Size = UDim2.new(0, 200, 0, 50) 
lblTeamTag.Position = UDim2.new(0, 0, 0, 0)

local lblPoints = Instance.new("TextLabel", teamFrame)
lblPoints.TextStrokeTransparency = 0
lblPoints.BackgroundTransparency = 0
lblPoints.Name = "Points"
lblPoints.Text = "0"
lblPoints.Size = UDim2.new(0, 200, 0, 150)
lblPoints.Position = UDim2.new(0, 0, 0, 50)

此代码所在的脚本是ScreenGui的本地脚本子级。我希望看到这样的东西:

enter image description here

但是我什么也没得到。我知道我很近。我想念什么。非常感谢

user-interface lua grid local roblox
1个回答
0
投票

如果您得到的只是TextLabels不在正确的位置,那是因为UIGridLayout必须位于框架内,而不是位于框架的同一父级内。因此,如果在实例化框架之后将UIGridLayout实例化,然后将框架设为UIGridLayout的父框架,则它应该可以工作。这是应该执行的操作,因此您只需复制和粘贴即可:

local teamFrame = Instance.new("Frame")
teamFrame.BackgroundTransparency = 0
teamFrame.LayoutOrder = 0
teamFrame.Parent = script.Parent

local layout = Instance.new("UIGridLayout")
layout.Name = "UIGridLayout"
layout.Parent = script.Parent
layout.FillDirection = Enum.FillDirection.Horizontal
layout.CellPadding = UDim2.new(0, 100, 0, 5)
layout.CellSize = UDim2.new(0, 200, 0, 200)
layout.HorizontalAlignment = Enum.HorizontalAlignment.Center
layout.SortOrder = Enum.SortOrder.LayoutOrder
layout.StartCorner = Enum.StartCorner.TopLeft
layout.VerticalAlignment = Enum.VerticalAlignment.Bottom



local lblTeamTag = Instance.new("TextLabel", teamFrame)
lblTeamTag.TextTransparency = 0
lblTeamTag.TextStrokeTransparency = 0
lblTeamTag.Name = "TeamTag"
lblTeamTag.Text = "Team"
lblTeamTag.Size = UDim2.new(0, 200, 0, 50) 
lblTeamTag.Position = UDim2.new(0, 0, 0, 0)

local lblPoints = Instance.new("TextLabel", teamFrame)
lblPoints.TextStrokeTransparency = 0
lblPoints.BackgroundTransparency = 0
lblPoints.Name = "Points"
lblPoints.Text = "0"
lblPoints.Size = UDim2.new(0, 200, 0, 150)
lblPoints.Position = UDim2.new(0, 0, 0, 50)
© www.soinside.com 2019 - 2024. All rights reserved.