GuiObject
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
GuiObject เป็นคลาสที่เป็นนามธรรม (คล้ายกับ BasePart ) สำหรับวัตถุอินเทอร์เฟซผู้ใช้ 2Dมันกำหนดคุณสมบัติทั้งหมดที่เกี่ยวข้องกับการแสดงผลของอินเทอร์เฟซผู้ใช้กราฟิก (GUI) เช่น Size และ Positionนอกจากนี้ยังมีคุณสมบัติอ่านได้ที่มีประโยชน์บางอย่างเช่น AbsolutePosition , AbsoluteSize และ AbsoluteRotation
เพื่อควบคุมเลย์เอาต์ของวัตถุ GUI ในลักษณะพิเศษ คุณสามารถใช้โครงสร้างเลย์เอาต์เช่น รายการ/ยืดหยุ่น หรือ กริด และคุณสามารถตกแต่งพวกเขาเกินคุณสมบัติหลักของพวกเขาผ่าน ตัวแก้ไขรูปลักษณ์
แม้ว่าจะเป็นไปได้ที่จะตรวจจับเหตุการณ์ปุ่มเมาส์บนวัตถุ GUI ใดๆ โดยใช้ InputBegan และ InputEnded แต่เฉพาะ ImageButton และ TextButton เท่านั้นที่มีกิจกรรมที่สะดวกสำหรับการคลิก/กดเช่น Activated เพื่อตรวจจับ
สรุป
คุณสมบัติ
กำหนดว่าองค์ประกอบ UI นี้จะรับอินพุตหรือไม่
กำหนดจุดต้นกำเนิดของ GuiObject เมื่อเทียบกับขนาดสัมบูรณ์ของมัน
กำหนดว่าการปรับขนาดจะเกิดขึ้นตามเนื้อหาลูกหรือไม่
กำหนดสีพื้นหลัง GuiObject
กำหนดความโปร่งใสของพื้นหลังและขอบ GuiObject
กำหนดสีขอบ GuiObject ได้
กำหนดวิธีที่ขอบ GuiObject ถูกวางในลักษณะที่เกี่ยวข้องกับขนาด
กำหนดความกว้างของพิกเซลของขอบ GuiObject
กำหนดว่าถ้าลูกหลาน GuiObjects นอกขอบเขตขององค์ประกอบ GUI พ่อจะต้องแสดงผลหรือไม่
กำหนดว่าเมาส์ของผู้เล่นกดอยู่อย่างใดอย่างหนึ่งบน GuiObject หรือไม่
กำหนดว่า GuiButton สามารถโต้ตอบได้หรือไม่ หรือถ้า GuiState ของ GuiObject กำลังเปลี่ยนแปลงหรือไม่
ควบคุมลำดับการจัดเรียงของ GuiObject เมื่อใช้กับ UIGridStyleLayout
ตั้งค่า GuiObject ซึ่งจะถูกเลือกเมื่อตัวเลือกเกมแพดถูกเลื่อนลง
ตั้งค่า GuiObject ซึ่งจะถูกเลือกเมื่อตัวเลือกเกมแพดถูกย้ายไปทางซ้าย
ตั้งค่า GuiObject ซึ่งจะถูกเลือกเมื่อตัวเลือกเกมแพดถูกย้ายไปทางขวา
ตั้งค่า GuiObject ซึ่งจะถูกเลือกเมื่อตัวเลือกเกมแพดถูกย้ายขึ้น
กำหนดพิกเซลและตําแหน่งเวกเตอร์ของ GuiObject
กำหนดจำนวนองศาที่ GuiObject หมุนไป
ตรวจสอบว่า GuiObject สามารถเลือกได้โดยคอนโซลเกมหรือไม่
แทนที่การเลือกเริ่มต้นที่ใช้สำหรับเกมแพด
ลำดับของ GuiObjects ที่เลือกโดยการเลือก UI ของเกมแพด
กำหนดพิกเซลและขนาดสเกลาร์ของ GuiObject
ตั้งแกน Size ที่ GuiObject จะอ้างอิงจากขนาดของพ่อของมัน เมื่อเทียบกับขนาดของพ่อ
คุณสมบัติผสมของ BackgroundTransparency และ TextTransparency .
กำหนดว่า GuiObject และบรรดาลูกหลานจะถูกแสดงหรือไม่
กำหนดลำดับที่เรนเดอร์ GuiObject จะแสดงเป็นค่าสัมพันธ์กับคนอื่น
อธิบายตำแหน่งจอแสดงจริงขององค์ประกอบ GuiBase2d ในพิกเซล
อธิบายการหมุนหน้าจอที่เกิดขึ้นจริงขององค์ประกอบ GuiBase2d ในองศา
อธิบายขนาดหน้าจอที่เป็นจริงขององค์ประกอบ GuiBase2d ในพิกเซล
เมื่อตั้งค่าเป็น true การแปลท้องถิ่นจะถูกใช้กับ GuiBase2d และบรรดาลูกหลานของมัน
การอ้างอิงถึง LocalizationTable ที่จะใช้เพื่อใช้การแปลงภาษาท้องถิ่นอัตโนมัติกับ GuiBase2d และบรรดาลูกหลานของมัน
ปรับพฤติกรรมการเลือกเกมแพดในทิศทางลง
ปรับพฤติกรรมการเลือกเกมแพดในทิศทางซ้าย
ปรับพฤติกรรมการเลือก gamepad ในทิศทางที่ถูกต้อง
ปรับพฤติกรรมการเลือกเกมแพดในทิศทางขึ้น
อนุญาตให้ปรับแต่งการเคลื่อนไหวการเลือกเกมแพดได้
วิธีการ
- TweenPosition(endPosition : UDim2,easingDirection : Enum.EasingDirection,easingStyle : Enum.EasingStyle,time : number,override : boolean,callback : function):boolean
เคลื่อน GUI ไปยังใหม่ 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
เคลื่อน GUI ไปยังขนาดและตำแหน่งใหม่อย่างราบรื่น
อีเวนต์
ยิงเมื่อผู้ใช้เริ่มโต้ตอบผ่านอุปกรณ์อินเทอร์เฟซมนุษย์-คอมพิวเตอร์ (ปุ่มเมาส์ลง, แตะเริ่ม, ปุ่มแป้นพิมพ์ลง, เป็นต้น)
ยิงเมื่อผู้ใช้เปลี่ยนวิธีการโต้ตอบผ่านอุปกรณ์อินเทอร์เฟซมนุษย์-คอมพิวเตอร์ (ปุ่มเมาส์ลง, แตะเริ่ม, ปุ่มแป้นพิมพ์ลง, เป็นต้น)
ยิงเมื่อผู้ใช้หยุดโต้ตอบผ่านอุปกรณ์อินเทอร์เฟซมนุษย์-คอมพิวเตอร์ (ปุ่มเมาส์ลง, แตะเริ่ม, ปุ่มแป้นพิมพ์ลง, เป็นต้น)
เกิดไฟไหม้เมื่อผู้ใช้ย้ายเมาส์ไปยังองค์ประกอบ GUI
เกิดไฟไหม้เมื่อผู้ใช้ย้ายเมาส์ออกจากองค์ประกอบ GUI
เกิดไฟไหม้เมื่อผู้ใช้ย้ายเมาส์ในขณะที่อยู่ภายในองค์ประกอบ GUI ใดๆ
เกิดไฟไหม้เมื่อผู้ใช้เลื่อนล้อเมาส์กลับเมื่อเมาส์อยู่เหนือองค์ประกอบ GUI
เกิดไฟไหม้เมื่อผู้ใช้เลื่อนล้อเมาส์ไปข้างหน้าเมื่อเมาส์อยู่เหนือองค์ประกอบ GUI
ยิงเมื่อ GuiObject ได้รับการโฟกัสด้วยตัวเลือก Gamepad
ยิงเมื่อตัวเลือก Gamepad หยุดโฟกัสกับ GuiObject
เกิดไฟไหม้เมื่อผู้เล่นเริ่มต้น ดำเนินการต่อ และหยุดการกดค้างไว้นานบนองค์ประกอบ UI
- TouchPan(touchPositions : Array,totalTranslation : Vector2,velocity : Vector2,state : Enum.UserInputState):RBXScriptSignal
เกิดไฟไหม้เมื่อผู้เล่นย้ายนิ้วบนองค์ประกอบ UI
- TouchPinch(touchPositions : Array,scale : number,velocity : number,state : Enum.UserInputState):RBXScriptSignal
เกิดไฟไหม้เมื่อผู้เล่นดําเนินการสับหรือดึงด้วยนิ้วสองนิ้วบนองค์ประกอบ UI
- TouchRotate(touchPositions : Array,rotation : number,velocity : number,state : Enum.UserInputState):RBXScriptSignal
เกิดไฟไหม้เมื่อผู้เล่นดําเนินการทําท่าหมุนโดยใช้นิ้วสองนิ้วบนองค์ประกอบ UI
เกิดไฟไหม้เมื่อผู้เล่นดําเนินการสับข้อมูลบนองค์ประกอบ UI
เกิดไฟไหม้เมื่อผู้เล่นดําเนินการสัมผัสท่าทางบนองค์ประกอบ UI
- SelectionChanged(amISelected : boolean,previousSelection : GuiObject,newSelection : GuiObject):RBXScriptSignal
ไฟเมื่อการเลือกเกมแพดย้ายไปที่ ออก หรือเปลี่ยนภายในเชื่อมต่อ GuiBase2d หรือทายาทใดๆ GuiObjects
คุณสมบัติ
Active
คุณสมบัตินี้กำหนดว่า GuiObject จะส่งอินพุตไปยังพื้นที่ 3D หรือไม่ เช่น โมเดลพื้นฐานที่มีคลาส ClickDetector เช่น DragDetector
สำหรับวัตถุ GuiButton ( ImageButton และ TextButton ), คุณสมบัตินี้จะกำหนดว่าจะมีการเผาไหม้ Activated ( AutoButtonColor จะยังคงทำงานสำหรับผู้คนเหล่านั้นด้วย)กิจกรรม InputBegan , InputChanged และ InputEnded ทำงานตามปกติไม่ว่าค่าของคุณสมบัตินี้จะเป็นอย่างไร
ตัวอย่างโค้ด
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
คุณสมบัตินี้กำหนดจุดต้นกำเนิดของ GuiObject เมื่อเทียบกับขนาดสัมบูรณ์ของมันจุดต้นกำเนิดกำหนดจากที่ที่องค์ประกอบตั้งอยู่ (ผ่าน GuiObject.Position ) และจากที่ที่เรนเดอร์ GuiObject.Size ขยาย
ดู ที่นี่ สำหรับภาพวาดและรายละเอียดที่สร้างขึ้น
ตัวอย่างโค้ด
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
คุณสมบัตินี้ใช้เพื่อกำหนดขนาด UI รากโดยอัตโนมัติตามขนาดของบุตรหลานของมันคุณสามารถใช้คุณสมบัตินี้เพื่อเพิ่มข้อความและเนื้อหาอื่นๆ ในวัตถุ UI ได้อย่างไดนามิกในเวลาแก้ไขหรือรันเวลา และขนาดจะปรับให้พอดีกับเนื้อหานั้น
เมื่อ AutomaticSize ถูกตั้งค่าเป็น Enum.AutomaticSize ค่าให้กับสิ่งอื่นใดนอกเหนือจาก None วัตถุ UI นี้อาจปรับขนาดได้ขึ้นอยู่กับเนื้อหาลูก
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับวิธีการใช้คุณสมบัตินี้และวิธีที่มันทำงานโปรดดู ที่นี่ .
ตัวอย่างโค้ด
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
คุณสมบัตินี้กำหนดสีของพื้นหลัง GuiObject (สีเติม)หากองค์ประกอบของคุณมีข้อความ เช่น TextBox , TextButton หรือ TextLabel , ตรวจสอบให้แน่ใจว่าสีพื้นหลังของคุณสอดคล้องกับสีข้อความ
คุณสมบัติอื่นที่กำหนดคุณสมบัติทางภาพของพื้นหลังคือ GuiObject.BackgroundTransparency ; หากตั้งค่าเป็น 1 พื้นหลังและขอบเขตจะไม่แสดงผล
ดูเพิ่มเติม BorderColor3 .
ตัวอย่างโค้ด
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
คุณสมบัตินี้กำหนดความโปร่งใสของพื้นหลังและขอบ GuiObjectอย่างไรก็ตามมันไม่ได้กำหนดความโปร่งใสของข้อความถ้า GUI เป็น TextBox , TextButton หรือ TextLabel ; ความโปร่งใสของข้อความจะถูกกำหนด TextBox.TextTransparency , TextButton.TextTransparency และ TextLabel.TextTransparency ตามลำดับ
หากคุณสร้างคุณสมบัตินี้เป็น 1 ไม่มีพื้นหลังหรือขอบเขตจะไม่แสดงและพื้นหลัง GUI จะโปร่งใสอย่างสมบูรณ์
BorderColor3
กำหนดสีขอบสี่เหลี่ยมผืนผ้า GuiObject (ที่รู้จักกันในนามสีขอบ)นี้แยกจาก GuiObject.BackgroundColor3 ของวัตถุคุณจะไม่สามารถเห็นขอบวัตถุได้หากคุณสมบัติ GuiObject.BorderSizePixel ของวัตถุถูกตั้งค่าเป็น 0
โปรดทราบว่าส่วนประกอบ UIStroke สามารถรองรับเอฟเฟกต์เส้นขอบที่ซับซ้อนมากขึ้น
ตัวอย่างโค้ด
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
คุณสมบัตินี้กำหนดวิธีที่ขอบ GuiObject ถูกวางในแบบสัมพันธ์กับขนาดของมันโดยใช้รายการของชื่อเดียวกัน, Enum.BorderMode
โปรดทราบว่า UIStroke สามารถยกเลิกคุณสมบัตินี้และอนุญาตให้มีเอฟเฟกต์เส้นขอบที่ซับซ้อนมากขึ้น
BorderSizePixel
คุณสมบัตินี้กำหนดความกว้างของการเรนเดอร์ขอบ GuiObject ในพิกเซล การตั้งค่านี้เป็น 0 จะปิดใช้งานการเรนเดอร์ขอบทั้งหมด
โปรดทราบว่า UIStroke สามารถยกเลิกคุณสมบัตินี้และอนุญาตให้มีเอฟเฟกต์เส้นขอบที่ซับซ้อนมากขึ้น
ตัวอย่างโค้ด
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
คุณสมบัตินี้จะกำหนดว่า GuiObject จะตัด (ทำให้มองไม่เห็น) ส่วนใดส่วนหนึ่งขององค์ประกอบ GUI ลูกโซ่ที่จะไม่อยู่นอกขอบสี่เหลี่ยม
โปรดทราบว่า Rotation ไม่ได้รับการสนับสนุนโดยคุณสมบัตินี้หาก GUI นี้หรือบรรพบุรุษใดมี ไม่เป็นศูนย์ คุณสมบัตินี้จะถูกเพิกเฉยและองค์ประกอบ GUI ลูกหลานจะถูกแสดงออกไม่ว่าค่าของคุณสมบัตินี้จะเป็นเท่าใด
GuiState
เมื่อนิ้วของผู้เล่นถูกแตะและถือไว้บน GuiObject แล้ว GuiState ของ GuiObject จะถูกตั้งเป็น Pressเช่นเดียวกันเมื่อนิ้วของผู้เล่นถูกปล่อยออกจาก GuiObject แล้ว GuiState ของ GuiObject จะถูกตั้งเป็น Idle และเมื่อ Interactable ถูกปิดใน GuiObject แล้ว Class.GuiState ของ GuiObject จะถูกตั้งเป็น NonInteractable
Interactable
กำหนดว่า GuiButton สามารถโต้ตอบได้หรือไม่ หรือถ้า GuiState ของ GuiObject กำลังเปลี่ยนแปลงหรือไม่
บน GuiButton :
- เมื่อการตั้งค่า Interactable บน GuiButton ถูกตั้งค่าเป็น false แล้ว คุณจะไม่สามารถกดหรือคลิกที่ GuiButton ได้อีกต่อไป และ GuiState จะถูกตั้งค่าอย่างต่อเนื่องเป็น NonInteractable
- เมื่อการตั้งค่า Interactable บน GuiButton ถูกตั้งค่าเป็น true แล้ว ตัวเลือก GuiButton จะทำงานตามปกติอีกครั้ง และตัวเลือก GuiState จะทำงานตามปกติ
บน GuiObject :
- เมื่อการตั้งค่า Interactable บน GuiButton ถูกตั้งค่าเป็น false ตัวเลือก GuiState จะถูกตั้งค่าอย่างต่อเนื่องเป็น NonInteractable
- เมื่อการตั้งค่า Interactable บน GuiButton ถูกตั้งค่าเป็น true แล้ว ตัวเลือก GuiState จะทำงานตามปกติอีกครั้ง
LayoutOrder
คุณสมบัตินี้ควบคุมลำดับการจัดเรียงของ GuiObject เมื่อใช้ UIGridStyleLayout (เช่น UIListLayout หรือ UIPageLayout ) ด้วย SortOrder ตั้งค่าเป็น Enum.SortOrder.LayoutOrderมันไม่มีฟังก์ชันถ้าวัตถุไม่มีโครงสร้าง UI รุ่นพี่
GuiObjects จัดเรียงตามลําดับเพิ่มขึ้นเมื่อมีค่าต่ำกว่าได้รับความสําคัญมากกว่าค่าสูงวัตถุที่มีค่าเท่ากันจะกลับไปที่ลำดับที่พวกเขาถูกเพิ่มเข้ามา
หากคุณไม่แน่ใจว่าจะต้องเพิ่มองค์ประกอบระหว่างองค์ประกอบที่มีอยู่สองอย่างในอนาคตหรือไม่ เป็นการดีที่จะใช้หลายครั้งของ 100 ( 0 , 100 , 200 , เป็นต้น)สิ่งนี้ช่วยให้มีช่องว่างขนาดใหญ่ของค่าลำดับการจัดเตรียมที่คุณสามารถใช้สำหรับองค์ประกอบที่สั่งในระหว่างองค์ประกอบอื่นๆ
ดูเพิ่มเติม ZIndex ซึ่งกำหนดลำดับการเรนเดอร์ของวัตถุ rendering แทนการจัดเรียงตามลำดับ
NextSelectionDown
คุณสมบัตินี้ตั้งค่า GuiObject ที่เลือกเมื่อผู้ใช้ย้ายตัวเลือก gamepad ลงหากคุณสมบัตินี้ว่างเปล่า, การย้ายแป้นจับลงจะไม่เปลี่ยน GUI ที่เลือก
การย้ายตัวเลือกเกมแพดลงไปตั้ง GuiService.SelectedObject ให้กับวัตถุนี้ยกเว้น GUI ไม่ใช่ Selectableโปรดทราบว่าคุณสมบัตินี้สามารถตั้งค่าเป็นองค์ประกอบ GUI ได้แม้ว่ามันจะไม่ใช่ Selectable ดังนั้นคุณควรตรวจสอบให้แน่ใจว่ามูลค่าของคุณสมบัติที่เลือกได้ของ GUI ตรงกับพฤติกรรมที่คาดไว้
ดูเพิ่มเติม NextSelectionUp , NextSelectionLeft , และ NextSelectionRight .
ตัวอย่างโค้ด
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
คุณสมบัตินี้ตั้งค่า GuiObject ที่เลือกเมื่อผู้ใช้ย้ายตัวเลือก gamepad ไปทางซ้ายหากคุณสมบัตินี้ว่างเปล่า, การย้ายแป้นจับเวลาไปทางซ้ายจะไม่เปลี่ยน GUI ที่เลือก
การย้ายตัวเลือกเกมไปทางซ้ายตั้งค่า GuiService.SelectedObject ให้กับวัตถุนี้ยกเว้น GUI ไม่ใช่ Selectableโปรดทราบว่าคุณสมบัตินี้สามารถตั้งค่าเป็นองค์ประกอบ GUI ได้แม้ว่ามันจะไม่ใช่ Selectable ดังนั้นคุณควรตรวจสอบให้แน่ใจว่ามูลค่าของคุณสมบัติที่เลือกได้ของ GUI ตรงกับพฤติกรรมที่คาดไว้
ดูเพิ่มเติม NextSelectionUp , NextSelectionDown , และ NextSelectionRight .
ตัวอย่างโค้ด
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
คุณสมบัตินี้ตั้งค่า GuiObject ที่เลือกเมื่อผู้ใช้ย้ายตัวเลือก gamepad ไปทางขวาหากคุณสมบัตินี้ว่างเปล่า การย้ายแผ่นควบคุมไปทางขวาจะไม่เปลี่ยน GUI ที่เลือก
การย้ายตัวเลือกเกมไปที่ชุดทางด้านขวาตั้งค่า GuiService.SelectedObject ให้กับวัตถุนี้ยกเว้น GUI ไม่ได้เป็น Selectableโปรดทราบว่าคุณสมบัตินี้สามารถตั้งค่าเป็นองค์ประกอบ GUI ได้แม้ว่ามันจะไม่ใช่ Selectable ดังนั้นคุณควรตรวจสอบให้แน่ใจว่ามูลค่าของคุณสมบัติที่เลือกได้ของ GUI ตรงกับพฤติกรรมที่คาดไว้
ดูเพิ่มเติม NextSelectionUp , NextSelectionDown , และ NextSelectionLeft .
ตัวอย่างโค้ด
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
คุณสมบัตินี้ตั้งค่า GuiObject ที่เลือกเมื่อผู้ใช้ย้ายตัวเลือก gamepad ขึ้นหากคุณสมบัตินี้ว่างเปล่า, การย้ายแผ่นเกมขึ้นไปจะไม่เปลี่ยน GUI ที่เลือก
การย้ายตัวเลือกเกมแพดขึ้นไปตั้ง GuiService.SelectedObject ให้กับวัตถุนี้ยกเว้น GUI ไม่ใช่ Selectableโปรดทราบว่าคุณสมบัตินี้สามารถตั้งค่าเป็นองค์ประกอบ GUI ได้แม้ว่ามันจะไม่ใช่ Selectable ดังนั้นคุณควรตรวจสอบให้แน่ใจว่ามูลค่าของคุณสมบัติที่เลือกได้ของ GUI ตรงกับพฤติกรรมที่คาดไว้
ดูเพิ่มเติม NextSelectionDown , NextSelectionLeft , NextSelectionRight .
ตัวอย่างโค้ด
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
คุณสมบัตินี้กำหนดพิกเซลและตําแหน่งเวกเตอร์โดยใช้ GuiObject ตำแหน่งจะอยู่กลางรอบวัตถุ UDim2 . ตำแหน่งจะอยู่รอบวัตถุ GuiObject.AnchorPoint
ตำแหน่งของค่าเลขานุการเป็นค่าสัมพันธ์กับขนาดขององค์ประกอบ GUI พ่วงด้วยถ้ามี
ส่วนพิกเซลของค่า UDim2 มีขนาดเท่ากันไม่ว่าจะเป็นขนาดของ GUI แม่เท่าใดค่าเหล่านี้แทนที่ตำแหน่งของวัตถุในพิกเซลตำแหน่งพิกเซลที่แท้จริงของวัตถุสามารถอ่านได้จากคุณสมบัติ GuiBase2d.AbsolutePosition
Rotation
คุณสมบัตินี้กำหนดจำนวนองศาที่ GuiObject หมุนไปการหมุนเป็นค่าสัมพันธ์กับ ศูนย์กลาง ของวัตถุ, ไม่ จุดหมุน, หมายความว่าคุณไม่สามารถเปลี่ยนจุดหมุนได้นอกจากนี้คุณสมบัตินี้ไม่สอดคล้องกับ ClipsDescendants
Selectable
คุณสมบัตินี้กำหนดว่า GuiObject สามารถเลือกได้เมื่อนําทาง GUIs โดยใช้เกมแพด
หากคุณสมบัตินี้เป็น true , GUI สามารถเลือกได้ การเลือก GUI จะตั้งค่าคุณสมบัติ GuiService.SelectedObject ให้เป็นวัตถุนั้น
เมื่อนี้เป็น false ไม่สามารถเลือก GUI ได้อย่างไรก็ตาม การตั้งค่านี้เป็น false เมื่อ GUI ถูกเลือกจะไม่ยกเลิกการเลือกหรือเปลี่ยนค่าของคุณสมบัติ GuiService.SelectedObject
เพิ่ม GuiObject.SelectionGained และ GuiObject.SelectionLost จะไม่ยิงสำหรับองค์ประกอบ เพื่อยกเลิกการเลือก GuiObject คุณต้องเปลี่ยนค่า GuiService.SelectedObject คุณสมบัติ
คุณสมบัตินี้มีประโยชน์หาก GUI เชื่อมต่อกับ GUI หลายตัวผ่านคุณสมบัติเช่นนี้ GuiObject.NextSelectionUp , GuiObject.NextSelectionDown , NextSelectionRight หรือ NextSelectionLeftแทนที่จะเปลี่ยนคุณสมบัติทั้งหมดเพื่อให้ Gamepad ไม่สามารถเลือก GUI คุณสามารถปิดใช้งานคุณสมบัติที่เลือกได้เพื่อป้องกันไม่ให้ถูกเลือกชั่วคราวจากนั้นเมื่อคุณต้องการให้ตัวเลือกเกมแพดสามารถเลือก GUI ได้ เพียงเปิดใช้งานคุณสมบัติที่เลือกได้อีกครั้ง
ตัวอย่างโค้ด
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
คุณสมบัตินี้ทำลายการเลือกเริ่มต้นที่ใช้สำหรับเกมแพด
โปรดทราบว่าสิ่งที่เลือก SelectionImageObject ซ้อนสิ่งที่เลือก GuiObject ด้วย Size ของภาพเพื่อให้ได้ผลลัพธ์ที่ดีที่สุด คุณควรปรับขนาดของกำหนดเอง SelectionImageObject ผ่านค่าสเกล UDim2 เพื่อช่วยให้แน่ใจว่าวัตถุมีขนาดถูกต้องบนองค์ประกอบที่เลือก
การเปลี่ยน SelectionImageObject สําหรับองค์ประกอบ GuiObject เพียงส่งผลกระทบต่อองค์ประกอบนั้น เพื่อส่งผลกระทบต่อองค์ประกอบ GUI ทั้งหมดของผู้ใช้ ให้ตั้งค่าคุณสมบัติ PlayerGui.SelectionImageObject
เพื่อกำหนดหรือตั้งค่าว่าองค์ประกอบ GUI อันไหนที่ผู้ใช้เลือก คุณสามารถใช้คุณสมบัติ GuiService.SelectedObjectผู้เล่นใช้ gamepad เพื่อเลือกองค์ประกอบ GUI ที่แตกต่างกันโดยเรียกใช้ NextSelectionUp , NextSelectionDown , NextSelectionLeft และ NextSelectionRight อีเวนต์
SelectionOrder
GuiObjects ที่มีลำดับการเลือกต่ำกว่าจะถูกเลือกก่อน GuiObjects ที่มีลำดับการเลือกสูงกว่าเมื่อเริ่มการเลือกเกมแพดหรือเรียก GuiService:Select() บนบรรพบุรุษคุณสมบัตินี้ไม่ส่งผลต่อการนำทางแบบทิศทางค่าเริ่มต้นคือ 0
Size
คุณสมบัตินี้กำหนดขนาด GuiObject ของค่าเวกเตอร์และขนาดพิกเซลโดยใช้ UDim2
ขนาดสเกลาร์เป็นค่าสัมพันธ์กับขนาดขององค์ประกอบ GUI พ่วงด้วยถ้ามี
ส่วนพิกเซลของค่า UDim2 มีขนาดเท่ากันไม่ว่าจะเป็นขนาดของ GUI แม่เท่าใดค่าเหล่านี้แทนที่ขนาดของวัตถุในพิกเซลขนาดพิกเซลที่แท้จริงของวัตถุสามารถอ่านได้จากคุณสมบัติ GuiBase2d.AbsoluteSize
หาก GuiObject มีพ่อ ขนาดของมันตามแต่ละแกนก็ได้รับอิทธิพลจากพ่อของมัน SizeConstraint เช่นกัน
ตัวอย่างโค้ด
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
คุณสมบัตินี้ตั้งค่าแกน Size ที่ GuiObject จะใช้เป็นพื้นฐานตามขนาดของพ่อของมัน เมื่อเทียบกับขนาดของพ่อ
คุณสมบัตินี้มีประโยชน์สำหรับการสร้างวัตถุ GUI ที่มีจุดมุ่งหมายเพื่อขยายตามความกว้าง หรือ ความสูงของวัตถุบิดา แต่ไม่ทั้งสอง โดยมีประสิทธิภาพในการรักษาอัตราส่วนของวัตถุไว้
Visible
คุณสมบัตินี้ว่า GuiObject และบรรดาลูกหลานจะถูกแสดงออก
การเรนเดอร์ของส่วนประกอบแต่ละส่วนของ GuiObject สามารถควบคุมได้เองผ่านคุณสมบัติความโปร่งใสเช่น GuiObject.BackgroundTransparency และ TextLabel.TextTransparency และ ImageLabel.ImageTransparency
เมื่อคุณสมบัตินี้เป็น false , GuiObject จะถูกเพิกเฉยโดยโครงสร้างเลย์เอาต์เช่น UIListLayout , UIGridLayout และ UITableLayoutคือพื้นที่ที่องค์ประกอบอื่นจะใช้แทนที่จะใช้ในเลย์เอาต์ที่องค์ประกอบอื่นจะใช้แทน
ตัวอย่างโค้ด
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
คุณสมบัตินี้กำหนดลำดับที่เรนเดอร์ GuiObject จะแสดงเป็นค่าสัมบูรณ์เมื่อเทียบกับคนอื่น
โดยค่าเริ่มต้น, GuiObjects เรนเดอร์ในลำดับลำดับความสำคัญเพิ่มขึ้นเมื่อผู้ที่มีค่าต่ำกว่า ZIndex ถูกเรนเดอร์ภายใต้ผู้ที่มีค่าสูงกว่าคุณสามารถเปลี่ยนลำดับการแสดงผลภายใน ScreenGui , SurfaceGui หรือ BillboardGui โดยการเปลี่ยนค่าของตัวเลขของมัน ZIndexBehavior
หากคุณไม่แน่ใจว่าจะต้องเพิ่มองค์ประกอบระหว่างองค์ประกอบที่มีอยู่สองอย่างในอนาคตหรือไม่ เป็นการดีที่จะใช้หลายครั้งของ 100 ( 0 , 100 , 200 , เป็นต้น)สิ่งนี้ช่วยให้มีช่องว่างขนาดใหญ่ของค่าลำดับการเรนเดอร์ที่คุณสามารถใช้สำหรับองค์ประกอบที่ซ้อนอยู่ระหว่างองค์ประกอบอื่นๆ
ดูเพิ่มเติม LayoutOrder ซึ่งควบคุมลำดับการจัดเรียง **** ของ GuiObject เมื่อใช้กับโครงสร้างเลย์เอาต์เช่น UIListLayout หรือ UIGridLayout
วิธีการ
TweenPosition
เคลื่อน GUI ไปยังตำแหน่งใหม่ UDim2 ในเวลาที่กำหนดโดยใช้ Enum.EasingDirection และ Enum.EasingStyle ที่ระบุ
ฟังก์ชันนี้จะคืนว่าวัยรุ่นจะเล่นหรือไม่มันจะไม่เล่นถ้าวัยรุ่นอีกคนกําลังกระทําใน GuiObject และพารามิเตอร์การยกเลิกการกระทําคือ false
ดูเพิ่มเติม GuiObject:TweenSize() และ GuiObject:TweenSizeAndPosition() .
พารามิเตอร์
ที่ที่ GUI ควรย้ายไป
ทิศทางที่จะทำให้ GUI ง่ายขึ้นไปที่ ตำแหน่งสิ้นสุด
สไตล์ที่จะทำให้ GUI ง่ายขึ้นไปที่ตําแหน่งสิ้นสุด **
นานแค่ไหนในวินาทีที่วัยรุ่นควรใช้เวลาในการเสร็จสิ้น
ว่าวัยรุ่นจะยกเลิกวัยรุ่นที่กําลังดําเนินการหรือไม่
ฟังก์ชันการโทรกลับเพื่อดําเนินการเมื่อทีนสําเร็จ
ส่งค่ากลับ
ว่าวัยรุ่นจะเล่นหรือไม่
ตัวอย่างโค้ด
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
ปรับขนาดให้เหมาะสมของ GuiObject เป็นใหม่ UDim2 ในเวลาที่กำหนดโดยใช้ Enum.EasingDirection และ Enum.EasingStyle ที่กำหนด
ฟังก์ชันนี้จะคืนว่าวัยรุ่นจะเล่นหรือไม่โดยปกติจะกลับมาเสมอ true แต่จะกลับมา false หากวัยรุ่นอีกคนใช้งานและการปิดกั้นถูกตั้งค่าเป็น false
ดูเพิ่มเติม GuiObject:TweenSize() และ GuiObject:TweenSizeAndPosition() .
พารามิเตอร์
ขนาดที่ GUI ควรปรับขนาดใหม่
ทิศทางที่จะทำให้ GUI ง่ายขึ้นไปที่ endSize
สไตล์ที่จะทำให้ GUI ง่ายขึ้นไปที่ endSize
นานแค่ไหนในวินาทีที่วัยรุ่นควรใช้เวลาในการเสร็จสิ้น
ว่าวัยรุ่นจะยกเลิกวัยรุ่นที่กําลังดําเนินการหรือไม่
ฟังก์ชันการโทรกลับเพื่อดําเนินการเมื่อทีนสําเร็จ
ส่งค่ากลับ
ว่าวัยรุ่นจะเล่นหรือไม่
ตัวอย่างโค้ด
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
ปรับขนาดและเคลื่อนย้าย GUI ไปยังขนาดและตำแหน่งใหม่ UDim2 ในเวลาที่กำหนดโดยใช้ Enum.EasingDirection และ Enum.EasingStyle ที่ระบุ
ฟังก์ชันนี้จะคืนว่าวัยรุ่นจะเล่นหรือไม่โดยปกติจะกลับมาเสมอ true แต่จะกลับมา false หากวัยรุ่นอีกคนใช้งานและการปิดกั้นถูกตั้งค่าเป็น false
ดูเพิ่มเติม GuiObject:TweenSize() และ GuiObject:TweenSizeAndPosition() .
พารามิเตอร์
ขนาดที่ GUI ควรปรับขนาดใหม่
ที่ที่ GUI ควรย้ายไป
ทิศทางที่จะทำให้ GUI ง่ายขึ้นไปที่ endSize และ endPosition
สไตล์ที่จะทำให้ GUI ง่ายขึ้นไปที่ endSize และ endPosition
นานแค่ไหนในวินาทีที่วัยรุ่นควรใช้เวลาในการเสร็จสิ้น
ว่าวัยรุ่นจะยกเลิกวัยรุ่นที่กําลังดําเนินการหรือไม่
ฟังก์ชันการโทรกลับเพื่อดําเนินการเมื่อทีนสําเร็จ
ส่งค่ากลับ
ว่าวัยรุ่นจะเล่นหรือไม่
ตัวอย่างโค้ด
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))
อีเวนต์
InputBegan
อีเวนต์นี้จะเกิดขึ้นเมื่อผู้ใช้เริ่มโต้ตอบกับ GuiObject ผ่านอุปกรณ์อินเทอร์เฟซมนุษย์-คอมพิวเตอร์ (ปุ่มเมาส์ลง, แตะเริ่ม, ปุ่มแป้นพิมพ์ลง, เป็นต้น)
The UserInputService มีอีเวนต์ที่มีชื่อคล้ายกันที่ไม่ จํากัด เฉพาะองค์ประกอบ UI เฉพาะ: UserInputService.InputBegan .
อีเวนต์นี้จะเกิดขึ้นเสมอไม่ว่าสถานะเกมจะเป็นอย่างไร
ดูเพิ่มเติม GuiObject.InputEnded และ GuiObject.InputChanged .
พารามิเตอร์
An InputObject , ซึ่งมีข้อมูลที่มีประโยชน์สำหรับการค้นหาการใส่ของผู้ใช้เช่น type of input , state of input และ screen coordinates of the input
ตัวอย่างโค้ด
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
อีเวนต์นี้จะเกิดขึ้นเมื่อผู้ใช้เปลี่ยนวิธีการโต้ตอบผ่านอุปกรณ์อินเทอร์เฟซมนุษย์-คอมพิวเตอร์ (ปุ่มเมาส์ลง, แตะเริ่ม, ปุ่มแป้นพิมพ์ลง, เป็นต้น)
The UserInputService มีอีเวนต์ที่มีชื่อคล้ายกันที่ไม่ จํากัด เฉพาะองค์ประกอบ UI เฉพาะ: UserInputService.InputChanged .
อีเวนต์นี้จะเกิดขึ้นเสมอไม่ว่าสถานะเกมจะเป็นอย่างไร
ดูเพิ่มเติม GuiObject.InputBegan และ GuiObject.InputEnded .
พารามิเตอร์
An InputObject , ซึ่งมีข้อมูลที่มีประโยชน์สำหรับการค้นหาการใส่ของผู้ใช้เช่น type of input , state of input และ screen coordinates of the input
ตัวอย่างโค้ด
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
อีเวนต์ InputEnded จะเกิดขึ้นเมื่อผู้ใช้หยุดโต้ตอบผ่านอุปกรณ์อินเทอร์เฟซมนุษย์-คอมพิวเตอร์ (ปุ่มเมาส์ลง, แตะเริ่ม, ปุ่มพิมพ์ลง, เป็นต้น)
The UserInputService มีอีเวนต์ที่มีชื่อคล้ายกันที่ไม่ จํากัด เฉพาะองค์ประกอบ UI เฉพาะ: UserInputService.InputEnded .
อีเวนต์นี้จะเกิดขึ้นเสมอไม่ว่าสถานะเกมจะเป็นอย่างไร
ดูเพิ่มเติม GuiObject.InputBegan และ GuiObject.InputChanged .
พารามิเตอร์
An InputObject , ซึ่งมีข้อมูลที่มีประโยชน์สำหรับการค้นหาการใส่ของผู้ใช้เช่น Enum.UserInputType , Enum.UserInputState และ InputObject.Position
ตัวอย่างโค้ด
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
อีเวนต์ MouseEnter จะเกิดขึ้นเมื่อผู้ใช้ย้ายเมาส์ไปยังองค์ประกอบ GuiObject
โปรดอย่าพึ่งพาอาร์กิวเมนต์ x และ y ที่ส่งโดยเหตุการณ์นี้เป็นวิธีป้องกันข้อผิดพลาดในการกำหนดตำแหน่งเมาส์ของผู้ใช้เมื่อเข้าสู่ GUIพิกัดเหล่านี้อาจแตกต่างกันแม้เมื่อเมาส์เข้าสู่ GUI ผ่านขอบเดียวกัน - โดยเฉพาะอย่างยิ่งเมื่อเมาส์เข้าสู่องค์ประกอบอย่างรวดเร็วเนื่องจากพิกัดบ่งบอกตำแหน่งของเมาส์เมื่อเหตุการณ์เกิดขึ้นแทนที่จะเป็นช่วงเวลาที่แม่นยำที่เมาส์เข้าสู่ GUI
อีเวนต์นี้จะเกิดขึ้นแม้เมื่อองค์ประกอบ GUI เรนเดอร์ภายใต้องค์ประกอบอื่น
หากคุณต้องการติดตามเมื่อเมาส์ของผู้ใช้ออกจากองค์ประกอบ GUI คุณสามารถใช้เหตุการณ์ GuiObject.MouseLeave
ดูเพิ่มเติม
พารามิเตอร์
พิกเซลของความเคลื่อนที่ในหน้าจอของเมาส์ X ที่เกี่ยวข้องกับมุมซ้ายบนของหน้าจอ
พิกเซลของความเคลื่อนที่ในหน้าจอของเมาส์ Y ที่เกี่ยวข้องกับมุมซ้ายบนของหน้าจอ
ตัวอย่างโค้ด
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
อีเวนต์ MouseLeave จะเรียกเมื่อผู้ใช้ย้ายเมาส์ออกจากองค์ประกอบ GuiObject
โปรดอย่าพึ่งพาอาร์กิวเมนต์ x และ y ที่ส่งโดยเหตุการณ์นี้เป็นวิธีป้องกันข้อผิดพลาดในการกำหนดตำแหน่งเมาส์ของผู้ใช้เมื่อออกจาก GUIพิกัดเหล่านี้อาจแตกต่างกันแม้เมื่อเมาส์ออกจาก GUI ผ่านขอบเดียวกัน - โดยเฉพาะอย่างยิ่งเมื่อเมาส์ออกจากองค์ประกอบอย่างรวดเร็วเนื่องจากพิกัดบ่งบอกตำแหน่งของเมาส์เมื่อเหตุการณ์เกิดขึ้นแทนที่จะเป็นช่วงเวลาที่แม่เมาส์ออกจาก GUI อย่างแม่นยำ
อีเวนต์นี้จะเกิดขึ้นแม้เมื่อองค์ประกอบ GUI เรนเดอร์ภายใต้องค์ประกอบอื่น
ดูเพิ่มเติม
พารามิเตอร์
พิกเซลของความเคลื่อนที่ในหน้าจอของเมาส์ X ที่เกี่ยวข้องกับมุมซ้ายบนของหน้าจอ
พิกเซลของความเคลื่อนที่ในหน้าจอของเมาส์ Y ที่เกี่ยวข้องกับมุมซ้ายบนของหน้าจอ
MouseMoved
ไฟไหม้ทุกครั้งที่ผู้ใช้ย้ายเมาส์ในขณะที่อยู่ภายในองค์ประกอบ GuiObjectมันคล้ายกับ Mouse.Move ซึ่งจะยิงไม่ว่าเมาส์ของผู้ใช้จะอยู่เหนือองค์ประกอบ GUI หรือไม่
โปรดทราบว่าอีเวนต์นี้จะเกิดขึ้นเมื่อตำแหน่งของเมาส์ได้รับการอัปเดตจึงจะเกิดขึ้นซ้ำๆ ในขณะที่ย้าย
อาร์กิวเมนต์ x และ y บ่งบอกถึงพิกเซลของเส้นแนวนอนที่อัปเดตของเมาส์ของผู้ใช้สิ่งเหล่านี้สามารถมีประโยชน์ในการกำหนดตำแหน่งของเมาส์บน GUI หน้าจอ และเดลต้าตั้งแต่ตำแหน่งก่อนหน้าของเมาส์หากถูกติดตามในตัวแปรทั่วไป
รหัสด้านล่างแสดงวิธีการกำหนดออฟเซ็ต Vector2 ของเมาส์ของผู้ใช้เมื่อเทียบกับองค์ประกอบ 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)
โปรดทราบว่าอีเวนต์นี้อาจไม่เกิดขึ้นอย่างถูกต้องเมื่อเมาส์ของผู้ใช้เข้าหรือออกจากองค์ประกอบ GUIดังนั้น x และ y อาร์กิวเมนต์อาจไม่ตรงกับพิกัดของขอบ GUI ได้อย่างสมบูรณ์
ดูเพิ่มเติม
พารามิเตอร์
พิกเซลของความเคลื่อนที่ในหน้าจอของเมาส์ X ที่เกี่ยวข้องกับมุมซ้ายบนของหน้าจอ
พิกเซลของความเคลื่อนที่ในหน้าจอของเมาส์ Y ที่เกี่ยวข้องกับมุมซ้ายบนของหน้าจอ
MouseWheelBackward
อีเวนต์ WheelBackward จะเกิดขึ้นเมื่อผู้ใช้เลื่อนล้อเมาส์กลับเมื่อเมาส์อยู่เหนือองค์ประกอบ GuiObjectมันคล้ายกับ Mouse.WheelBackward ซึ่งจะยิงไม่ว่าเมาส์ของผู้ใช้จะอยู่เหนือองค์ประกอบ GUI หรือไม่
อีเวนต์นี้จะยิงเพียงเป็นตัวบ่งชี้ถึงการเคลื่อนที่ถอยหลังของล้อเท่านั้นซึ่งหมายความว่าข้อโต้แย้งเกี่ยวกับพิกัดเมาส์ x และ y จะไม่เปลี่ยนแปลงเนื่องจากเหตุการณ์นี้พิกัดเหล่านี้จะเปลี่ยนเฉพาะเมื่อเมาส์เคลื่อนที่ซึ่งสามารถติดตามได้โดยการเหตุการณ์ GuiObject.MouseMoved
ดูเพิ่มเติม
พารามิเตอร์
พิกเซลของความเคลื่อนที่ในหน้าจอของเมาส์ X ที่เกี่ยวข้องกับมุมซ้ายบนของหน้าจอ
พิกเซลของความเคลื่อนที่ในหน้าจอของเมาส์ Y ที่เกี่ยวข้องกับมุมซ้ายบนของหน้าจอ
MouseWheelForward
อีเวนต์ล้อข้างหน้าจะเกิดขึ้นเมื่อผู้ใช้เลื่อนล้อเมาส์ไปข้างหน้าเมื่อเมาส์อยู่เหนือองค์ประกอบ GuiObjectมันคล้ายกับ Mouse.WheelForward ซึ่งจะยิงไม่ว่าเมาส์ของผู้ใช้จะอยู่เหนือองค์ประกอบ GUI หรือไม่
อีเวนต์นี้จะยิงเพียงเป็นตัวบ่งชี้ถึงการเคลื่อนที่ไปข้างหน้าของล้อเท่านั้นซึ่งหมายความว่าข้อโต้แย้งเกี่ยวกับพิกัดเมาส์ X และ Y จะไม่เปลี่ยนแปลงเนื่องจากเหตุการณ์นี้พิกัดเหล่านี้จะเปลี่ยนเฉพาะเมื่อเมาส์เคลื่อนที่ซึ่งสามารถติดตามได้โดยการเหตุการณ์ GuiObject.MouseMoved
ดูเพิ่มเติม
พารามิเตอร์
พิกเซลของความเคลื่อนที่ในหน้าจอของเมาส์ X ที่เกี่ยวข้องกับมุมซ้ายบนของหน้าจอ
พิกัด Y ของเมาส์ของผู้ใช้
SelectionGained
อีเวนต์นี้จะเกิดขึ้นเมื่อตัวเลือก Gamepad เริ่มมุ่งเน้นไปที่ GuiObject
หากต้องการตรวจสอบจาก Gamepad เลือกหยุดโฟกัสบนองค์ประกอบ GUI คุณสามารถใช้เหตุการณ์ GuiObject.SelectionLost
เมื่อ GUI ได้รับการโฟกัสเลือกแล้ว ค่าของคุณสมบัติ SelectedObject ก็จะเปลี่ยนเป็นค่าที่ได้รับการเลือกเช่นกันเพื่อกำหนดว่า GUI ตัวใดได้รับการเลือก ตรวจสอบมูลค่าของคุณสมบัตินี้
ตัวอย่างโค้ด
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
อีเวนต์นี้จะเกิดขึ้นเมื่อตัวเลือก Gamepad หยุดโฟกัสที่ GuiObject
หากคุณต้องการตรวจสอบจาก Gamepad เลือกเริ่มมุ่งเน้นไปที่องค์ประกอบ GUI คุณสามารถใช้เหตุการณ์ GuiObject.SelectionGained
เมื่อ GUI สูญเสียการโฟกัสเลือกแล้ว ค่าของคุณสมบัติ SelectionObject จะเปลี่ยนเป็น nil หรือไปยังองค์ประกอบ GUI ที่ได้รับการโฟกัสเลือกเพื่อตรวจสอบว่า GUI ตัวใดได้รับการเลือกหรือไม่มี GUI ถูกเลือกใดๆ ตรวจสอบค่าของคุณสมบัตินี้
ตัวอย่างโค้ด
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
อีเวนต์นี้จะเกิดขึ้นหลังจากช่วงเวลาสั้น ๆ เมื่อผู้เล่นถือนิ้วบนองค์ประกอบ UI โดยใช้อุปกรณ์ที่เปิดใช้งานการสัมผัสมันยิงด้วยตารางของ Vector2 ที่อธิบายตำแหน่งหน้าจอสัมผัสที่เกี่ยวข้องกับนิ้วที่มีส่วนร่วมในการโต้ตอบนอกจากนี้ยังยิงหลายครั้ง: Enum.UserInputState.Begin หลังจากช่วงเวลาสั้น ๆ Enum.UserInputState.Change หากผู้เล่นย้ายนิ้วในระหว่างท่าทางและสุดท้าย Enum.UserInputState.Endความล่าช้าขึ้นอยู่กับแพลตฟอร์ม; ใน Studio มันนานกว่าหนึ่งวินาทีเล็กน้อย
เนื่องจากอีเวนต์นี้ต้องการนิ้วเดียวเท่านั้น อีเวนต์นี้สามารถจำลองได้ใน Studio โดยใช้เอมูเลเตอร์และเมาส์
พารามิเตอร์
A Enum.UserInputState ที่อธิบายสถานะของท่าทาง:
- เริ่มไฟเมื่อต้นที่จุดเริ่มต้นของท่าทาง (หลังจากความล่าช้าสั้น ๆ )
- เปลี่ยนไฟหากผู้เล่นย้ายนิ้วขณะกดลง
- ดับไฟสิ้นสุดที่จุดสิ้นสุดของท่าทางเมื่อพวกเขาปล่อยนิ้วมือ
ตัวอย่างโค้ด
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
อีเวนต์นี้จะเกิดขึ้นเมื่อผู้เล่นย้ายนิ้วบนองค์ประกอบ UI โดยใช้อุปกรณ์ที่เปิดใช้งานการสัมผัสมันจะยิงสั้นก่อนที่ GuiObject.TouchSwipe จะ, และไม่ได้ยิงด้วย GuiObject.TouchTapอีเวนต์นี้มีประโยชน์สำหรับการอนุญาตให้ผู้เล่นสามารถควบคุมตำแหน่งขององค์ประกอบ UI บนหน้าจอได้
อีเวนต์นี้จะยิงด้วยตารางของ Vector2 ที่อธิบายตำแหน่งหน้าจอสัมผัสที่เกี่ยวข้องกับนิ้วที่มีส่วนร่วมในการแสดงออกนอกจากนี้ยังยิงหลายครั้ง: Enum.UserInputState.Begin หลังจากช่วงเวลาสั้น ๆ Enum.UserInputState.Change เมื่อผู้เล่นย้ายนิ้วของพวกเขาในระหว่างท่าทาง และสุดท้ายด้วย Enum.UserInputState.End
อีเวนต์นี้ไม่สามารถจำลองได้ใน Studio โดยใช้เอมูเลเตอร์และเมาส์; คุณต้องมีอุปกรณ์จริงที่เปิดใช้งานการสัมผัสเพื่อยิงมัน
พารามิเตอร์
คอลเลกชัน Luau ของวัตถุ Vector2 แต่ละรายการที่ระบุตำแหน่งของนิ้วทั้งหมดที่มีส่วนร่วมในท่าทาง
บ่งบอกถึงระยะที่การสัมผัสแผ่นเปลี่ยนจากจุดเริ่มต้น
บ่งบอกถึงความเร็วที่ท่าทางถูกดําเนินการในแต่ละมิติอย่างรวดเร็ว
บ่งบอก Enum.UserInputState ของท่าทาง
ตัวอย่างโค้ด
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
อีเวนต์นี้จะเกิดขึ้นเมื่อผู้เล่นใช้นิ้วสองนิ้วเพื่อทำท่าบีบหรือดึงบนองค์ประกอบ UI โดยใช้อุปกรณ์ที่เปิดใช้งานการสัมผัสการบีบ เกิดขึ้นเมื่อสองนิ้วหรือมากกว่าเคลื่อนใกล้กัน และการดึง เกิดขึ้นเมื่อพวกเขาเคลื่อนออกไปอีเวนต์นี้จะเกิดขึ้นร่วมกับ GuiObject.TouchPanอีเวนต์นี้มีประโยชน์สำหรับการอนุญาตให้ผู้เล่นสามารถควบคุมขนาด (ขนาด) ขององค์ประกอบ UI บนหน้าจอและมักใช้สำหรับคุณลักษณะการซูม
อีเวนต์นี้จะยิงด้วยตารางของ Vector2 ที่อธิบายตำแหน่งหน้าจอสัมผัสที่เกี่ยวข้องกับนิ้วที่มีส่วนร่วมในการแสดงออกนอกจากนี้ยังยิงหลายครั้ง: Enum.UserInputState.Begin หลังจากช่วงเวลาสั้น ๆ Enum.UserInputState.Change เมื่อผู้เล่นย้ายนิ้วในระหว่างท่าทางและสุดท้ายด้วย Enum.UserInputState.Endควรทราบว่าเครื่องชั่งควรใช้งานเป็นทวีคูณ **** เนื่องจากอีเวนต์นี้ต้องใช้นิ้วอย่างน้อยสองนิ้ว จึงไม่สามารถจำลองได้ใน Studio โดยใช้เอมูเลเตอร์และเมาส์; คุณต้องมีอุปกรณ์ที่เปิดใช้งานการสัมผัสจริง
พารามิเตอร์
คอลเลกชัน Luau ของวัตถุ Vector2 แต่ละรายการที่ระบุตำแหน่งของนิ้วทั้งหมดที่มีส่วนร่วมในการสัมผัส
ลอยตัวที่บ่งบอกถึงความแตกต่างจากจุดเริ่มต้นของการสัมผัส
ลอยตัวบ่งบอกถึงความเร็วที่การกดปิดเกิดขึ้นอย่างรวดเร็ว
บ่งบอก Enum.UserInputState ของท่าทาง
ตัวอย่างโค้ด
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
อีเวนต์นี้จะเกิดขึ้นเมื่อผู้เล่นใช้นิ้วสองนิ้วเพื่อทำท่าบีบหรือดึงบนองค์ประกอบ UI โดยใช้อุปกรณ์ที่เปิดใช้งานการสัมผัสการหมุนเกิดขึ้นเมื่อมุมของเส้นระหว่างนิ้วสองนิ้วเปลี่ยนไปอีเวนต์นี้จะเกิดขึ้นร่วมกับ GuiObject.TouchPanอีเวนต์นี้มีประโยชน์ในการอนุญาตให้ผู้เล่นสามารถควบคุมการหมุนขององค์ประกอบ UI บนหน้าจอได้
อีเวนต์นี้จะยิงด้วยตารางของ Vector2 ที่อธิบายตำแหน่งหน้าจอสัมผัสที่เกี่ยวข้องกับนิ้วที่มีส่วนร่วมในการแสดงออกนอกจากนี้ยังยิงหลายครั้ง: Enum.UserInputState.Begin หลังจากช่วงเวลาสั้น ๆ Enum.UserInputState.Change เมื่อผู้เล่นย้ายนิ้วในระหว่างท่าทางและสุดท้ายด้วย Enum.UserInputState.End
เนื่องจากอีเวนต์นี้ต้องใช้นิ้วอย่างน้อยสองนิ้ว จึงไม่สามารถจำลองได้ใน Studio โดยใช้เอมูเลเตอร์และเมาส์; คุณต้องมีอุปกรณ์ที่สามารถสัมผัสได้จริง
พารามิเตอร์
คอลเลกชัน Luau ของวัตถุ Vector2 แต่ละรายการที่ระบุตำแหน่งของนิ้วทั้งหมดที่มีส่วนร่วมในท่าทาง
ลอยตัวบ่งบอกถึงจํานวนการหมุนที่ไปจากจุดเริ่มต้นของการเคลื่อนไหว
ลอยตัวที่บ่งบอกถึงความเร็วที่ท่าทางถูกดําเนินการอย่างรวดเร็ว
บ่งบอก Enum.UserInputState ของท่าทาง
ตัวอย่างโค้ด
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
อีเวนต์นี้จะเกิดขึ้นเมื่อผู้เล่นดําเนินการสับข้อมูลบนองค์ประกอบ UI โดยใช้อุปกรณ์ที่สนับสนุนการสัมผัสมันจะยิงด้วยทิศทางของท่าทาง (ขึ้น ลง ซ้าย หรือขวา) และจํานวนจุดสัมผัสที่เกี่ยวข้องกับท่าทางการสั่งการแตะมักใช้เพื่อเปลี่ยนแท็บในอินเทอร์เฟซมือถือ
เนื่องจากอีเวนต์นี้ต้องการนิ้วเท้าเพียงนิ้วเดียว จึงสามารถจำลองได้ใน Studio โดยใช้ตัวจำลองและเมาส์
พารามิเตอร์
A Enum.SwipeDirection บ่งบอกทิศทางของการสไลด์ท่าทาง (ขึ้น ลง ซ้าย หรือขวา)
จํานวนจุดสัมผัสที่เกี่ยวข้องกับท่าทาง (โดยปกติ 1)
ตัวอย่างโค้ด
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
อีเวนต์นี้จะเกิดขึ้นเมื่อผู้เล่นดําเนินการสัมผัสทางกายภาพบนองค์ประกอบ UI โดยใช้อุปกรณ์ที่เปิดใช้งานการสัมผัสการแตะเป็นการแตะเพียงครั้งเดียวอย่างรวดเร็วโดยไม่มีการเคลื่อนไหวเกี่ยวข้อง (การกดนานขึ้นจะยิง GuiObject.TouchLongPress และการเคลื่อนที่ระหว่างการแตะจะยิง GuiObject.TouchPan และ/หรือ GuiObject.TouchSwipe )มันยิงด้วยตารางของวัตถุ Vector2 ที่อธิบายตำแหน่งที่เกี่ยวข้องของนิ้วที่มีส่วนร่วมในท่าทาง
เนื่องจากอีเวนต์นี้ต้องการนิ้วเท้าเพียงนิ้วเดียว จึงสามารถจำลองได้ใน Studio โดยใช้ตัวจำลองและเมาส์
พารามิเตอร์
ตัวอย่างโค้ด
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)