GuiObject
*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.
GuiObject là một lớp trừu tượng (tương tự như BasePart ) cho một đối tượng giao diện người dùng 2D.Nó xác định tất cả các thuộc tính liên quan đến việc hiển thị giao diện người dùng đồ họa (GUI) như Size và Position .Nó cũng có một số tính năng đọc chỉ một lần hữu ích như AbsolutePosition , AbsoluteSize , và AbsoluteRotation .
Để thao tác bố trí các đối tượng GUI theo cách đặc biệt, bạn có thể sử dụng một cấu trúc bố trí như danh sách/flex hoặc lưới , và bạn có thể tạo kiểu cho chúng vượt ra ngoài các tính chất cốt lõi của chúng thông qua modifier xuất hiện .
Mặc dù có thể phát hiện sự kiện nút chuột trên bất kỳ đối tượng GUI nào bằng cách sử dụng InputBegan và InputEnded, chỉ ImageButton và TextButton có các sự kiện dành riêng thuận tiện như Activated để phát hiện nhấp/nhấn.
Tóm Tắt
Thuộc Tính
Xác định xem yếu tố UI này có chìm nhập hay không.
Xác định điểm nguồn của một GuiObject , so với kích thước tuyệt đối của nó.
Xác định xem có thay đổi kích thước xảy ra dựa trên nội dung con không.
Xác định màu nền GuiObject .
Xác định độ trong suốt của nền và biên giới GuiObject .
Xác định màu của biên giới GuiObject .
Xác định theo cách nào biên giới GuiObject được xếp theo kích thước của nó.
Xác định chiều rộng pixel của biên giới GuiObject .
Xác định xem con cháu GuiObjects bên ngoài giới hạn của một yếu tố GUI cha có nên hiển thị hay không.
Xác định xem chuột của người chơi đang được nhấn chủ động trên GuiObject hay không.
Xác định liệu GuiButton có thể tương tác với nó hay không, hoặc nếu GuiState của GuiObject đang thay đổi hay không.
Kiểm soát thứ tự sắp xếp của GuiObject khi sử dụng với UIGridStyleLayout .
Bộ GuiObject được chọn khi công tắc gamepad được di chuyển xuống dưới.
Bộ GuiObject được chọn khi chọn gamepad bị di chuyển sang bên trái.
Bộ GuiObject được chọn khi chọn gamepad bị di chuyển sang bên phải.
Bộ GuiObject được chọn khi công tắc gamepad được di chuyển lên trên.
Xác định vị trí pixel và vectơ của GuiObject .
Xác định số độ mà GuiObject được xoay.
Xác định xem liệu GuiObject có thể được chọn bởi gamepad hay không.
Thay thế trang trí lựa chọn mặc định được sử dụng cho gamepad.
Thứ tự của GuiObjects được chọn bởi lựa chọn UI gamepad.
Xác định kích thước pixel và phương trình của GuiObject .
Đặt các trục Size mà GuiObject sẽ dựa trên, so với kích thước của cha của nó.
Một tính chất hỗn hợp của BackgroundTransparency và TextTransparency .
Xác định xem GuiObject và con cháu của nó sẽ được hiển thị hay không.
Xác định thứ tự mà một GuiObject render so với những người khác.
Mô tả vị trí màn hình thực của một thành phần GuiBase2d , trong điểm ảnh.
Mô tả sự xoay màn hình thực sự của một thành phần GuiBase2d , trong độ.
Mô tả kích thước màn hình thực của một thành phần GuiBase2d , bằng像素.
Khi được đặt thành true , bản địa hóa sẽ được áp dụng cho GuiBase2d và con cháu của nó.
Một tham chiếu đến một LocalizationTable để được sử dụng để áp dụng lok hóa tự động cho GuiBase2d và con cháu của nó.
Tùy chỉnh hành vi lựa chọn gamepad theo chiều xuống.
Tùy chỉnh hành vi lựa chọn gamepad theo hướng bên trái.
Tùy chỉnh hành vi lựa chọn gamepad theo hướng bên phải.
Tùy chỉnh hành vi lựa chọn gamepad trong hướng lên.
Cho phép tùy chỉnh chuyển động lựa chọn gamepad.
Phương Pháp
- TweenPosition(endPosition : UDim2,easingDirection : Enum.EasingDirection,easingStyle : Enum.EasingStyle,time : number,override : boolean,callback : function):boolean
Di chuyển một GUI mượt mà sang một mới UDim2 .
- TweenSize(endSize : UDim2,easingDirection : Enum.EasingDirection,easingStyle : Enum.EasingStyle,time : number,override : boolean,callback : function):boolean
- TweenSizeAndPosition(endSize : UDim2,endPosition : UDim2,easingDirection : Enum.EasingDirection,easingStyle : Enum.EasingStyle,time : number,override : boolean,callback : function):boolean
Di chuyển một GUI mượt mà sang một kích cỡ và vị trí mới.
Sự Kiện
Bị sa thải khi người dùng bắt đầu tương tác thông qua thiết bị Giao diện Con người - Máy tính ( nút chuột xuống, chạm bắt đầu, nút bàn phím xuống, v.v.).
Bị sa thải khi người dùng thay đổi cách họ tương tác thông qua thiết bị Giao diện Con người - Máy tính ( nút chuột xuống, chạm bắt đầu, nút bàn phím xuống, v.v.).
Bị sa thả khi người dùng ngừng tương tác thông qua thiết bị Giao diện Con người - Máy tính ( nút chuột xuống, chạm bắt đầu, nút bàn phím xuống, v.v.).
Bắt lửa khi người dùng di chuyển con trỏ vào một thành phần GUI.
Bắt lửa khi người dùng di chuyển con trỏ ra khỏi một thành phần GUI.
Bắt lửa mỗi khi người dùng di chuyển con trỏ trong khi nó nằm bên trong thành phần GUI.
Bắt lửa khi người dùng cuộn bánh xe chuột trở lại khi chuột ở trên một thành phần GUI.
Bắt lửa khi một người dùng cuộn bánh xoay chuột của họ về phía trước khi chuột ở trên một thành phần GUI.
Bị sa thả khi GuiObject được tập trung vào với lựa chọn Gamepad.
Bị sa thả khi lựa chọn Gamepad ngừng tập trung vào GuiObject.
Bắt lửa khi người chơi bắt đầu, tiếp tục và dừng việc giữ lâu UI.
- TouchPan(touchPositions : Array,totalTranslation : Vector2,velocity : Vector2,state : Enum.UserInputState):RBXScriptSignal
Bắt lửa khi người chơi di chuyển ngón tay trên thành phần UI.
- TouchPinch(touchPositions : Array,scale : number,velocity : number,state : Enum.UserInputState):RBXScriptSignal
Bắt lửa khi người chơi thực hiện cử chỉ kéo hoặc kéo bằng hai ngón tay trên thành phần UI.
- TouchRotate(touchPositions : Array,rotation : number,velocity : number,state : Enum.UserInputState):RBXScriptSignal
Bắt lửa khi người chơi thực hiện cử chỉ xoay bằng hai ngón tay trên thành phần UI.
Bắt lửa khi người chơi thực hiện cử chỉ vuốt trên thành phần UI.
Bắt lửa khi người chơi thực hiện một cử chỉ vuốt trên thành phần UI.
- SelectionChanged(amISelected : boolean,previousSelection : GuiObject,newSelection : GuiObject):RBXScriptSignal
Bắt lửa khi lựa chọn gamepad di chuyển đến, rời hoặc thay đổi trong khu vực kết nối GuiBase2d hoặc bất kỳ con trai nào GuiObjects .
Thuộc Tính
Active
Tính chất này xác định xem GuiObject sẽ chìm đầu vào vào không gian 3D, chẳng hạn như các mô hình cơ bản với lớp ClickDetector như DragDetector .
Đối với các đối tượng GuiButton ( ImageButton và TextButton ), tính năng này xác định xem có bắt lửa Activated ( AutoButtonColor vẫn sẽ hoạt động đối với những người như vậy).Các sự kiện InputBegan, InputChanged và InputEnded hoạt động bình thường bất kể giá trị của thuộc tính này.
Mẫu mã
This code sample demonstrates the usage of the Active property as a debounce for the Activated event.
-- Place this LocalScript within a TextButton (or ImageButton)
local textButton = script.Parent
textButton.Text = "Click me"
textButton.Active = true
local function onActivated()
-- This acts like a debounce
textButton.Active = false
-- Count backwards from 5
for i = 5, 1, -1 do
textButton.Text = "Time: " .. i
task.wait(1)
end
textButton.Text = "Click me"
textButton.Active = true
end
textButton.Activated:Connect(onActivated)
AnchorPoint
Thuộc tính này xác định điểm nguồn của một GuiObject , so với kích thước tuyệt đối của nó.Điểm nguồn xác định từ nơi mà thành phần được đặt (thông qua GuiObject.Position ) và từ đó mà thành phần được hiển thị GuiObject.Size mở rộng.
Xem ở đây để có các bản minh họa và chi tiết.
Mẫu mã
This code sample moves a UI element to different sides of the parent element. It starts at the top-left and ends at the bottom-right. Paste into a LocalScript in a Frame, within a ScreenGui.
local guiObject = script.Parent
while true do
-- Top-left
guiObject.AnchorPoint = Vector2.new(0, 0)
guiObject.Position = UDim2.new(0, 0, 0, 0)
task.wait(1)
-- Top
guiObject.AnchorPoint = Vector2.new(0.5, 0)
guiObject.Position = UDim2.new(0.5, 0, 0, 0)
task.wait(1)
-- Top-right
guiObject.AnchorPoint = Vector2.new(1, 0)
guiObject.Position = UDim2.new(1, 0, 0, 0)
task.wait(1)
-- Left
guiObject.AnchorPoint = Vector2.new(0, 0.5)
guiObject.Position = UDim2.new(0, 0, 0.5, 0)
task.wait(1)
-- Dead center
guiObject.AnchorPoint = Vector2.new(0.5, 0.5)
guiObject.Position = UDim2.new(0.5, 0, 0.5, 0)
task.wait(1)
-- Right
guiObject.AnchorPoint = Vector2.new(1, 0.5)
guiObject.Position = UDim2.new(1, 0, 0.5, 0)
task.wait(1)
-- Bottom-left
guiObject.AnchorPoint = Vector2.new(0, 1)
guiObject.Position = UDim2.new(0, 0, 1, 0)
task.wait(1)
-- Bottom
guiObject.AnchorPoint = Vector2.new(0.5, 1)
guiObject.Position = UDim2.new(0.5, 0, 1, 0)
task.wait(1)
-- Bottom-right
guiObject.AnchorPoint = Vector2.new(1, 1)
guiObject.Position = UDim2.new(1, 0, 1, 0)
task.wait(1)
end
AutomaticSize
Tính năng này được sử dụng để tự động thay đổi kích thước các đối tượng UI cha dựa trên kích thước của con trai của nó.Bạn có thể sử dụng thuộc tính này để thêm nội dung và nội dung khác vào một đối tượng UI tại thời gian biên tập hoặc chạy, và kích thước sẽ điều chỉnh để phù hợp với nội dung đó.
Khi AutomaticSize được đặt thành một giá trị Enum.AutomaticSize cho bất cứ thứ gì khác ngoài None, đối tượng UI này có thể thay đổi kích thước tùy thuộc vào nội dung con của nó.
Để biết thêm thông tin về cách sử dụng tính năng này và cách nó hoạt động, xem ở đây .
Mẫu mã
The following script creates an automatically-sized parent frame with aUIListLayout, then it inserts several automatically-sized TextLabel objects. Note how the parent UIListLayout automatically resizes to fit its child content and the labels automatically resize to fit their text content. This script can be parented to a ScreenGui.
-- Array of text labels/fonts/sizes to output
local labelArray = {
{ text = "Lorem", font = Enum.Font.Creepster, size = 50 },
{ text = "ipsum", font = Enum.Font.IndieFlower, size = 35 },
{ text = "dolor", font = Enum.Font.Antique, size = 55 },
{ text = "sit", font = Enum.Font.SpecialElite, size = 65 },
{ text = "amet", font = Enum.Font.FredokaOne, size = 40 },
}
-- Create an automatically-sized parent frame
local parentFrame = Instance.new("Frame")
parentFrame.AutomaticSize = Enum.AutomaticSize.XY
parentFrame.BackgroundColor3 = Color3.fromRGB(90, 90, 90)
parentFrame.Size = UDim2.fromOffset(25, 100)
parentFrame.Position = UDim2.fromScale(0.1, 0.1)
parentFrame.Parent = script.Parent
-- Add a list layout
local listLayout = Instance.new("UIListLayout")
listLayout.Padding = UDim.new(0, 5)
listLayout.Parent = parentFrame
-- Set rounded corners and padding for visual aesthetics
local roundedCornerParent = Instance.new("UICorner")
roundedCornerParent.Parent = parentFrame
local uiPaddingParent = Instance.new("UIPadding")
uiPaddingParent.PaddingTop = UDim.new(0, 5)
uiPaddingParent.PaddingLeft = UDim.new(0, 5)
uiPaddingParent.PaddingRight = UDim.new(0, 5)
uiPaddingParent.PaddingBottom = UDim.new(0, 5)
uiPaddingParent.Parent = parentFrame
for i = 1, #labelArray do
-- Create an automatically-sized text label from array
local childLabel = Instance.new("TextLabel")
childLabel.AutomaticSize = Enum.AutomaticSize.XY
childLabel.Size = UDim2.fromOffset(75, 15)
childLabel.Text = labelArray[i]["text"]
childLabel.Font = labelArray[i]["font"]
childLabel.TextSize = labelArray[i]["size"]
childLabel.TextColor3 = Color3.new(1, 1, 1)
childLabel.Parent = parentFrame
-- Visual aesthetics
local roundedCorner = Instance.new("UICorner")
roundedCorner.Parent = childLabel
local uiPadding = Instance.new("UIPadding")
uiPadding.PaddingTop = UDim.new(0, 5)
uiPadding.PaddingLeft = UDim.new(0, 5)
uiPadding.PaddingRight = UDim.new(0, 5)
uiPadding.PaddingBottom = UDim.new(0, 5)
uiPadding.Parent = childLabel
task.wait(2)
end
BackgroundColor3
Tính chất này xác định màu của một nền GuiObject nền (màu điền).Nếu yếu tố của bạn có chứa văn bản, chẳng hạn như TextBox , TextButton , hoặc TextLabel , hãy đảm bảo rằng màu nền của bạn tương phản với màu của văn bản.
Một tính chất khác xác định các thuộc tính hình ảnh của nền là GuiObject.BackgroundTransparency ; nếu được đặt thành 1 , cả nền và biên giới sẽ không được hiển thị.
Xem thêm BorderColor3 .
Mẫu mã
This code sample causes a parent Frame to loop through all colors of the rainbow using Color3.fromHSV.
-- Put this code in a LocalScript in a Frame
local frame = script.Parent
while true do
for hue = 0, 255, 4 do
-- HSV = hue, saturation, value
-- If we loop from 0 to 1 repeatedly, we get a rainbow!
frame.BorderColor3 = Color3.fromHSV(hue / 256, 1, 1)
frame.BackgroundColor3 = Color3.fromHSV(hue / 256, 0.5, 0.8)
task.wait()
end
end
BackgroundTransparency
Tính chất này xác định độ trong suốt của nền và biên giới GuiObject .Tuy nhiên, nó không xác định độ trong suốt của văn bản nếu GUI là một TextBox , TextButton , hoặc TextLabel ; độ trong suốt của văn bản được xác định TextBox.TextTransparency , TextButton.TextTransparency , và TextLabel.TextTransparency lần lượt.
Nếu thuộc tính này được đặt thành 1, cả nền và biên giới sẽ không hiển thị và nền GUI sẽ hoàn toàn trong suốt.
BorderColor3
Xác định màu của biên giới hình chữ nhật GuiObject (cũng được gọi là màu của đường viền).Điều này khác với đối tượng của GuiObject.BackgroundColor3 .Bạn sẽ không thể nhìn thấy ranh giới của đối tượng nếu thuộc tính GuiObject.BorderSizePixel của nó được đặt thành 0 .
Lưu ý rằng thành phần UIStroke cho phép các hiệu ứng biên giới tiên tiến hơn.
Mẫu mã
This code sample causes the border of a parent GuiObject to highlight when the user hovers their mouse over the element.
-- Put me inside some GuiObject, preferrably an ImageButton/TextButton
local button = script.Parent
local function onEnter()
button.BorderSizePixel = 2
button.BorderColor3 = Color3.new(1, 1, 0) -- Yellow
end
local function onLeave()
button.BorderSizePixel = 1
button.BorderColor3 = Color3.new(0, 0, 0) -- Black
end
-- Connect events
button.MouseEnter:Connect(onEnter)
button.MouseLeave:Connect(onLeave)
-- Our default state is "not hovered"
onLeave()
BorderMode
Thuộc tính này xác định theo cách nào biên giới GuiObject được xếp theo kích thước của nó bằng cách sử dụng danh sách các tên tương tự, Enum.BorderMode .
Lưu ý rằng UIStroke có thể ghi đè tính chất này và cho phép các hiệu ứng biên giới tiên tiến hơn.
BorderSizePixel
Thuộc tính này xác định rộng bao nhiêu biên giới GuiObject được hiển thị, bằng像素. Đặt thuộc tính này thành 0 vô hiệu hóa biên giới hoàn toàn.
Lưu ý rằng UIStroke có thể ghi đè tính chất này và cho phép các hiệu ứng biên giới tiên tiến hơn.
Mẫu mã
This code sample causes the border of a parent GuiObject to highlight when the user hovers their mouse over the element.
-- Put me inside some GuiObject, preferrably an ImageButton/TextButton
local button = script.Parent
local function onEnter()
button.BorderSizePixel = 2
button.BorderColor3 = Color3.new(1, 1, 0) -- Yellow
end
local function onLeave()
button.BorderSizePixel = 1
button.BorderColor3 = Color3.new(0, 0, 0) -- Black
end
-- Connect events
button.MouseEnter:Connect(onEnter)
button.MouseLeave:Connect(onLeave)
-- Our default state is "not hovered"
onLeave()
ClipsDescendants
Tính chất này xác định xem GuiObject có cắt (làm cho vô hình) bất kỳ phần nào của các thành phần GUI con trai không khác sẽ hiển thị bên ngoài giới hạn của hình chữ nhật hay không.
Lưu ý rằng Rotation không được hỗ trợ bởi tính năng này.Nếu GUI này hoặc bất kỳ tổ tiên nào có không phải là không , thuộc tính này sẽ bị bỏ qua và các thành phần GUI con sẽ được hiển thị dù không có giá trị của thuộc tính này.
GuiState
Khi ngón tay của người chơi được nhấn và giữ trên GuiObject , GuiState của GuiObject sẽ được đặt thành Press .Tương tự, khi ngón tay của người chơi được giải phóng khỏi GuiObject , GuiState của GuiObject sẽ được đặt thành Idle , và khi Interactable bị tắt trên GuiObject , Class.GuiState của GuiObject sẽ được đặt thành NonInteractable .
Interactable
Xác định liệu GuiButton có thể tương tác với nó hay không, hoặc nếu GuiState của GuiObject đang thay đổi hay không.
Trên một GuiButton :
- Khi cài đặt Interactable trên GuiButton được đặt thành false , GuiButton sẽ không còn có thể được nhấn hoặc nhấp vào, và GuiState sẽ được đặt liên tục thành NonInteractable .
- Khi cài đặt Interactable trên GuiButton được đặt lại thành true , GuiButton sẽ hành xử bình thường trở lại và GuiState sẽ hành xử bình thường.
Trên một GuiObject :
- Khi cài đặt Interactable trên GuiButton được đặt thành false , GuiState sẽ được thiết lập liên tục thành NonInteractable .
- Khi cài đặt Interactable trên GuiButton được đặt lại thành true, GuiState sẽ hành xử bình thường trở lại.
LayoutOrder
Tính năng này kiểm soát thứ tự sắp xếp của GuiObject khi sử dụng UIGridStyleLayout (như UIListLayout hoặc UIPageLayout ) với SortOrder được đặt thành Enum.SortOrder.LayoutOrder .Nó không có chức năng nếu đối tượng không có cấu trúc bố trí UI anh em.
GuiObjects được sắp xếp theo thứ tự tăng dần nơi các giá trị thấp hơn có ưu tiên hơn các giá trị cao hơn.Các đối tượng có giá trị tương tự rơi về lịch trình mà chúng được thêm vào.
Nếu bạn không chắc chắn liệu bạn có cần thêm một yếu tố giữa hai yếu tố hiện có trong tương lai hay không, thì thực hành tốt là sử dụng nhân số của 100 ( 0 , 100 , 200 , v.v.).Điều này đảm bảo một khoảng cách lớn về giá trị trật tự bố trí mà bạn có thể sử dụng cho các yếu tố được đặt giữa các yếu tố khác.
Xem thêm ZIndex định vị trật tự render của đối tượng thay vì trật tự sắp xếp.
NextSelectionDown
Thuộc tính này đặt GuiObject được chọn khi người dùng di chuyển nút chọn gamepad xuống.Nếu thuộc tính này trống, di chuyển gamepad xuống sẽ không thay đổi GUI được chọn.
Di chuyển công tắc chọn gamepad xuống dưới đặt GuiService.SelectedObject vào đối tượng này trừ khi GUI không phải là Selectable .Lưu ý rằng thuộc tính này có thể được đặt cho một thành phần GUI ngay cả khi nó không phải là Selectable, vì vậy bạn nên đảm bảo rằng giá trị của một thuộc tính GUI có thể chọn phù hợp với hành vi mong đợi của bạn.
Xem thêm NextSelectionUp , NextSelectionLeft , và NextSelectionRight .
Mẫu mã
This example demonstrates how to enable Gamepad navigation through a grid of GuiObject|GUI elements without manually having to connect the GuiObject.NextSelectionUp, GuiObject.NextSelectionDown, and GuiObject|NextSelectionRight, and GuiObject.NextSelectionLeft properties for every element in the grid.
Note that this code sample assumes your UIGridLayout is sorted by name, where elements are named in successive numerical order.
The code relies on this to set the NextSelection properties for all GuiObjects in the same level as the UIGridLayout. In our example, the UIGridLayoutObject and GUI elements within the grid are all children of a Frame named "Container". The code gets the children of "Container" and loops through each child. Children that are not GuiObjects are ignored. For each GUI element, the code attempts to assigned the NextSelection properties using the following logic:
- Starting with 1, the name of all GUI elements match their position in the grid
- Left: The item to the left will always be numbered 1 less than the current element
- Right: The item to the left will always be numbered 1 more than the current element
- Up: The item above (up) will always be number of GUIs in a row 1 less than the current element
- Down: The item below (down) will always be the number of GUIs in a row more than the current element This logic also allows for the GUI elements at the begging and end of rows (excluding the first and last element) to wrap around to the next and previous rows. If an element doesn't exist to the left, right, up, or down, the NextSelection will remain nil and moving the Gamepad selector in the direction will not change the selected GUI.
This example also contains code to test the grid using the arrow keys (Up, Down, Left, Right) of your keyboard instead of a gamepad, just in case you don't have a gamepad to test with. This portion of code initially selects the element named "1" by assigning it to the GuiService.SelectedObject property.
-- Setup the Gamepad selection grid using the code below
local container = script.Parent:FindFirstChild("Container")
local grid = container:GetChildren()
local rowSize = container:FindFirstChild("UIGridLayout").FillDirectionMaxCells
for _, gui in pairs(grid) do
if gui:IsA("GuiObject") then
local pos = gui.Name
-- Left edge
gui.NextSelectionLeft = container:FindFirstChild(pos - 1)
-- Right edge
gui.NextSelectionRight = container:FindFirstChild(pos + 1)
-- Above
gui.NextSelectionUp = container:FindFirstChild(pos - rowSize)
-- Below
gui.NextSelectionDown = container:FindFirstChild(pos + rowSize)
end
end
-- Test the Gamepad selection grid using the code below
local GuiService = game:GetService("GuiService")
local UserInputService = game:GetService("UserInputService")
GuiService.SelectedObject = container:FindFirstChild("1")
function updateSelection(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
local key = input.KeyCode
local selectedObject = GuiService.SelectedObject
if not selectedObject then
return
end
if key == Enum.KeyCode.Up then
if not selectedObject.NextSelectionUp then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Down then
if not selectedObject.NextSelectionDown then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Left then
if not selectedObject.NextSelectionLeft then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Right then
if not selectedObject.NextSelectionRight then
GuiService.SelectedObject = selectedObject
end
end
end
end
UserInputService.InputBegan:Connect(updateSelection)
NextSelectionLeft
Thuộc tính này đặt GuiObject được chọn khi người dùng di chuyển chọn gamepad sang bên trái.Nếu thuộc tính này trống, di chuyển gamepad sang bên trái sẽ không thay đổi GUI được chọn.
Di chuyển công tắc chọn gamepad sang bên trái đặt GuiService.SelectedObject vào đối tượng này trừ khi GUI không phải là Selectable .Lưu ý rằng thuộc tính này có thể được đặt cho một thành phần GUI ngay cả khi nó không phải là Selectable, vì vậy bạn nên đảm bảo rằng giá trị của một thuộc tính GUI có thể chọn phù hợp với hành vi mong đợi của bạn.
Xem thêm NextSelectionUp , NextSelectionDown , và NextSelectionRight .
Mẫu mã
This example demonstrates how to enable Gamepad navigation through a grid of GuiObject|GUI elements without manually having to connect the GuiObject.NextSelectionUp, GuiObject.NextSelectionDown, and GuiObject|NextSelectionRight, and GuiObject.NextSelectionLeft properties for every element in the grid.
Note that this code sample assumes your UIGridLayout is sorted by name, where elements are named in successive numerical order.
The code relies on this to set the NextSelection properties for all GuiObjects in the same level as the UIGridLayout. In our example, the UIGridLayoutObject and GUI elements within the grid are all children of a Frame named "Container". The code gets the children of "Container" and loops through each child. Children that are not GuiObjects are ignored. For each GUI element, the code attempts to assigned the NextSelection properties using the following logic:
- Starting with 1, the name of all GUI elements match their position in the grid
- Left: The item to the left will always be numbered 1 less than the current element
- Right: The item to the left will always be numbered 1 more than the current element
- Up: The item above (up) will always be number of GUIs in a row 1 less than the current element
- Down: The item below (down) will always be the number of GUIs in a row more than the current element This logic also allows for the GUI elements at the begging and end of rows (excluding the first and last element) to wrap around to the next and previous rows. If an element doesn't exist to the left, right, up, or down, the NextSelection will remain nil and moving the Gamepad selector in the direction will not change the selected GUI.
This example also contains code to test the grid using the arrow keys (Up, Down, Left, Right) of your keyboard instead of a gamepad, just in case you don't have a gamepad to test with. This portion of code initially selects the element named "1" by assigning it to the GuiService.SelectedObject property.
-- Setup the Gamepad selection grid using the code below
local container = script.Parent:FindFirstChild("Container")
local grid = container:GetChildren()
local rowSize = container:FindFirstChild("UIGridLayout").FillDirectionMaxCells
for _, gui in pairs(grid) do
if gui:IsA("GuiObject") then
local pos = gui.Name
-- Left edge
gui.NextSelectionLeft = container:FindFirstChild(pos - 1)
-- Right edge
gui.NextSelectionRight = container:FindFirstChild(pos + 1)
-- Above
gui.NextSelectionUp = container:FindFirstChild(pos - rowSize)
-- Below
gui.NextSelectionDown = container:FindFirstChild(pos + rowSize)
end
end
-- Test the Gamepad selection grid using the code below
local GuiService = game:GetService("GuiService")
local UserInputService = game:GetService("UserInputService")
GuiService.SelectedObject = container:FindFirstChild("1")
function updateSelection(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
local key = input.KeyCode
local selectedObject = GuiService.SelectedObject
if not selectedObject then
return
end
if key == Enum.KeyCode.Up then
if not selectedObject.NextSelectionUp then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Down then
if not selectedObject.NextSelectionDown then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Left then
if not selectedObject.NextSelectionLeft then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Right then
if not selectedObject.NextSelectionRight then
GuiService.SelectedObject = selectedObject
end
end
end
end
UserInputService.InputBegan:Connect(updateSelection)
NextSelectionRight
Thuộc tính này đặt GuiObject được chọn khi người dùng di chuyển chọn gamepad sang bên phải.Nếu thuộc tính này trống, di chuyển gamepad sang bên phải sẽ không thay đổi GUI được chọn.
Di chuyển công tắc chọn gamepad sang bộ bên phải thiết lập GuiService.SelectedObject cho đối tượng này trừ khi GUI không phải là Selectable .Lưu ý rằng thuộc tính này có thể được đặt cho một thành phần GUI ngay cả khi nó không phải là Selectable, vì vậy bạn nên đảm bảo rằng giá trị của một thuộc tính GUI có thể chọn phù hợp với hành vi mong đợi của bạn.
Xem thêm NextSelectionUp , NextSelectionDown , và NextSelectionLeft .
Mẫu mã
This example demonstrates how to enable Gamepad navigation through a grid of GuiObject|GUI elements without manually having to connect the GuiObject.NextSelectionUp, GuiObject.NextSelectionDown, and GuiObject|NextSelectionRight, and GuiObject.NextSelectionLeft properties for every element in the grid.
Note that this code sample assumes your UIGridLayout is sorted by name, where elements are named in successive numerical order.
The code relies on this to set the NextSelection properties for all GuiObjects in the same level as the UIGridLayout. In our example, the UIGridLayoutObject and GUI elements within the grid are all children of a Frame named "Container". The code gets the children of "Container" and loops through each child. Children that are not GuiObjects are ignored. For each GUI element, the code attempts to assigned the NextSelection properties using the following logic:
- Starting with 1, the name of all GUI elements match their position in the grid
- Left: The item to the left will always be numbered 1 less than the current element
- Right: The item to the left will always be numbered 1 more than the current element
- Up: The item above (up) will always be number of GUIs in a row 1 less than the current element
- Down: The item below (down) will always be the number of GUIs in a row more than the current element This logic also allows for the GUI elements at the begging and end of rows (excluding the first and last element) to wrap around to the next and previous rows. If an element doesn't exist to the left, right, up, or down, the NextSelection will remain nil and moving the Gamepad selector in the direction will not change the selected GUI.
This example also contains code to test the grid using the arrow keys (Up, Down, Left, Right) of your keyboard instead of a gamepad, just in case you don't have a gamepad to test with. This portion of code initially selects the element named "1" by assigning it to the GuiService.SelectedObject property.
-- Setup the Gamepad selection grid using the code below
local container = script.Parent:FindFirstChild("Container")
local grid = container:GetChildren()
local rowSize = container:FindFirstChild("UIGridLayout").FillDirectionMaxCells
for _, gui in pairs(grid) do
if gui:IsA("GuiObject") then
local pos = gui.Name
-- Left edge
gui.NextSelectionLeft = container:FindFirstChild(pos - 1)
-- Right edge
gui.NextSelectionRight = container:FindFirstChild(pos + 1)
-- Above
gui.NextSelectionUp = container:FindFirstChild(pos - rowSize)
-- Below
gui.NextSelectionDown = container:FindFirstChild(pos + rowSize)
end
end
-- Test the Gamepad selection grid using the code below
local GuiService = game:GetService("GuiService")
local UserInputService = game:GetService("UserInputService")
GuiService.SelectedObject = container:FindFirstChild("1")
function updateSelection(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
local key = input.KeyCode
local selectedObject = GuiService.SelectedObject
if not selectedObject then
return
end
if key == Enum.KeyCode.Up then
if not selectedObject.NextSelectionUp then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Down then
if not selectedObject.NextSelectionDown then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Left then
if not selectedObject.NextSelectionLeft then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Right then
if not selectedObject.NextSelectionRight then
GuiService.SelectedObject = selectedObject
end
end
end
end
UserInputService.InputBegan:Connect(updateSelection)
NextSelectionUp
Thuộc tính này đặt GuiObject được chọn khi người dùng di chuyển nút chọn gamepad lên trên.Nếu thuộc tính này trống, di chuyển gamepad lên trên sẽ không thay đổi GUI được chọn.
Di chuyển công tắc chọn gamepad lên trên đặt GuiService.SelectedObject vào đối tượng này trừ khi GUI không phải là Selectable .Lưu ý rằng thuộc tính này có thể được đặt cho một thành phần GUI ngay cả khi nó không phải là Selectable, vì vậy bạn nên đảm bảo rằng giá trị của một thuộc tính GUI có thể chọn phù hợp với hành vi mong đợi của bạn.
Xem thêm NextSelectionDown , NextSelectionLeft , NextSelectionRight .
Mẫu mã
This example demonstrates how to enable Gamepad navigation through a grid of GuiObject|GUI elements without manually having to connect the GuiObject.NextSelectionUp, GuiObject.NextSelectionDown, and GuiObject|NextSelectionRight, and GuiObject.NextSelectionLeft properties for every element in the grid.
Note that this code sample assumes your UIGridLayout is sorted by name, where elements are named in successive numerical order.
The code relies on this to set the NextSelection properties for all GuiObjects in the same level as the UIGridLayout. In our example, the UIGridLayoutObject and GUI elements within the grid are all children of a Frame named "Container". The code gets the children of "Container" and loops through each child. Children that are not GuiObjects are ignored. For each GUI element, the code attempts to assigned the NextSelection properties using the following logic:
- Starting with 1, the name of all GUI elements match their position in the grid
- Left: The item to the left will always be numbered 1 less than the current element
- Right: The item to the left will always be numbered 1 more than the current element
- Up: The item above (up) will always be number of GUIs in a row 1 less than the current element
- Down: The item below (down) will always be the number of GUIs in a row more than the current element This logic also allows for the GUI elements at the begging and end of rows (excluding the first and last element) to wrap around to the next and previous rows. If an element doesn't exist to the left, right, up, or down, the NextSelection will remain nil and moving the Gamepad selector in the direction will not change the selected GUI.
This example also contains code to test the grid using the arrow keys (Up, Down, Left, Right) of your keyboard instead of a gamepad, just in case you don't have a gamepad to test with. This portion of code initially selects the element named "1" by assigning it to the GuiService.SelectedObject property.
-- Setup the Gamepad selection grid using the code below
local container = script.Parent:FindFirstChild("Container")
local grid = container:GetChildren()
local rowSize = container:FindFirstChild("UIGridLayout").FillDirectionMaxCells
for _, gui in pairs(grid) do
if gui:IsA("GuiObject") then
local pos = gui.Name
-- Left edge
gui.NextSelectionLeft = container:FindFirstChild(pos - 1)
-- Right edge
gui.NextSelectionRight = container:FindFirstChild(pos + 1)
-- Above
gui.NextSelectionUp = container:FindFirstChild(pos - rowSize)
-- Below
gui.NextSelectionDown = container:FindFirstChild(pos + rowSize)
end
end
-- Test the Gamepad selection grid using the code below
local GuiService = game:GetService("GuiService")
local UserInputService = game:GetService("UserInputService")
GuiService.SelectedObject = container:FindFirstChild("1")
function updateSelection(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
local key = input.KeyCode
local selectedObject = GuiService.SelectedObject
if not selectedObject then
return
end
if key == Enum.KeyCode.Up then
if not selectedObject.NextSelectionUp then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Down then
if not selectedObject.NextSelectionDown then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Left then
if not selectedObject.NextSelectionLeft then
GuiService.SelectedObject = selectedObject
end
elseif key == Enum.KeyCode.Right then
if not selectedObject.NextSelectionRight then
GuiService.SelectedObject = selectedObject
end
end
end
end
UserInputService.InputBegan:Connect(updateSelection)
Position
Thuộc tính này xác định điểm ảnh GuiObject và vị trí tuyến tính bằng cách sử dụng UDim2 . Vị trí được xác định xung quanh đối tượng GuiObject.AnchorPoint .
Vị trí scalar là tương đối với kích thước của thành phần GUI cha, nếu có.
Các phần pixel của giá trị UDim2 không phụ thuộc vào kích thước của GUI cha.Các giá trị đại diện cho vị trí của đối tượng bằng像素.Vị trí pixel thực của một đối tượng có thể được đọc từ thuộc tính GuiBase2d.AbsolutePosition.
Rotation
Tính chất này xác định số độ mà GuiObject được xoay theo.Vòng xoay là tương quan đến trung tâm của đối tượng, không điểm , có nghĩa là bạn không thể thay đổi điểm xoay.Ngoài ra, thuộc tính này không tương thích với ClipsDescendants .
Selectable
Thuộc tính này xác định xem liệu GuiObject có thể được chọn khi điều hướng GUI bằng gamepad hay không.
Nếu thuộc tính này là true, một GUI có thể được chọn. Việc chọn một GUI cũng đặt thuộc tính GuiService.SelectedObject vào đối tượng đó.
Khi điều này là false, GUI không thể được chọn.Tuy nhiên, đặt điều này thành false khi một GUI được chọn sẽ không bỏ chọn nó hoặc thay đổi giá trị của thuộc tính GuiService.SelectedObject.
Thêm GuiObject.SelectionGained và GuiObject.SelectionLost sẽ không bắn cho thành phần. Để bỏ chọn GuiObject, bạn phải thay đổi thuộc tính GuiService.SelectedObject.
Tính năng này hữu ích nếu một GUI được kết nối với một số GUI thông qua các tính năng như thế này GuiObject.NextSelectionUp , GuiObject.NextSelectionDown , NextSelectionRight hoặc NextSelectionLeft .Thay vì thay đổi tất cả các thuộc tính để Gamepad không thể chọn GUI, bạn có thể vô hiệu hóa thuộc tính Selectable để tạm thời ngăn chặn nó không được chọn.Sau đó, khi bạn muốn gamepad lựa chọn có thể chọn GUI, chỉ cần bật lại tính năng có thể chọn của nó.
Mẫu mã
The example below offers a simple demonstration on how to use the GuiObject.Selectable property to limit when a GUI element can be selected by the Gamepad navigator.
When a TextBox has gains focus, it can be selected. However, when a TextBox loses focus it can no longer be selected.
Although this is a simple demonstration, the property can also be used to prevent the navigator from selecting UI elements that exist for cosmetic rather than functional purposes. For instance, while the buttons on a menu screen should be selectable, the title image should not be.
local GuiService = game:GetService("GuiService")
local textBox = script.Parent
local function gainFocus()
textBox.Selectable = true
GuiService.SelectedObject = textBox
end
local function loseFocus(_enterPressed, _inputObject)
GuiService.SelectedObject = nil
textBox.Selectable = false
end
-- The FocusLost and FocusGained event will fire because the textBox
-- is of type TextBox
textBox.Focused:Connect(gainFocus)
textBox.FocusLost:Connect(loseFocus)
SelectionImageObject
Tính chất này thay thế trang trí lựa chọn mặc định được sử dụng cho gamepad.
Lưu ý rằng lớp phủ được chọn SelectionImageObject vượt qua lớp phủ được chọn GuiObject với Size của hình ảnh.Để có kết quả tốt nhất, bạn nên thay đổi kích thước tùy chỉnh SelectionImageObject qua các giá trị thước đo UDim2 để giúp đảm bảo rằng đối tượng phù hợp với kích thước trên phần tử được chọn.
Thay đổi SelectionImageObject cho một thành phần GuiObject chỉ ảnh hưởng đến thành phần đó. Để ảnh hưởng đến tất cả các thành phần GUI của người dùng, hãy đặt tính năng PlayerGui.SelectionImageObject.
Để xác định hoặc đặt GUI nào được chọn bởi người dùng, bạn có thể sử dụng thuộc tính GuiService.SelectedObject.Người chơi sử dụng gamepad để chọn các thành phần GUI khác nhau, kích hoạt sự kiện NextSelectionUp, NextSelectionDown, NextSelectionLeft và NextSelectionRight.
SelectionOrder
GuiObjects với Lệnh lựa chọn thấp hơn được chọn trước khi GuiObjects với Lệnh lựa chọn cao hơn khi bắt đầu lựa chọn gamepad hoặc gọi GuiService:Select() trên một tổ tiên.Tính chất này không ảnh hưởng đến điều hướng theo hướng.Giá trị mặc định là 0.
Size
Thuộc tính này xác định kích thước GuiObject ẩn và kích thước pixel bằng cách sử dụng UDim2 .
Kích thước scalar là tương đối với kích thước của thành phần GUI cha, nếu có.
Các phần pixel của giá trị UDim2 không phụ thuộc vào kích thước của GUI cha.Các giá trị đại diện cho kích thước của đối tượng bằng像素.Kích thước pixel thực của một đối tượng có thể được đọc từ thuộc tính GuiBase2d.AbsoluteSize.
Nếu GuiObject có một cha, kích thước của nó theo mỗi trục cũng bị ảnh hưởng bởi cha của nó SizeConstraint.
Mẫu mã
This code sample allows you to create a simple color-changing health bar using two nested Frames. Paste this into a LocalScript on the inner frame.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
-- Paste script into a LocalScript that is
-- parented to a Frame within a Frame
local frame = script.Parent
local container = frame.Parent
container.BackgroundColor3 = Color3.new(0, 0, 0) -- black
-- This function is called when the humanoid's health changes
local function onHealthChanged()
local human = player.Character.Humanoid
local percent = human.Health / human.MaxHealth
-- Change the size of the inner bar
frame.Size = UDim2.new(percent, 0, 1, 0)
-- Change the color of the health bar
if percent < 0.1 then
frame.BackgroundColor3 = Color3.new(1, 0, 0) -- black
elseif percent < 0.4 then
frame.BackgroundColor3 = Color3.new(1, 1, 0) -- yellow
else
frame.BackgroundColor3 = Color3.new(0, 1, 0) -- green
end
end
-- This function runs is called the player spawns in
local function onCharacterAdded(character)
local human = character:WaitForChild("Humanoid")
-- Pattern: update once now, then any time the health changes
human.HealthChanged:Connect(onHealthChanged)
onHealthChanged()
end
-- Connect our spawn listener; call it if already spawned
player.CharacterAdded:Connect(onCharacterAdded)
if player.Character then
onCharacterAdded(player.Character)
end
SizeConstraint
Thuộc tính này đặt các trục Size mà GuiObject sẽ dựa trên, so với kích thước của cha của nó, so với kích thước của cha của nó.
Tính năng này hữu ích để tạo các đối tượng GUI nhằm mục đích mở rộng với chiều rộng hoặc chiều cao của một đối tượng cha, nhưng không cả hai, hiệu quả bảo tồn tỷ lệ khía cạnh của đối tượng.
Visible
Tính chất này xem xét liệu GuiObject và con cháu của nó sẽ được hiển thị.
Hiển thị của các thành phần riêng lẻ của một GuiObject có thể được kiểm soát riêng lẻ thông qua các tính năng minh bạch như GuiObject.BackgroundTransparency , TextLabel.TextTransparency và ImageLabel.ImageTransparency .
Khi thuộc tính này là false , GuiObject sẽ bị bỏ qua bởi cấu trúc bố trí như UIListLayout , UIGridLayout và UITableLayout .Nói cách khác, không gian mà thành phần sẽ chiếm trong bố trí khác sẽ được các thành phần khác sử dụng thay thế.
Mẫu mã
This code sample adds open/close functionality to a Window UI. Paste as a LocalScript that is a sibling of a Frame named Window, a TextButton/ImageButton named Window, and a TextButton/ImageButton within the Window called Close.
local gui = script.Parent
local window = gui:WaitForChild("Window")
local toggleButton = gui:WaitForChild("ToggleWindow")
local closeButton = window:WaitForChild("Close")
local function toggleWindowVisbility()
-- Flip a boolean using the `not` keyword
window.Visible = not window.Visible
end
toggleButton.Activated:Connect(toggleWindowVisbility)
closeButton.Activated:Connect(toggleWindowVisbility)
ZIndex
Tính chất này xác định thứ tự mà một GuiObject render so với những người khác.
Mặc định, GuiObjects render theo thứ tự ưu tiên tăng dần nơi những người có giá trị thấp hơn ZIndex được render dưới những người có giá trị cao hơn.Bạn có thể thay đổi trật tự render trong một ScreenGui , SurfaceGui , hoặc BillboardGui bằng cách thay đổi giá trị của ZIndexBehavior .
Nếu bạn không chắc chắn liệu bạn có cần thêm một yếu tố giữa hai yếu tố hiện có trong tương lai hay không, thì thực hành tốt là sử dụng nhân số của 100 ( 0 , 100 , 200 , v.v.).Điều này đảm bảo một khoảng cách lớn của giá trị trật tự render mà bạn có thể sử dụng cho các yếu tố được xếp chồng giữa các yếu tố khác.
Xem thêm LayoutOrder điều khiển trật tự sắp xếp của một GuiObject khi sử dụng với một cấu trúc bố trí như UIListLayout hoặc UIGridLayout .
Phương Pháp
TweenPosition
Di chuyển một GUI mượt mà đến vị trí mới UDim2 trong thời gian được chỉ định bằng cách sử dụng Enum.EasingDirection và Enum.EasingStyle .
Chức năng này sẽ trả về xem liệu tween có chơi hay không.Nó sẽ không chơi nếu một tween khác đang hoạt động trên GuiObject và tham số trừng phạt là false .
Xem thêm GuiObject:TweenSize() và GuiObject:TweenSizeAndPosition() .
Tham Số
Nơi GUI nên di chuyển đến.
Hướng để dễ dàng GUI đến vị trí cuối.
Phong cách để dễ dàng GUI đến vị trí cuối.
Trong bao lâu, trong giây lát, tween nên mất để hoàn thành.
Liệu tween sẽ thay thế một tween đang thực hiện hay không.
Một chức năng trả lại khi hoàn thành khi tween hoàn thành.
Lợi Nhuận
Xem liệu tween có chơi hay không.
Mẫu mã
This code sample demonstrates a more involved usage of TweenPosition by detecting when the tween completes/cancels by defining a callback function. It also prints whether the tween will play.
local START_POSITION = UDim2.new(0, 0, 0, 0)
local GOAL_POSITION = UDim2.new(1, 0, 1, 0)
local guiObject = script.Parent
local function callback(state)
if state == Enum.TweenStatus.Completed then
print("The tween completed uninterrupted")
elseif state == Enum.TweenStatus.Canceled then
print("Another tween cancelled this one")
end
end
-- Initialize the GuiObject position, then start the tween:
guiObject.Position = START_POSITION
local willPlay = guiObject:TweenPosition(
GOAL_POSITION, -- Final position the tween should reach
Enum.EasingDirection.In, -- Direction of the easing
Enum.EasingStyle.Sine, -- Kind of easing to apply
2, -- Duration of the tween in seconds
true, -- Whether in-progress tweens are interrupted
callback -- Function to be callled when on completion/cancelation
)
if willPlay then
print("The tween will play")
else
print("The tween will not play")
end
TweenSize
Thay đổi kích thước một GuiObject sang một UDim2 mới trong thời gian quy định bằng cách sử dụng Enum.EasingDirection và Enum.EasingStyle được chỉ định.
Chức năng này sẽ trả về xem liệu tween có chơi hay không.Thông thường, nó sẽ luôn trả về true , nhưng nó sẽ trả về false nếu một tween khác đang hoạt động và việc thay thế được đặt thành false .
Xem thêm GuiObject:TweenSize() và GuiObject:TweenSizeAndPosition() .
Tham Số
Kích thước mà GUI nên thay đổi.
Hướng để dễ dàng GUI đến endSize.
Phong cách để dễ dàng GUI đến endSize.
Trong bao lâu, trong giây lát, tween nên mất để hoàn thành.
Liệu tween sẽ thay thế một tween đang thực hiện hay không.
Một chức năng trả lại khi hoàn thành khi tween hoàn thành.
Lợi Nhuận
Xem liệu tween có chơi hay không.
Mẫu mã
This code sample demonstrates the usage of the GuiObject:TweenSize() function. It initiates an animation on the parent's GuiObject.Size property to UDim2.new(0.5, 0, 0.5, 0), which is half the GuiObject's parent size on both axes.
Additionally, it demonstrates how the callback parameter can be used to detect when the tween stops (whether it was cancelled by another tween or completed).
local guiObject = script.Parent
local function callback(didComplete)
if didComplete then
print("The tween completed successfully")
else
print("The tween was cancelled")
end
end
local willTween = guiObject:TweenSize(
UDim2.new(0.5, 0, 0.5, 0), -- endSize (required)
Enum.EasingDirection.In, -- easingDirection (default Out)
Enum.EasingStyle.Sine, -- easingStyle (default Quad)
2, -- time (default: 1)
true, -- should this tween override ones in-progress? (default: false)
callback -- a function to call when the tween completes (default: nil)
)
if willTween then
print("The GuiObject will tween")
else
print("The GuiObject will not tween")
end
TweenSizeAndPosition
Thay đổi kích thước và vị trí GUI một cách mượt mà sang kích thước và vị trí mới UDim2 trong thời gian quy định bằng cách sử dụng Enum.EasingDirection và Enum.EasingStyle .
Chức năng này sẽ trả về xem liệu tween có chơi hay không.Thông thường, nó sẽ luôn trả về true , nhưng nó sẽ trả về false nếu một tween khác đang hoạt động và việc thay thế được đặt thành false .
Xem thêm GuiObject:TweenSize() và GuiObject:TweenSizeAndPosition() .
Tham Số
Kích thước mà GUI nên thay đổi.
Nơi GUI nên di chuyển đến.
Hướng để dễ dàng GUI đến endSize và endPosition .
Phong cách để dễ dàng GUI đến endSize và endPosition .
Trong bao lâu, trong giây lát, tween nên mất để hoàn thành.
Liệu tween sẽ thay thế một tween đang thực hiện hay không.
Một chức năng trả lại khi hoàn thành khi tween hoàn thành.
Lợi Nhuận
Xem liệu tween có chơi hay không.
Mẫu mã
The below example would tween a Frame to the top left of the parent's size and resize it down to 0.
local frame = script.Parent.Frame
frame:TweenSizeAndPosition(UDim2.new(0, 0, 0, 0), UDim2.new(0, 0, 0, 0))
Sự Kiện
InputBegan
Sự kiện này bắt lửa khi một người dùng bắt đầu tương tác với GuiObject bằng thiết bị Giao diện con người-máy (nhấn nút chuột xuống, chạm bắt đầu, nút bàn phím xuống, v.v.).
The UserInputService có một sự kiện có tên tương tự không bị giới hạn ở một thành phần UI cụ thể: UserInputService.InputBegan .
Sự kiện này sẽ luôn bắt lửa bất kể tình trạng trò chơi.
Xem thêm GuiObject.InputEnded và GuiObject.InputChanged .
Tham Số
Một InputObject , chứa các dữ liệu hữu ích cho việc truy vấn nhập của người dùng như type of input , state of input , và screen coordinates of the input .
Mẫu mã
The following example demonstrates one of many usage examples of handling user input from InputBegan depending on its type.
In order for this to work as expected, it must be placed in a LocalScript and a child of gui.
-- In order to use the InputBegan event, you must specify the GuiObject
local gui = script.Parent
-- A sample function providing multiple usage cases for various types of user input
local function inputBegan(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
print("A key is being pushed down! Key:", input.KeyCode)
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
print("The left mouse button has been pressed down at", input.Position)
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
print("The right mouse button has been pressed down at", input.Position)
elseif input.UserInputType == Enum.UserInputType.Touch then
print("A touchscreen input has started at", input.Position)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
print("A button is being pressed on a gamepad! Button:", input.KeyCode)
end
end
gui.InputBegan:Connect(inputBegan)
InputChanged
Sự kiện này bắt lửa khi người dùng thay đổi cách họ tương tác thông qua thiết bị Giao diện Con người - Máy tính (nhấn nút chuột xuống, chạm bắt đầu, nút bàn phím xuống, v.v.).
The UserInputService có một sự kiện có tên tương tự không bị giới hạn ở một thành phần UI cụ thể: UserInputService.InputChanged .
Sự kiện này sẽ luôn bắt lửa bất kể tình trạng trò chơi.
Xem thêm GuiObject.InputBegan và GuiObject.InputEnded .
Tham Số
Một InputObject , chứa các dữ liệu hữu ích cho việc truy vấn nhập của người dùng như type of input , state of input , và screen coordinates of the input .
Mẫu mã
The following example demonstrates one of many usage examples of handling user input from InputChanged depending on its type.
In order for this to work as expected, it must be placed in a LocalScript and a child of gui.
local UserInputService = game:GetService("UserInputService")
local gui = script.Parent
local function printMovement(input)
print("Position:", input.Position)
print("Movement Delta:", input.Delta)
end
local function inputChanged(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
print("The mouse has been moved!")
printMovement(input)
elseif input.UserInputType == Enum.UserInputType.MouseWheel then
print("The mouse wheel has been scrolled!")
print("Wheel Movement:", input.Position.Z)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
if input.KeyCode == Enum.KeyCode.Thumbstick1 then
print("The left thumbstick has been moved!")
printMovement(input)
elseif input.KeyCode == Enum.KeyCode.Thumbstick2 then
print("The right thumbstick has been moved!")
printMovement(input)
elseif input.KeyCode == Enum.KeyCode.ButtonL2 then
print("The pressure being applied to the left trigger has changed!")
print("Pressure:", input.Position.Z)
elseif input.KeyCode == Enum.KeyCode.ButtonR2 then
print("The pressure being applied to the right trigger has changed!")
print("Pressure:", input.Position.Z)
end
elseif input.UserInputType == Enum.UserInputType.Touch then
print("The user's finger is moving on the screen!")
printMovement(input)
elseif input.UserInputType == Enum.UserInputType.Gyro then
local _rotInput, rotCFrame = UserInputService:GetDeviceRotation()
local rotX, rotY, rotZ = rotCFrame:toEulerAnglesXYZ()
local rot = Vector3.new(math.deg(rotX), math.deg(rotY), math.deg(rotZ))
print("The rotation of the user's mobile device has been changed!")
print("Position", rotCFrame.p)
print("Rotation:", rot)
elseif input.UserInputType == Enum.UserInputType.Accelerometer then
print("The acceleration of the user's mobile device has been changed!")
printMovement(input)
end
end
gui.InputChanged:Connect(inputChanged)
InputEnded
Sự kiện InputEnded bắt lửa khi một người dùng ngừng tương tác thông qua thiết bị Giao diện Con người - Máy tính ( nút chuột xuống, chạm bắt đầu, nút bàn phím xuống, v.v.).
The UserInputService có một sự kiện có tên tương tự không bị giới hạn ở một thành phần UI cụ thể: UserInputService.InputEnded .
Sự kiện này sẽ luôn bắt lửa bất kể tình trạng trò chơi.
Xem thêm GuiObject.InputBegan và GuiObject.InputChanged .
Tham Số
Một InputObject , chứa các dữ liệu hữu ích cho việc truy vấn nhập của người dùng như Enum.UserInputType , Enum.UserInputState , và InputObject.Position .
Mẫu mã
The following example demonstrates one of many usage examples of handling user input from InputEnded depending on its type.
In order for this to work as expected, it must be placed in a LocalScript and a child of gui.
-- In order to use the InputChanged event, you must specify a GuiObject
local gui = script.Parent
-- A sample function providing multiple usage cases for various types of user input
local function inputEnded(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
print("A key has been released! Key:", input.KeyCode)
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
print("The left mouse button has been released at", input.Position)
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
print("The right mouse button has been released at", input.Position)
elseif input.UserInputType == Enum.UserInputType.Touch then
print("A touchscreen input has been released at", input.Position)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
print("A button has been released on a gamepad! Button:", input.KeyCode)
end
end
gui.InputEnded:Connect(inputEnded)
MouseEnter
Sự kiện MouseEnter bắt lửa khi một người dùng di chuyển chuột vào một thành phần GuiObject .
Xin đừng dựa vào các x và y argument được truyền bởi sự kiện này như một cách ngăn chặn sai lầm để xác định vị trí con trỏ của người dùng khi nó vào một GUI.Các điểm cực trùng này có thể thay đổi ngay cả khi chuột vào GUI thông qua cạnh tương tự - đặc biệt là khi chuột nhập vào thành phần nhanh chóng.Điều này là do các điểm cố định chỉ ra vị trí của con trỏ khi sự kiện bắt lửa xảy ra chứ không phải là thời điểm chính xác con trỏ vào GUI.
Sự kiện này bắt lửa ngay cả khi thành phần GUI được hiển thị dưới thành phần khác.
Nếu bạn muốn theo dõi khi con trỏ của người dùng rời khỏi một thành phần GUI, bạn có thể sử dụng sự kiện GuiObject.MouseLeave.
Xem thêm
Tham Số
Vị trí màn hình của chuột X theo điểm, so với góc trên cùng bên trái của màn hình.
Vị trí màn hình của chuột Y theo điểm ở góc trên cùng bên trái của màn hình.
Mẫu mã
The following example prints the mouse location, in pixels, when it enters GUI element.
local guiObject = script.Parent
guiObject.MouseEnter:Connect(function(x, y)
print("The user's mouse cursor has entered the GuiObject at position", x, ",", y)
end)
MouseLeave
Sự kiện MouseLeave bắt lửa khi một người dùng di chuyển chuột ra khỏi một GuiObject nguyên tố.
Xin đừng dựa vào các x và y argument được truyền bởi sự kiện này như một cách ngăn chặn sai lầm để xác định vị trí con trỏ của người dùng khi nó rời khỏi GUI.Các điểm cực trùng này có thể thay đổi ngay cả khi chuột rời GUI qua cạnh tương tự - đặc biệt là khi chuột nhanh chóng rời khỏi thành phần.Điều này là do các điểm cố định chỉ ra vị trí của con trỏ khi sự kiện bắt lửa xảy ra chứ không phải là thời điểm chính xác con trỏ rời khỏi GUI.
Sự kiện này bắt lửa ngay cả khi thành phần GUI được hiển thị dưới thành phần khác.
Xem thêm
Tham Số
Vị trí màn hình của chuột X theo điểm, so với góc trên cùng bên trái của màn hình.
Vị trí màn hình của chuột Y theo điểm ở góc trên cùng bên trái của màn hình.
MouseMoved
Bắt lửa mỗi khi người dùng di chuyển chuột trong khi nó nằm bên trong một thành phần GuiObject.Nó giống như Mouse.Move , bắn ra bất kể liệu chuột của người dùng có ở trên một thành phần GUI hay không.
Lưu ý, sự kiện này sẽ bắt lửa khi vị trí của con chuộtđược cập nhật, do đó nó sẽ bắt lửa lại nhiều lần trong khi di chuyển.
Các x và y argument chỉ ra các điểm đối xứng trên màn hình được cập nhật của chuột của người dùng bằng pixel.Chúng có thể hữu ích để xác định vị trí của con trỏ trên GUI, màn hình và delta kể từ vị trí trước của con trỏ nếu nó được theo dõi trong một biến toàn cầu.
Mã bên dưới minh họa cách xác định offset Vector2 của chuột người dùng so với một thành phần GUI:
local Players = game:GetService("Players")
local CustomScrollingFrame = script.Parent
local SubFrame = CustomScrollingFrame:FindFirstChild("SubFrame")
local mouse = Players.LocalPlayer:GetMouse()
local function getPosition(X, Y)
local gui_X = CustomScrollingFrame.AbsolutePosition.X
local gui_Y = CustomScrollingFrame.AbsolutePosition.Y
local pos = Vector2.new(math.abs(X - gui_X), math.abs(Y - gui_Y - 36))
print(pos)
end
CustomScrollingFrame.MouseMoved:Connect(getPosition)
Lưu ý rằng sự kiện này có thể không bắt lửa chính xác khi con trỏ của người dùng vào hoặc thoát khỏi một thành phần GUI.Do đó, các tham số x và y có thể không phù hợp hoàn toàn với các điểm của cạnh của GUI.
Xem thêm
Tham Số
Vị trí màn hình của chuột X theo điểm, so với góc trên cùng bên trái của màn hình.
Vị trí màn hình của chuột Y theo điểm ở góc trên cùng bên trái của màn hình.
MouseWheelBackward
Sự kiện WheelBackward bắt lửa khi một người dùng cuộn bánh xoay lại khi chuột ở trên một GuiObject nguyên tố.Nó giống như Mouse.WheelBackward , bắn ra bất kể liệu chuột của người dùng có ở trên một thành phần GUI hay không.
Sự kiện này chỉ bắt lửa như một chỉ báo về sự di chuyển lùi của bánh xe.Điều này có nghĩa là các tham số phối trí chuột x và y không thay đổi do sự kiện này.Các điểm cực trùng này chỉ thay đổi khi chuột di chuyển, có thể được theo dõi bởi sự kiện GuiObject.MouseMoved .
Xem thêm
Tham Số
Vị trí màn hình của chuột X theo điểm, so với góc trên cùng bên trái của màn hình.
Vị trí màn hình của chuột Y theo điểm ở góc trên cùng bên trái của màn hình.
MouseWheelForward
Sự kiện WheelForward bắt lửa khi một người dùng cuộn bánh xoay của họ về phía trước khi con trỏ ở trên một thành phần GuiObject .Nó giống như Mouse.WheelForward , bắn ra bất kể liệu chuột của người dùng có ở trên một thành phần GUI hay không.
Sự kiện này chỉ bắt lửa như một chỉ báo về sự di chuyển của bánh xe.Điều này có nghĩa là các tham số điều phối chuột X và Y không thay đổi do sự kiện này.Các điểm cực trùng này chỉ thay đổi khi chuột di chuyển, có thể được theo dõi bởi sự kiện GuiObject.MouseMoved .
Xem thêm
Tham Số
Vị trí màn hình của chuột X theo điểm, so với góc trên cùng bên trái của màn hình.
Vị trí Y của chuộttrỏ của người dùng.
SelectionGained
Sự kiện này bắt lửa khi lựa chọn Gamepad bắt đầu tập trung vào GuiObject.
Nếu bạn muốn kiểm tra từ Gamepad chọn dừng tập trung vào yếu tố GUI, bạn có thể sử dụng sự kiện GuiObject.SelectionLost.
Khi một GUI nhận được sự tập trung lựa chọn, giá trị của thuộc tính SelectedObject cũng thay đổi thành giá trị lựa chọn đó.Để xác định GUI nào đã được chọn, hãy kiểm tra giá trị của thuộc tính này.
Mẫu mã
The following example prints a message when the user selects the object with a gamepad.
In order for this to work as expected, it must be placed in a LocalScript and a child of gui.
local guiObject = script.Parent
local function selectionGained()
print("The user has selected this button with a gamepad.")
end
guiObject.SelectionGained:Connect(selectionGained)
SelectionLost
Sự kiện này xảy ra khi lựa chọn Gamepad ngừng tập trung vào GuiObject.
Nếu bạn muốn kiểm tra từ Gamepad bắt đầu tập trung vào yếu tố GUI, bạn có thể sử dụng sự kiện GuiObject.SelectionGained.
Khi một GUI mất tập trung lựa chọn, giá trị của thuộc tính SelectionObject thay đổi thành nil hoặc vào thành phần GUI nhận được tập trung lựa chọn.Để xác định GUI nào đã được chọn, hoặc nếu không có GUI nào được chọn, kiểm tra giá trị của thuộc tính này.
Mẫu mã
The following example prints a message when the element has its focus lost on a gamepad.
In order for this to work as expected, it must be placed in a LocalScript and a child of gui.
local guiObject = script.Parent
local function selectionLost()
print("The user no longer has this selected with their gamepad.")
end
guiObject.SelectionLost:Connect(selectionLost)
TouchLongPress
Sự kiện này bắt lửa sau một lúc ngắn khi người chơi giữ ngón tay trên thành phần UI bằng một thiết bị có khả năng chạm.Nó bắn với một bảng của Vector2 mô tả vị trí màn hình tương đối của các ngón tay liên quan đến cử chỉ.Ngoài ra, nó bắn nhiều lần: Enum.UserInputState.Begin sau một khoảng thời gian ngắn, Enum.UserInputState.Change nếu người chơi di chuyển ngón tay trong lúc cử chỉ, và cuối cùng Enum.UserInputState.End .Thời gian trễ phụ thuộc vào nền tảng; trong Studio nó lâu hơn một giây.
Vì sự kiện này chỉ yêu cầu một ngón tay, sự kiện này có thể được mô phỏng trong Studio bằng cách sử dụng emulator và một con chuột.
Tham Số
Một mảng của mô tả vị trí tương đối của các ngón tay liên quan đến cử chỉ.
Một Enum.UserInputState mô tả trạng thái của cử chỉ:
- Bắt đầu lửa một lần khi ở đầu cuối của cử chỉ (sau thời gian ngắn ngủi)
- Thay đổi lửa nếu người chơi di chuyển ngón tay trong khi nhấn xuống
- Kết thúc lửa một lần khi kết thúc cử chỉ khi họ thả ngón tay của họ.
Mẫu mã
This code sample allows the player to manipulate the screen position of some UI element, like a Frame, by holding down on the UI element for a brief moment. Then, the player moves their finger and releases. During the gesture the Frame is colored blue with a white outline.
local frame = script.Parent
frame.Active = true
local dragging = false
local basePosition
local startTouchPosition
local borderColor3
local backgroundColor3
local function onTouchLongPress(touchPositions, state)
if state == Enum.UserInputState.Begin and not dragging then
-- Start a drag
dragging = true
basePosition = frame.Position
startTouchPosition = touchPositions[1]
-- Color the frame to indicate the drag is happening
borderColor3 = frame.BorderColor3
backgroundColor3 = frame.BackgroundColor3
frame.BorderColor3 = Color3.new(1, 1, 1) -- White
frame.BackgroundColor3 = Color3.new(0, 0, 1) -- Blue
elseif state == Enum.UserInputState.Change then
local touchPosition = touchPositions[1]
local deltaPosition =
UDim2.new(0, touchPosition.X - startTouchPosition.X, 0, touchPosition.Y - startTouchPosition.Y)
frame.Position = basePosition + deltaPosition
elseif state == Enum.UserInputState.End and dragging then
-- Stop the drag
dragging = false
frame.BorderColor3 = borderColor3
frame.BackgroundColor3 = backgroundColor3
end
end
frame.TouchLongPress:Connect(onTouchLongPress)
TouchPan
Sự kiện này bắt lửa khi người chơi di chuyển ngón tay trên thành phần UI bằng một thiết bị có khả năng chạm.Nó bắn trước khi GuiObject.TouchSwipe sẽ xảy ra, và không bắn với GuiObject.TouchTap.Sự kiện này hữu ích để cho phép người chơi thao tác vị trí của các thành phần UI trên màn hình.
Sự kiện này bắt lửa với một bảng của Vector2 mô tả vị trí màn hình tương đối của các ngón tay liên quan đến cử chỉ.Ngoài ra, nó bắn nhiều lần: Enum.UserInputState.Begin sau một khoảng thời gian ngắn, Enum.UserInputState.Change khi người chơi di chuyển ngón tay trong lúc cử chỉ, và cuối cùng với Enum.UserInputState.End .
Sự kiện này không thể được mô phỏng trong Studio bằng cách sử dụng máy giả lập và chuột; bạn phải có một thiết bị thực sự có khả năng chạm để bắn nó.
Tham Số
Một mảng Luau của Vector2 đối tượng, mỗi đối tượng chỉ ra vị trí của tất cả các ngón tay liên quan đến cử chỉ.
Chỉ ra cách xa mà cử chỉ chảo đã đi từ điểm xuất phát của nó.
Chỉ ra tốc độ mà cử chỉ được thực hiện trong mỗi chiều không gian.
Chỉ ra Enum.UserInputState của cử chỉ.
Mẫu mã
This code sample is meant to be placed in a LocalScript within an inner Frame that is inside an outer Frame, or other GuiObject. It allows the player to manipulate the position of the inner frame by moving their finger on the outer frame.
local innerFrame = script.Parent
local outerFrame = innerFrame.Parent
outerFrame.BackgroundTransparency = 0.75
outerFrame.Active = true
outerFrame.Size = UDim2.new(1, 0, 1, 0)
outerFrame.Position = UDim2.new(0, 0, 0, 0)
outerFrame.AnchorPoint = Vector2.new(0, 0)
outerFrame.ClipsDescendants = true
local dragging = false
local basePosition
local function onTouchPan(_touchPositions, totalTranslation, _velocity, state)
if state == Enum.UserInputState.Begin and not dragging then
dragging = true
basePosition = innerFrame.Position
outerFrame.BackgroundTransparency = 0.25
elseif state == Enum.UserInputState.Change then
innerFrame.Position = basePosition + UDim2.new(0, totalTranslation.X, 0, totalTranslation.Y)
elseif state == Enum.UserInputState.End and dragging then
dragging = false
outerFrame.BackgroundTransparency = 0.75
end
end
outerFrame.TouchPan:Connect(onTouchPan)
TouchPinch
Sự kiện này xảy ra khi người chơi sử dụng hai ngón tay để thực hiện cử chỉ kẹp hoặc kéo trên thành phần UI bằng một thiết bị có khả năng chạm.Một nắm xảy ra khi hai hoặc nhiều ngón tay di chuyển gần nhau hơn, và một kéo xảy ra khi chúng di chuyển ra xa nhau.Sự kiện này bắt lửa cùng với GuiObject.TouchPan .Sự kiện này hữu ích để cho phép người chơi thao tác với quy mô (kích cỡ) của các thành phần UI trên màn hình, và được sử dụng phổ biến nhất cho các tính năng thu phóng.
Sự kiện này bắt lửa với một bảng của Vector2 mô tả vị trí màn hình tương đối của các ngón tay liên quan đến cử chỉ.Ngoài ra, nó bắn nhiều lần: Enum.UserInputState.Begin sau một khoảng thời gian ngắn, Enum.UserInputState.Change khi người chơi di chuyển một ngón tay trong lúc cử chỉ, và cuối cùng với Enum.UserInputState.End .Cần lưu ý rằng thước đo nên được sử dụng theo cách nhân .
Bởi vì sự kiện này yêu cầu ít nhất hai ngón tay, không thể mô phỏng nó trong Studio bằng cách sử dụng máy phát lại và chuột; bạn phải có một thiết bị có thực sự có thể bật chạm.
Tham Số
Một mảng Luau của Vector2 đối tượng, mỗi đối tượng chỉ ra vị trí của tất cả các ngón tay liên quan đến cử chỉ kẹp.
Một float chỉ ra sự khác biệt từ đầu của cử chỉ kẹp.
Một thông số nổi chỉ ra tốc độ mà cử chỉ kẹp xảy ra.
Chỉ ra Enum.UserInputState của cử chỉ.
Mẫu mã
This code sample is meant for a LocalScript within an inner Frame that is inside an outer Frame, or other GuiObject. It allows the player to scale the inner frame by performing a GuiObject.TouchPinch gesture on the outer frame.
local innerFrame = script.Parent
local outerFrame = innerFrame.Parent
outerFrame.BackgroundTransparency = 0.75
outerFrame.Active = true
outerFrame.Size = UDim2.new(1, 0, 1, 0)
outerFrame.Position = UDim2.new(0, 0, 0, 0)
outerFrame.AnchorPoint = Vector2.new(0, 0)
outerFrame.ClipsDescendants = true
local dragging = false
local uiScale = Instance.new("UIScale")
uiScale.Parent = innerFrame
local baseScale
local function onTouchPinch(_touchPositions, scale, _velocity, state)
if state == Enum.UserInputState.Begin and not dragging then
dragging = true
baseScale = uiScale.Scale
outerFrame.BackgroundTransparency = 0.25
elseif state == Enum.UserInputState.Change then
uiScale.Scale = baseScale * scale -- Notice the multiplication here
elseif state == Enum.UserInputState.End and dragging then
dragging = false
outerFrame.BackgroundTransparency = 0.75
end
end
outerFrame.TouchPinch:Connect(onTouchPinch)
TouchRotate
Sự kiện này xảy ra khi người chơi sử dụng hai ngón tay để thực hiện cử chỉ kẹp hoặc kéo trên thành phần UI bằng một thiết bị có khả năng chạm.Vòng xoay xảy ra khi góc của dòng giữa hai ngón tay thay đổi.Sự kiện này bắt lửa cùng với GuiObject.TouchPan .Sự kiện này hữu ích để cho phép người chơi thao tác với sự xoay của các thành phần UI trên màn hình.
Sự kiện này bắt lửa với một bảng của Vector2 mô tả vị trí màn hình tương đối của các ngón tay liên quan đến cử chỉ.Ngoài ra, nó bắn nhiều lần: Enum.UserInputState.Begin sau một khoảng thời gian ngắn, Enum.UserInputState.Change khi người chơi di chuyển một ngón tay trong lúc cử chỉ, và cuối cùng với Enum.UserInputState.End .
Vì sự kiện này yêu cầu ít nhất hai ngón tay, không thể mô phỏng trong Studio bằng cách sử dụng máy phát lại và chuột; bạn phải có một thiết bị có thực sự có thể bật chạm.
Tham Số
Một mảng Luau của Vector2 đối tượng, mỗi đối tượng chỉ ra vị trí của tất cả các ngón tay liên quan đến cử chỉ.
Một float chỉ ra số lượng quay đã diễn ra từ khi bắt đầu cử chỉ.
Một float chỉ ra tốc độ mà cử chỉ đang được thực hiện.
Chỉ ra Enum.UserInputState của cử chỉ.
Mẫu mã
This code sample is meant for a LocalScript within an inner Frame that is inside an outer Frame, or other GuiObject. It allows the player to rotate the inner frame by performing a GuiObject.TouchRotate gesture on the outer frame.
local innerFrame = script.Parent
local outerFrame = innerFrame.Parent
outerFrame.BackgroundTransparency = 0.75
outerFrame.Active = true
outerFrame.Size = UDim2.new(1, 0, 1, 0)
outerFrame.Position = UDim2.new(0, 0, 0, 0)
outerFrame.AnchorPoint = Vector2.new(0, 0)
outerFrame.ClipsDescendants = true
local dragging = false
local baseRotation = innerFrame.Rotation
local function onTouchRotate(_touchPositions, rotation, _velocity, state)
if state == Enum.UserInputState.Begin and not dragging then
dragging = true
baseRotation = innerFrame.Rotation
outerFrame.BackgroundTransparency = 0.25
elseif state == Enum.UserInputState.Change then
innerFrame.Rotation = baseRotation + rotation
elseif state == Enum.UserInputState.End and dragging then
dragging = false
outerFrame.BackgroundTransparency = 0.75
end
end
outerFrame.TouchRotate:Connect(onTouchRotate)
TouchSwipe
Sự kiện này bắt lửa khi người chơi thực hiện cử chỉ vuốt trên thành phần UI bằng một thiết bị có khả năng chạm.Nó bắn theo hướng của cử chỉ (Lên, Xuống, Trái hoặc Phải) và số điểm chạm liên quan đến cử chỉ.Cử chỉ vuốt thường được sử dụng để thay đổi tab trong giao diện người dùng di động.
Vì sự kiện này chỉ yêu cầu một ngón tay, nó có thể được mô phỏng trong Studio bằng cách sử dụng emulator và một con chuột.
Tham Số
A Enum.SwipeDirection được chỉ ra hướng của cử chỉ vuốt (Lên, Xuống, Trái hoặc Phải).
Số điểm tiếp xúc liên quan đến cử chỉ (thường là 1).
Mẫu mã
This code sample will cause a Frame (or other GuiObject) to bounce when a swipe gesture is performed on a touch-enabled device (or Studio's emulator). Horizontal swipes will change the hue of the GuiObject.BackgroundColor3, while vertical swipes will change the saturation.
local frame = script.Parent
frame.Active = true
-- How far the frame should bounce on a successful swipe
local BOUNCE_DISTANCE = 50
-- Current state of the frame
local basePosition = frame.Position
local hue = 0
local saturation = 128
local function updateColor()
frame.BackgroundColor3 = Color3.fromHSV(hue / 256, saturation / 256, 1)
end
local function onTouchSwipe(swipeDir, _touchCount)
-- Change the BackgroundColor3 based on the swipe direction
local deltaPos
if swipeDir == Enum.SwipeDirection.Right then
deltaPos = UDim2.new(0, BOUNCE_DISTANCE, 0, 0)
hue = (hue + 16) % 255
elseif swipeDir == Enum.SwipeDirection.Left then
deltaPos = UDim2.new(0, -BOUNCE_DISTANCE, 0, 0)
hue = (hue - 16) % 255
elseif swipeDir == Enum.SwipeDirection.Up then
deltaPos = UDim2.new(0, 0, 0, -BOUNCE_DISTANCE)
saturation = (saturation + 16) % 255
elseif swipeDir == Enum.SwipeDirection.Down then
deltaPos = UDim2.new(0, 0, 0, BOUNCE_DISTANCE)
saturation = (saturation - 16) % 255
else
deltaPos = UDim2.new()
end
-- Update the color and bounce the frame a little
updateColor()
frame.Position = basePosition + deltaPos
frame:TweenPosition(basePosition, Enum.EasingDirection.Out, Enum.EasingStyle.Bounce, 0.7, true)
end
frame.TouchSwipe:Connect(onTouchSwipe)
updateColor()
TouchTap
Sự kiện này bắt lửa khi người chơi thực hiện một cử chỉ vuốt trên thành phần UI bằng một thiết bị có khả năng chạm.Một lần nhấn là một lần chạm nhanh không có sự tham gia của bất kỳ chuyển động nào (một lần nhấn lâu hơn sẽ bắn GuiObject.TouchLongPress , và di chuyển trong lúc chạm sẽ bắn GuiObject.TouchPan và/hoặc GuiObject.TouchSwipe ).Nó bắn với một bảng của Vector2 đối tượng mô tả vị trí tương đối của các ngón tay liên quan đến cử chỉ.
Vì sự kiện này chỉ yêu cầu một ngón tay, nó có thể được mô phỏng trong Studio bằng cách sử dụng emulator và một con chuột.
Tham Số
Một mảng của mô tả vị trí tương đối của các ngón tay liên quan đến cử chỉ.
Mẫu mã
This code sample will toggle the GuiObject.BackgroundTransparency of a UI element, like a Frame, when it is tapped on a touch-enabled device.
local frame = script.Parent
frame.Active = true
local function onTouchTap()
-- Toggle background transparency
if frame.BackgroundTransparency > 0 then
frame.BackgroundTransparency = 0
else
frame.BackgroundTransparency = 0.75
end
end
frame.TouchTap:Connect(onTouchTap)