Engine Class
GuiObject
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
สรุป
คุณสมบัติ
วิธีการ
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 |
เหตุการณ์
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 |
สมาชิกที่ได้รับทอด
สมาชิก 12 คนที่รับทอดมาจาก GuiBase2d
สมาชิก 57 คนที่รับทอดมาจาก Instance
สมาชิก 6 คนที่รับทอดมาจาก Object
ได้รับทอดโดย
เอกสารอ้างอิงเกี่ยวกับ API
คุณสมบัติ
Active
ตัวอย่างโค้ด
ปุ่มข้อความที่เปิดใช้งานแล้ว
-- วาง LocalScript นี้ใน TextButton (หรือ ImageButton)
local textButton = script.Parent
textButton.Text = "คลิกที่นี่"
textButton.Active = true
local function onActivated()
-- นี่ทำหน้าที่เหมือนไดบาวน์
textButton.Active = false
-- นับถอยหลังจาก 5
for i = 5, 1, -1 do
textButton.Text = "เวลา: " .. i
task.wait(1)
end
textButton.Text = "คลิกที่นี่"
textButton.Active = true
end
textButton.Activated:Connect(onActivated)AnchorPoint
ตัวอย่างโค้ด
สาธิต AnchorPoint
local guiObject = script.Parent
while true do
-- มุมซ้ายบน
guiObject.AnchorPoint = Vector2.new(0, 0)
guiObject.Position = UDim2.new(0, 0, 0, 0)
task.wait(1)
-- ด้านบน
guiObject.AnchorPoint = Vector2.new(0.5, 0)
guiObject.Position = UDim2.new(0.5, 0, 0, 0)
task.wait(1)
-- มุมขวาบน
guiObject.AnchorPoint = Vector2.new(1, 0)
guiObject.Position = UDim2.new(1, 0, 0, 0)
task.wait(1)
-- ด้านซ้าย
guiObject.AnchorPoint = Vector2.new(0, 0.5)
guiObject.Position = UDim2.new(0, 0, 0.5, 0)
task.wait(1)
-- ตรงกลาง
guiObject.AnchorPoint = Vector2.new(0.5, 0.5)
guiObject.Position = UDim2.new(0.5, 0, 0.5, 0)
task.wait(1)
-- ด้านขวา
guiObject.AnchorPoint = Vector2.new(1, 0.5)
guiObject.Position = UDim2.new(1, 0, 0.5, 0)
task.wait(1)
-- มุมซ้ายล่าง
guiObject.AnchorPoint = Vector2.new(0, 1)
guiObject.Position = UDim2.new(0, 0, 1, 0)
task.wait(1)
-- ด้านล่าง
guiObject.AnchorPoint = Vector2.new(0.5, 1)
guiObject.Position = UDim2.new(0.5, 0, 1, 0)
task.wait(1)
-- มุมขวาล่าง
guiObject.AnchorPoint = Vector2.new(1, 1)
guiObject.Position = UDim2.new(1, 0, 1, 0)
task.wait(1)
endAutomaticSize
ตัวอย่างโค้ด
LocalScript ใน ScreenGui
-- อาร์เรย์ของป้ายชื่อ/แบบอักษร/ขนาดที่จะนำเสนอ
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 },
}
-- สร้างกรอบหลักที่มีขนาดอัตโนมัติ
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
-- เพิ่มเค้าโครงรายการ
local listLayout = Instance.new("UIListLayout")
listLayout.Padding = UDim.new(0, 5)
listLayout.Parent = parentFrame
-- ตั้งค่ามุมมนและช่องว่างเพื่อความสวยงาม
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
-- สร้างป้ายชื่อข้อความที่มีขนาดอัตโนมัติจากอาร์เรย์
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
-- ความสวยงาม
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
ตัวอย่างโค้ด
กรอบสายรุ้ง
-- ใส่โค้ดนี้ใน LocalScript ในกรอบ
local frame = script.Parent
while true do
for hue = 0, 255, 4 do
-- HSV = สี, ความอิ่มตัว, ค่า
-- ถ้าเราหมุนจาก 0 ถึง 1 ซ้ำ ๆ เราจะได้รับสายรุ้ง!
frame.BorderColor3 = Color3.fromHSV(hue / 256, 1, 1)
frame.BackgroundColor3 = Color3.fromHSV(hue / 256, 0.5, 0.8)
task.wait()
end
endBorderColor
BorderColor3
ตัวอย่างโค้ด
ปุ่มไฮไลต์
-- ใส่ฉันไว้ใน GuiObject โดยเฉพาะอย่างยิ่งใน ImageButton/TextButton
local button = script.Parent
local function onEnter()
button.BorderSizePixel = 2
button.BorderColor3 = Color3.new(1, 1, 0) -- สีเหลือง
end
local function onLeave()
button.BorderSizePixel = 1
button.BorderColor3 = Color3.new(0, 0, 0) -- สีดำ
end
-- เชื่อมต่อเหตุการณ์
button.MouseEnter:Connect(onEnter)
button.MouseLeave:Connect(onLeave)
-- สถานะเริ่มต้นของเราคือ "ไม่ได้วางหรือเลื่อนเมาส์"
onLeave()BorderSizePixel
ตัวอย่างโค้ด
ปุ่มไฮไลต์
-- ใส่ฉันไว้ใน GuiObject โดยเฉพาะอย่างยิ่งใน ImageButton/TextButton
local button = script.Parent
local function onEnter()
button.BorderSizePixel = 2
button.BorderColor3 = Color3.new(1, 1, 0) -- สีเหลือง
end
local function onLeave()
button.BorderSizePixel = 1
button.BorderColor3 = Color3.new(0, 0, 0) -- สีดำ
end
-- เชื่อมต่อเหตุการณ์
button.MouseEnter:Connect(onEnter)
button.MouseLeave:Connect(onLeave)
-- สถานะเริ่มต้นของเราคือ "ไม่ได้วางหรือเลื่อนเมาส์"
onLeave()Draggable
NextSelectionDown
ตัวอย่างโค้ด
การสร้างกริดการเลือก Gamepad
-- ตั้งค่ากริดการเลือก Gamepad โดยใช้โค้ดด้านล่าง
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
-- ขอบซ้าย
gui.NextSelectionLeft = container:FindFirstChild(pos - 1)
-- ขอบขวา
gui.NextSelectionRight = container:FindFirstChild(pos + 1)
-- ด้านบน
gui.NextSelectionUp = container:FindFirstChild(pos - rowSize)
-- ด้านล่าง
gui.NextSelectionDown = container:FindFirstChild(pos + rowSize)
end
end
-- ทดสอบกริดการเลือก Gamepad โดยใช้โค้ดด้านล่าง
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
ตัวอย่างโค้ด
การสร้างกริดการเลือก Gamepad
-- ตั้งค่ากริดการเลือก Gamepad โดยใช้โค้ดด้านล่าง
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
-- ขอบซ้าย
gui.NextSelectionLeft = container:FindFirstChild(pos - 1)
-- ขอบขวา
gui.NextSelectionRight = container:FindFirstChild(pos + 1)
-- ด้านบน
gui.NextSelectionUp = container:FindFirstChild(pos - rowSize)
-- ด้านล่าง
gui.NextSelectionDown = container:FindFirstChild(pos + rowSize)
end
end
-- ทดสอบกริดการเลือก Gamepad โดยใช้โค้ดด้านล่าง
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
ตัวอย่างโค้ด
การสร้างกริดการเลือก Gamepad
-- ตั้งค่ากริดการเลือก Gamepad โดยใช้โค้ดด้านล่าง
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
-- ขอบซ้าย
gui.NextSelectionLeft = container:FindFirstChild(pos - 1)
-- ขอบขวา
gui.NextSelectionRight = container:FindFirstChild(pos + 1)
-- ด้านบน
gui.NextSelectionUp = container:FindFirstChild(pos - rowSize)
-- ด้านล่าง
gui.NextSelectionDown = container:FindFirstChild(pos + rowSize)
end
end
-- ทดสอบกริดการเลือก Gamepad โดยใช้โค้ดด้านล่าง
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
ตัวอย่างโค้ด
การสร้างกริดการเลือก Gamepad
-- ตั้งค่ากริดการเลือก Gamepad โดยใช้โค้ดด้านล่าง
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
-- ขอบซ้าย
gui.NextSelectionLeft = container:FindFirstChild(pos - 1)
-- ขอบขวา
gui.NextSelectionRight = container:FindFirstChild(pos + 1)
-- ด้านบน
gui.NextSelectionUp = container:FindFirstChild(pos - rowSize)
-- ด้านล่าง
gui.NextSelectionDown = container:FindFirstChild(pos + rowSize)
end
end
-- ทดสอบกริดการเลือก Gamepad โดยใช้โค้ดด้านล่าง
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
ตัวอย่างโค้ด
การจำกัดการเลือก 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
-- เหตุการณ์ FocusLost และ FocusGained จะถูกเรียกใช้เพราะ textBox
-- เป็นชนิด TextBox
textBox.Focused:Connect(gainFocus)
textBox.FocusLost:Connect(loseFocus)Size
ตัวอย่างโค้ด
แถบสุขภาพ
local Players = game:GetService("Players")
local player = Players.LocalPlayer
-- วางสคริปต์ลงใน LocalScript ที่
-- ถูกทำให้เป็นพ่อของเฟรมภายในเฟรม
local frame = script.Parent
local container = frame.Parent
container.BackgroundColor3 = Color3.new(0, 0, 0) -- สีดำ
-- ฟังก์ชันนี้จะถูกเรียกเมื่อสุขภาพของมนุษย์เปลี่ยนแปลง
local function onHealthChanged()
local human = player.Character.Humanoid
local percent = human.Health / human.MaxHealth
-- เปลี่ยนขนาดของแถบภายใน
frame.Size = UDim2.new(percent, 0, 1, 0)
-- เปลี่ยนสีของแถบสุขภาพ
if percent < 0.1 then
frame.BackgroundColor3 = Color3.new(1, 0, 0) -- สีดำ
elseif percent < 0.4 then
frame.BackgroundColor3 = Color3.new(1, 1, 0) -- สีเหลือง
else
frame.BackgroundColor3 = Color3.new(0, 1, 0) -- สีเขียว
end
end
-- ฟังก์ชันนี้จะถูกเรียกเมื่อผู้เล่นเกิดขึ้น
local function onCharacterAdded(character)
local human = character:WaitForChild("Humanoid")
-- รูปแบบ: อัปเดตหนึ่งครั้งตอนนี้ จากนั้นทุกครั้งที่สุขภาพเปลี่ยนแปลง
human.HealthChanged:Connect(onHealthChanged)
onHealthChanged()
end
-- เชื่อมต่อผู้ฟังการเกิดของเรา; เรียกใช้งานหากเกิดขึ้นแล้ว
player.CharacterAdded:Connect(onCharacterAdded)
if player.Character then
onCharacterAdded(player.Character)
endTransparency
Visible
ตัวอย่างโค้ด
หน้าต่าง UI
local gui = script.Parent
local window = gui:WaitForChild("Window")
local toggleButton = gui:WaitForChild("ToggleWindow")
local closeButton = window:WaitForChild("Close")
local function toggleWindowVisbility()
-- สลับค่าจริงด้วยคำสั่ง `not`
window.Visible = not window.Visible
end
toggleButton.Activated:Connect(toggleWindowVisbility)
closeButton.Activated:Connect(toggleWindowVisbility)วิธีการ
TweenPosition
TweenSize
TweenSizeAndPosition
เหตุการณ์
DragBegin
DragStopped
InputBegan
พารามิเตอร์
ตัวอย่างโค้ด
ติดตามการเริ่มต้นของการป้อนข้อมูลบน GuiObject
-- เพื่อใช้เหตุการณ์ InputBegan คุณต้องระบุ GuiObject
local gui = script.Parent
-- ฟังก์ชันตัวอย่างที่ให้กรณีการใช้งานหลายกรณีสำหรับประเภทต่าง ๆ ของการป้อนข้อมูลผู้ใช้
local function inputBegan(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
print("มีการกดปุ่มลง! ปุ่ม:", input.KeyCode)
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
print("ปุ่มเมาส์ด้านซ้ายถูกกดลงที่", input.Position)
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
print("ปุ่มเมาส์ด้านขวาถูกกดลงที่", input.Position)
elseif input.UserInputType == Enum.UserInputType.Touch then
print("มีการป้อนข้อมูลจากหน้าจอสัมผัสเริ่มต้นที่", input.Position)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
print("มีการกดปุ่มบนเกมแพด! ปุ่ม:", input.KeyCode)
end
end
gui.InputBegan:Connect(inputBegan)InputChanged
พารามิเตอร์
ตัวอย่างโค้ด
การสาธิต InputChanged ของ GuiObject
local UserInputService = game:GetService("UserInputService")
local gui = script.Parent
local function printMovement(input)
print("ตำแหน่ง:", input.Position)
print("การเคลื่อนไหว Delta:", input.Delta)
end
local function inputChanged(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
print("เมาส์ถูกเคลื่อนไหว!")
printMovement(input)
elseif input.UserInputType == Enum.UserInputType.MouseWheel then
print("ล้อเมาส์ถูกเลื่อน!")
print("การเคลื่อนไหวของล้อ:", input.Position.Z)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
if input.KeyCode == Enum.KeyCode.Thumbstick1 then
print("ทัชช์ที่ซ้ายถูกเคลื่อนไหว!")
printMovement(input)
elseif input.KeyCode == Enum.KeyCode.Thumbstick2 then
print("ทัชช์ที่ขวาถูกเคลื่อนไหว!")
printMovement(input)
elseif input.KeyCode == Enum.KeyCode.ButtonL2 then
print("แรงที่ถูกกดบนทริกเกอร์ซ้ายมีการเปลี่ยนแปลง!")
print("แรงกด:", input.Position.Z)
elseif input.KeyCode == Enum.KeyCode.ButtonR2 then
print("แรงที่ถูกกดบนทริกเกอร์ขวามีการเปลี่ยนแปลง!")
print("แรงกด:", input.Position.Z)
end
elseif input.UserInputType == Enum.UserInputType.Touch then
print("นิ้วของผู้ใช้กำลังเคลื่อนที่บนหน้าจอ!")
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("การหมุนของอุปกรณ์มือถือของผู้ใช้มีการเปลี่ยนแปลง!")
print("ตำแหน่ง", rotCFrame.p)
print("การหมุน:", rot)
elseif input.UserInputType == Enum.UserInputType.Accelerometer then
print("การเร่งของอุปกรณ์มือถือของผู้ใช้มีการเปลี่ยนแปลง!")
printMovement(input)
end
end
gui.InputChanged:Connect(inputChanged)InputEnded
พารามิเตอร์
ตัวอย่างโค้ด
ติดตามการสิ้นสุดของอินพุตบน GuiObject
-- เพื่อใช้เหตุการณ์ InputChanged คุณต้องระบุ GuiObject
local gui = script.Parent
-- ฟังก์ชันตัวอย่างที่ให้กรณีการใช้งานหลายประการสำหรับประเภทต่าง ๆ ของอินพุตของผู้ใช้
local function inputEnded(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
print("ปุ่มถูกปล่อยแล้ว! คีย์:", input.KeyCode)
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
print("ปุ่มเมาส์ซ้ายถูกปล่อยที่", input.Position)
elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
print("ปุ่มเมาส์ขวาถูกปล่อยที่", input.Position)
elseif input.UserInputType == Enum.UserInputType.Touch then
print("อินพุตจอสัมผัสถูกปล่อยที่", input.Position)
elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
print("ปุ่มถูกปล่อยบนเกมแพด! ปุ่ม:", input.KeyCode)
end
end
gui.InputEnded:Connect(inputEnded)MouseEnter
ตัวอย่างโค้ด
การพิมพ์เมื่อเมาส์เข้ามาใน GuiObject
local guiObject = script.Parent
guiObject.MouseEnter:Connect(function(x, y)
print("เคอร์เซอร์เมาส์ของผู้ใช้ได้เข้ามาที่ GuiObject ที่ตำแหน่ง", x, ",", y)
end)MouseWheelBackward
MouseWheelForward
SelectionGained
ตัวอย่างโค้ด
การจัดการการเลือก GUI ที่ได้รับ
local guiObject = script.Parent
local function selectionGained()
print("ผู้ใช้ได้เลือกปุ่มนี้ด้วยเกมแพด。")
end
guiObject.SelectionGained:Connect(selectionGained)SelectionLost
ตัวอย่างโค้ด
การจัดการการเลือก GUI ที่หายไป
local guiObject = script.Parent
local function selectionLost()
print("ผู้ใช้ไม่ได้เลือกสิ่งนี้ด้วยเกมแพดอีกต่อไป.")
end
guiObject.SelectionLost:Connect(selectionLost)TouchLongPress
พารามิเตอร์
ตัวอย่างโค้ด
ย้าย UI Element ด้วยการกดนิ้วค้าง
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
-- เริ่มต้นการลาก
dragging = true
basePosition = frame.Position
startTouchPosition = touchPositions[1]
-- เปลี่ยนสีของเฟรมเพื่อแสดงว่ากำลังลากอยู่
borderColor3 = frame.BorderColor3
backgroundColor3 = frame.BackgroundColor3
frame.BorderColor3 = Color3.new(1, 1, 1) -- ขาว
frame.BackgroundColor3 = Color3.new(0, 0, 1) -- น้ำเงิน
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
-- หยุดการลาก
dragging = false
frame.BorderColor3 = borderColor3
frame.BackgroundColor3 = backgroundColor3
end
end
frame.TouchLongPress:Connect(onTouchLongPress)TouchPan
GuiObject.TouchPan(
พารามิเตอร์
ตัวอย่างโค้ด
องค์ประกอบ UI ที่ปรับพาน
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(
พารามิเตอร์
ตัวอย่างโค้ด
การปรับขนาดด้วยการบีบหรือดึง
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 -- สังเกตการคูณที่นี่
elseif state == Enum.UserInputState.End and dragging then
dragging = false
outerFrame.BackgroundTransparency = 0.75
end
end
outerFrame.TouchPinch:Connect(onTouchPinch)TouchRotate
GuiObject.TouchRotate(
พารามิเตอร์
ตัวอย่างโค้ด
การหมุนแบบสัมผัส
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
พารามิเตอร์
ตัวอย่างโค้ด
ตัวเลือกสีที่เด้ง
local frame = script.Parent
frame.Active = true
-- ระยะที่เฟรมควรเด้งเมื่อมีการเลื่อนสำเร็จ
local BOUNCE_DISTANCE = 50
-- สถานะปัจจุบันของเฟรม
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)
-- เปลี่ยน BackgroundColor3 ตามทิศทางการเลื่อน
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
-- อัปเดตสีและทำให้เฟรมเด้งเล็กน้อย
updateColor()
frame.Position = basePosition + deltaPos
frame:TweenPosition(basePosition, Enum.EasingDirection.Out, Enum.EasingStyle.Bounce, 0.7, true)
end
frame.TouchSwipe:Connect(onTouchSwipe)
updateColor()TouchTap
พารามิเตอร์
ตัวอย่างโค้ด
ปุ่มสลับความโปร่งใส
local frame = script.Parent
frame.Active = true
local function onTouchTap()
-- สลับความโปร่งใสของพื้นหลัง
if frame.BackgroundTransparency > 0 then
frame.BackgroundTransparency = 0
else
frame.BackgroundTransparency = 0.75
end
end
frame.TouchTap:Connect(onTouchTap)