Lớp công cụ
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.
Tóm Tắt
Thuộc Tính
Phương Pháp
TweenPosition(endPosition: UDim2,easingDirection: Enum.EasingDirection,easingStyle: Enum.EasingStyle,time: number,override: boolean,callback: function):boolean |
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 |
Sự Kiện
DragBegin(initialPosition: UDim2):RBXScriptSignal |
DragStopped(x: number,y: number):RBXScriptSignal |
InputBegan(input: InputObject):RBXScriptSignal |
InputChanged(input: InputObject):RBXScriptSignal |
InputEnded(input: InputObject):RBXScriptSignal |
MouseEnter(x: number,y: number):RBXScriptSignal |
MouseLeave(x: number,y: number):RBXScriptSignal |
MouseMoved(x: number,y: number):RBXScriptSignal |
TouchLongPress(touchPositions: {any},state: Enum.UserInputState):RBXScriptSignal |
TouchPan(touchPositions: {any},totalTranslation: Vector2,velocity: Vector2,state: Enum.UserInputState):RBXScriptSignal |
TouchPinch(touchPositions: {any},scale: number,velocity: number,state: Enum.UserInputState):RBXScriptSignal |
TouchRotate(touchPositions: {any},rotation: number,velocity: number,state: Enum.UserInputState):RBXScriptSignal |
TouchSwipe(swipeDirection: Enum.SwipeDirection,numberOfTouches: number):RBXScriptSignal |
TouchTap(touchPositions: {any}):RBXScriptSignal |
Thành viên kế thừa
12 thành viên kế thừa từ GuiBase2d
57 thành viên kế thừa từ Instance
6 thành viên kế thừa từ Object
Được kế thừa bởi
Tài Liệu Tham Khảo API
Thuộc Tính
Active
Mẫu mã
Nút Văn Bản Hoạt Động Lùi
-- Đặt LocalScript này bên trong một TextButton (hoặc ImageButton)
local textButton = script.Parent
textButton.Text = "Nhấn vào tôi"
textButton.Active = true
local function onActivated()
-- Điều này hoạt động như một bộ lùi
textButton.Active = false
-- Đếm ngược từ 5
for i = 5, 1, -1 do
textButton.Text = "Thời gian: " .. i
task.wait(1)
end
textButton.Text = "Nhấn vào tôi"
textButton.Active = true
end
textButton.Activated:Connect(onActivated)AnchorPoint
Mẫu mã
Mẫu Demo Điểm Neo
local guiObject = script.Parent
while true do
-- Góc trên bên trái
guiObject.AnchorPoint = Vector2.new(0, 0)
guiObject.Position = UDim2.new(0, 0, 0, 0)
task.wait(1)
-- Trên
guiObject.AnchorPoint = Vector2.new(0.5, 0)
guiObject.Position = UDim2.new(0.5, 0, 0, 0)
task.wait(1)
-- Góc trên bên phải
guiObject.AnchorPoint = Vector2.new(1, 0)
guiObject.Position = UDim2.new(1, 0, 0, 0)
task.wait(1)
-- Bên trái
guiObject.AnchorPoint = Vector2.new(0, 0.5)
guiObject.Position = UDim2.new(0, 0, 0.5, 0)
task.wait(1)
-- Trung tâm chết
guiObject.AnchorPoint = Vector2.new(0.5, 0.5)
guiObject.Position = UDim2.new(0.5, 0, 0.5, 0)
task.wait(1)
-- Bên phải
guiObject.AnchorPoint = Vector2.new(1, 0.5)
guiObject.Position = UDim2.new(1, 0, 0.5, 0)
task.wait(1)
-- Góc dưới bên trái
guiObject.AnchorPoint = Vector2.new(0, 1)
guiObject.Position = UDim2.new(0, 0, 1, 0)
task.wait(1)
-- Dưới
guiObject.AnchorPoint = Vector2.new(0.5, 1)
guiObject.Position = UDim2.new(0.5, 0, 1, 0)
task.wait(1)
-- Góc dưới bên phải
guiObject.AnchorPoint = Vector2.new(1, 1)
guiObject.Position = UDim2.new(1, 0, 1, 0)
task.wait(1)
endAutomaticSize
Mẫu mã
LocalScript trong một ScreenGui
-- Mảng các nhãn văn bản/phông chữ/kích thước để xuất
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 },
}
-- Tạo một khung cha tự động có kích thước
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
-- Thêm một bố cục danh sách
local listLayout = Instance.new("UIListLayout")
listLayout.Padding = UDim.new(0, 5)
listLayout.Parent = parentFrame
-- Đặt các góc bo tròn và khoảng cách cho thẩm mỹ trực quan
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
-- Tạo một nhãn văn bản tự động có kích thước từ mảng
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
-- Thẩm mỹ trực quan
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)
endBackgroundColor
BackgroundColor3
Mẫu mã
Khung Cầu Vồng
-- Đặt mã này vào một LocalScript trong một Frame
local frame = script.Parent
while true do
for hue = 0, 255, 4 do
-- HSV = sắc thái, độ bão hòa, giá trị
-- Nếu chúng ta lặp từ 0 đến 1 liên tục, chúng ta sẽ có một cầu vồng!
frame.BorderColor3 = Color3.fromHSV(hue / 256, 1, 1)
frame.BackgroundColor3 = Color3.fromHSV(hue / 256, 0.5, 0.8)
task.wait()
end
endBorderColor
BorderColor3
Mẫu mã
Nổi Bật Nút
-- Đặt tôi vào trong một GuiObject, tốt nhất là một ImageButton/TextButton
local button = script.Parent
local function onEnter()
button.BorderSizePixel = 2
button.BorderColor3 = Color3.new(1, 1, 0) -- Vàng
end
local function onLeave()
button.BorderSizePixel = 1
button.BorderColor3 = Color3.new(0, 0, 0) -- Đen
end
-- Kết nối sự kiện
button.MouseEnter:Connect(onEnter)
button.MouseLeave:Connect(onLeave)
-- Trạng thái mặc định của chúng tôi là "không được di chuột"
onLeave()BorderSizePixel
Mẫu mã
Nổi Bật Nút
-- Đặt tôi vào trong một GuiObject, tốt nhất là một ImageButton/TextButton
local button = script.Parent
local function onEnter()
button.BorderSizePixel = 2
button.BorderColor3 = Color3.new(1, 1, 0) -- Vàng
end
local function onLeave()
button.BorderSizePixel = 1
button.BorderColor3 = Color3.new(0, 0, 0) -- Đen
end
-- Kết nối sự kiện
button.MouseEnter:Connect(onEnter)
button.MouseLeave:Connect(onLeave)
-- Trạng thái mặc định của chúng tôi là "không được di chuột"
onLeave()Draggable
NextSelectionDown
Mẫu mã
Tạo một Lưới Lựa Chọn Gamepad
-- Thiết lập lưới lựa chọn Gamepad bằng mã dưới đây
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
-- Cạnh trái
gui.NextSelectionLeft = container:FindFirstChild(pos - 1)
-- Cạnh phải
gui.NextSelectionRight = container:FindFirstChild(pos + 1)
-- Phía trên
gui.NextSelectionUp = container:FindFirstChild(pos - rowSize)
-- Phía dưới
gui.NextSelectionDown = container:FindFirstChild(pos + rowSize)
end
end
-- Kiểm tra lưới lựa chọn Gamepad bằng mã dưới đây
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
Mẫu mã
Tạo một Lưới Lựa Chọn Gamepad
-- Thiết lập lưới lựa chọn Gamepad bằng mã dưới đây
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
-- Cạnh trái
gui.NextSelectionLeft = container:FindFirstChild(pos - 1)
-- Cạnh phải
gui.NextSelectionRight = container:FindFirstChild(pos + 1)
-- Phía trên
gui.NextSelectionUp = container:FindFirstChild(pos - rowSize)
-- Phía dưới
gui.NextSelectionDown = container:FindFirstChild(pos + rowSize)
end
end
-- Kiểm tra lưới lựa chọn Gamepad bằng mã dưới đây
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
Mẫu mã
Tạo một Lưới Lựa Chọn Gamepad
-- Thiết lập lưới lựa chọn Gamepad bằng mã dưới đây
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
-- Cạnh trái
gui.NextSelectionLeft = container:FindFirstChild(pos - 1)
-- Cạnh phải
gui.NextSelectionRight = container:FindFirstChild(pos + 1)
-- Phía trên
gui.NextSelectionUp = container:FindFirstChild(pos - rowSize)
-- Phía dưới
gui.NextSelectionDown = container:FindFirstChild(pos + rowSize)
end
end
-- Kiểm tra lưới lựa chọn Gamepad bằng mã dưới đây
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
Mẫu mã
Tạo một Lưới Lựa Chọn Gamepad
-- Thiết lập lưới lựa chọn Gamepad bằng mã dưới đây
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
-- Cạnh trái
gui.NextSelectionLeft = container:FindFirstChild(pos - 1)
-- Cạnh phải
gui.NextSelectionRight = container:FindFirstChild(pos + 1)
-- Phía trên
gui.NextSelectionUp = container:FindFirstChild(pos - rowSize)
-- Phía dưới
gui.NextSelectionDown = container:FindFirstChild(pos + rowSize)
end
end
-- Kiểm tra lưới lựa chọn Gamepad bằng mã dưới đây
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)Selectable
Mẫu mã
Giới Hạn Lựa Chọn TextBox
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
-- Sự kiện FocusLost và FocusGained sẽ được kích hoạt vì textBox
-- là loại TextBox
textBox.Focused:Connect(gainFocus)
textBox.FocusLost:Connect(loseFocus)Size
Mẫu mã
Thanh Sức Khỏe
local Players = game:GetService("Players")
local player = Players.LocalPlayer
-- Dán mã vào một LocalScript mà
-- được gán cho một Frame bên trong một Frame
local frame = script.Parent
local container = frame.Parent
container.BackgroundColor3 = Color3.new(0, 0, 0) -- đen
-- Hàm này được gọi khi sức khỏe của humanoid thay đổi
local function onHealthChanged()
local human = player.Character.Humanoid
local percent = human.Health / human.MaxHealth
-- Thay đổi kích thước của thanh bên trong
frame.Size = UDim2.new(percent, 0, 1, 0)
-- Thay đổi màu sắc của thanh sức khỏe
if percent < 0.1 then
frame.BackgroundColor3 = Color3.new(1, 0, 0) -- đen
elseif percent < 0.4 then
frame.BackgroundColor3 = Color3.new(1, 1, 0) -- vàng
else
frame.BackgroundColor3 = Color3.new(0, 1, 0) -- xanh
end
end
-- Hàm này được gọi khi người chơi xuất hiện
local function onCharacterAdded(character)
local human = character:WaitForChild("Humanoid")
-- Mẫu: cập nhật một lần bây giờ, sau đó bất cứ khi nào sức khỏe thay đổi
human.HealthChanged:Connect(onHealthChanged)
onHealthChanged()
end
-- Kết nối trình lắng nghe xuất hiện của chúng tôi; gọi nó nếu đã xuất hiện
player.CharacterAdded:Connect(onCharacterAdded)
if player.Character then
onCharacterAdded(player.Character)
endTransparency
Visible
Mẫu mã
Cửa sổ UI
local gui = script.Parent
local window = gui:WaitForChild("Window")
local toggleButton = gui:WaitForChild("ToggleWindow")
local closeButton = window:WaitForChild("Close")
local function toggleWindowVisbility()
-- Đảo ngược một boolean bằng cách sử dụng từ khóa `not`
window.Visible = not window.Visible
end
toggleButton.Activated:Connect(toggleWindowVisbility)
closeButton.Activated:Connect(toggleWindowVisbility)Phương Pháp
TweenPosition
TweenSize
TweenSizeAndPosition
Sự Kiện
DragBegin
DragStopped
InputBegan
Tham Số
Mẫu mã
Theo dõi sự bắt đầu của đầu vào trên một GuiObject
-- Để sử dụng sự kiện InputBegan, bạn phải chỉ định GuiObject
local gui = script.Parent
-- Một hàm mẫu cung cấp nhiều trường hợp sử dụng cho các loại đầu vào của người dùng khác nhau
local function inputBegan(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
print("Một phím đang được nhấn xuống! Phím:", input.KeyCode)
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
print("Nút chuột trái đã được nhấn xuống tại", input.Position)
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
print("Nút chuột phải đã được nhấn xuống tại", input.Position)
elseif input.UserInputType == Enum.UserInputType.Touch then
print("Một đầu vào từ màn hình cảm ứng đã bắt đầu tại", input.Position)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
print("Một nút đang được nhấn trên gamepad! Nút:", input.KeyCode)
end
end
gui.InputBegan:Connect(inputBegan)InputChanged
Tham Số
Mẫu mã
Ví dụ về InputChanged của GuiObject
local UserInputService = game:GetService("UserInputService")
local gui = script.Parent
local function printMovement(input)
print("Vị trí:", input.Position)
print("Delta Di chuyển:", input.Delta)
end
local function inputChanged(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
print("Chuột đã được di chuyển!")
printMovement(input)
elseif input.UserInputType == Enum.UserInputType.MouseWheel then
print("Bánh xe chuột đã được cuộn!")
print("Di chuyển Bánh xe:", input.Position.Z)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
if input.KeyCode == Enum.KeyCode.Thumbstick1 then
print("Bánh analog trái đã được di chuyển!")
printMovement(input)
elseif input.KeyCode == Enum.KeyCode.Thumbstick2 then
print("Bánh analog phải đã được di chuyển!")
printMovement(input)
elseif input.KeyCode == Enum.KeyCode.ButtonL2 then
print("Áp lực được áp dụng lên cò trái đã thay đổi!")
print("Áp lực:", input.Position.Z)
elseif input.KeyCode == Enum.KeyCode.ButtonR2 then
print("Áp lực được áp dụng lên cò phải đã thay đổi!")
print("Áp lực:", input.Position.Z)
end
elseif input.UserInputType == Enum.UserInputType.Touch then
print("Ngón tay của người dùng đang di chuyển trên màn hình!")
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("Sự xoay của thiết bị di động của người dùng đã thay đổi!")
print("Vị trí", rotCFrame.p)
print("Xoay:", rot)
elseif input.UserInputType == Enum.UserInputType.Accelerometer then
print("Gia tốc của thiết bị di động của người dùng đã thay đổi!")
printMovement(input)
end
end
gui.InputChanged:Connect(inputChanged)InputEnded
Tham Số
Mẫu mã
Theo dõi sự kết thúc của đầu vào trên một GuiObject
-- Để sử dụng sự kiện InputChanged, bạn phải chỉ định một GuiObject
local gui = script.Parent
-- Một hàm mẫu cung cấp nhiều trường hợp sử dụng cho các loại đầu vào của người dùng khác nhau
local function inputEnded(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
print("Một phím đã được thả! Phím:", input.KeyCode)
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
print("Nút chuột trái đã được thả tại", input.Position)
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
print("Nút chuột phải đã được thả tại", input.Position)
elseif input.UserInputType == Enum.UserInputType.Touch then
print("Một đầu vào từ màn hình cảm ứng đã được thả tại", input.Position)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
print("Một nút đã được thả trên gamepad! Nút:", input.KeyCode)
end
end
gui.InputEnded:Connect(inputEnded)MouseEnter
Mẫu mã
In Ấn Nơi Chuột Vào Một GuiObject
local guiObject = script.Parent
guiObject.MouseEnter:Connect(function(x, y)
print("Con trỏ chuột của người dùng đã vào GuiObject tại vị trí", x, ",", y)
end)MouseWheelBackward
MouseWheelForward
SelectionGained
Mẫu mã
Xử lý lựa chọn GUI đã nhận
local guiObject = script.Parent
local function selectionGained()
print("Người dùng đã chọn nút này bằng gamepad.")
end
guiObject.SelectionGained:Connect(selectionGained)SelectionLost
Mẫu mã
Xử lý mất lựa chọn GUI
local guiObject = script.Parent
local function selectionLost()
print("Người dùng không còn chọn cái này bằng tay cầm của họ.")
end
guiObject.SelectionLost:Connect(selectionLost)TouchLongPress
Tham Số
Mẫu mã
Di chuyển phần tử UI với TouchLongPress
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
-- Bắt đầu kéo
dragging = true
basePosition = frame.Position
startTouchPosition = touchPositions[1]
-- Tô màu cho frame để chỉ ra rằng việc kéo đang diễn ra
borderColor3 = frame.BorderColor3
backgroundColor3 = frame.BackgroundColor3
frame.BorderColor3 = Color3.new(1, 1, 1) -- Trắng
frame.BackgroundColor3 = Color3.new(0, 0, 1) -- Xanh
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
-- Dừng kéo
dragging = false
frame.BorderColor3 = borderColor3
frame.BackgroundColor3 = backgroundColor3
end
end
frame.TouchLongPress:Connect(onTouchLongPress)TouchPan
GuiObject.TouchPan(
Tham Số
Mẫu mã
Yếu Tố Giao Diện Panning
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
GuiObject.TouchPinch(
Tham Số
Mẫu mã
Tỉ lệ Kéo/Giảm
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 -- Lưu ý phép nhân ở đây
elseif state == Enum.UserInputState.End and dragging then
dragging = false
outerFrame.BackgroundTransparency = 0.75
end
end
outerFrame.TouchPinch:Connect(onTouchPinch)TouchRotate
GuiObject.TouchRotate(
Tham Số
Mẫu mã
Xoay Chạm
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
Tham Số
Mẫu mã
Bảng Màu Nhảy
local frame = script.Parent
frame.Active = true
-- Khoảng cách mà khung nên nhảy lên khi vuốt thành công
local BOUNCE_DISTANCE = 50
-- Trạng thái hiện tại của khung
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)
-- Thay đổi BackgroundColor3 dựa trên hướng vuốt
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
-- Cập nhật màu sắc và làm cho khung nhảy một chút
updateColor()
frame.Position = basePosition + deltaPos
frame:TweenPosition(basePosition, Enum.EasingDirection.Out, Enum.EasingStyle.Bounce, 0.7, true)
end
frame.TouchSwipe:Connect(onTouchSwipe)
updateColor()TouchTap
Tham Số
Mẫu mã
Chạm để chuyển đổi độ trong suốt
local frame = script.Parent
frame.Active = true
local function onTouchTap()
-- Chuyển đổi độ trong suốt nền
if frame.BackgroundTransparency > 0 then
frame.BackgroundTransparency = 0
else
frame.BackgroundTransparency = 0.75
end
end
frame.TouchTap:Connect(onTouchTap)