GuiObject

顯示已棄用項目

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

無法建立
無法瀏覽

GuiObject 是一個抽象類別(就像 BasePart 一樣),用於 2D 使用者介面物件。它定義了與圖形使用者介面 (GUI) 物件顯示相關的所有屬性,如 SizePosition。它還具有一些有用的唯讀屬性,如 AbsolutePositionAbsoluteSizeAbsoluteRotation

要以特殊方式操作 GUI 物件的佈局,您可以使用布局結構,如 list/flexgrid,並且您可以透過 appearance modifiers 對它們進行樣式設定。

雖然使用 InputBeganInputEnded 可以檢測到任何 GUI 物件上的滑鼠按鈕事件,但只有 ImageButtonTextButton 有方便的專用事件,如 Activated 來檢測點擊/按壓。

概要

屬性

屬性 繼承自 GuiBase2d

屬性

方法

活動

活動 繼承自 GuiBase2d

活動

屬性

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

平行讀取

範例程式碼

錨點範例

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)
end

AutomaticSize

平行讀取

範例程式碼

ScreenGui 中的 LocalScript

-- 輸出文本標籤/字體/大小的數組
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)
end

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
end

BackgroundTransparency

平行讀取

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()

BorderMode

平行讀取

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()

ClipsDescendants

平行讀取
唯讀
未複製
平行讀取

Interactable

平行讀取

LayoutOrder

平行讀取

NextSelectionDown

平行讀取

範例程式碼

創建遊戲手把選擇網格

-- 使用下面的代碼設置遊戲手把選擇網格
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
-- 使用下面的代碼測試遊戲手把選擇網格
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

平行讀取

範例程式碼

創建遊戲手把選擇網格

-- 使用下面的代碼設置遊戲手把選擇網格
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
-- 使用下面的代碼測試遊戲手把選擇網格
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

平行讀取

範例程式碼

創建遊戲手把選擇網格

-- 使用下面的代碼設置遊戲手把選擇網格
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
-- 使用下面的代碼測試遊戲手把選擇網格
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

平行讀取

範例程式碼

創建遊戲手把選擇網格

-- 使用下面的代碼設置遊戲手把選擇網格
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
-- 使用下面的代碼測試遊戲手把選擇網格
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

平行讀取

Rotation

平行讀取

Selectable

平行讀取

範例程式碼

限制文本框選取

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)

SelectionImageObject

平行讀取

SelectionOrder

平行讀取

Size

平行讀取

範例程式碼

健康條

local Players = game:GetService("Players")
local player = Players.LocalPlayer
-- 將腳本粘貼到一個 LocalScript 中,該 LocalScript
-- 被放置到一個 Frame 內部的 Frame 中
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)
end

SizeConstraint

平行讀取

Transparency

隱藏
未複製
平行讀取

Visible

平行讀取

範例程式碼

使用者介面視窗

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)

ZIndex

平行讀取

方法

TweenPosition

參數

endPosition: UDim2
easingDirection: Enum.EasingDirection
預設值:"Out"
easingStyle: Enum.EasingStyle
預設值:"Quad"
time: number
預設值:1
override: boolean
預設值:false
callback: function
預設值:"nil"

返回

範例程式碼

調整 GUI 的位置

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("tween 完成且未中斷")
elseif state == Enum.TweenStatus.Canceled then
print("另一個 tween 取消了這個")
end
end
-- 初始化 GuiObject 的位置,然後開始 tween:
guiObject.Position = START_POSITION
local willPlay = guiObject:TweenPosition(
GOAL_POSITION, -- tween 應達到的最終位置
Enum.EasingDirection.In, -- 緩動的方向
Enum.EasingStyle.Sine, -- 要應用的緩動類型
2, -- tween 持續的時間(秒)
true, -- 進行中的 tweens 是否被中斷
callback -- 完成/取消時調用的函數
)
if willPlay then
print("tween 將會播放")
else
print("tween 不會播放")
end

TweenSize

參數

endSize: UDim2
easingDirection: Enum.EasingDirection
預設值:"Out"
easingStyle: Enum.EasingStyle
預設值:"Quad"
time: number
預設值:1
override: boolean
預設值:false
callback: function
預設值:"nil"

返回

範例程式碼

調整 GuiObject 的大小

local guiObject = script.Parent
local function callback(didComplete)
if didComplete then
print("動畫成功完成")
else
print("動畫已被取消")
end
end
local willTween = guiObject:TweenSize(
UDim2.new(0.5, 0, 0.5, 0), -- endSize (必填)
Enum.EasingDirection.In, -- easingDirection (預設為 Out)
Enum.EasingStyle.Sine, -- easingStyle (預設 為 Quad)
2, -- time (預設: 1)
true, -- 這個 tween 是否應覆蓋正在進行的 tween? (預設: false)
callback -- tween 完成時要呼叫的函數 (預設: nil)
)
if willTween then
print("GuiObject 將會進行動畫")
else
print("GuiObject 將不會進行動畫")
end

TweenSizeAndPosition

參數

endSize: UDim2
endPosition: UDim2
easingDirection: Enum.EasingDirection
預設值:"Out"
easingStyle: Enum.EasingStyle
預設值:"Quad"
time: number
預設值:1
override: boolean
預設值:false
callback: function
預設值:"nil"

返回

範例程式碼

調整 GUI 的大小和位置

-- 將 frame 的大小和位置進行動畫
local frame = script.Parent.Frame
frame:TweenSizeAndPosition(UDim2.new(0, 0, 0, 0), UDim2.new(0, 0, 0, 0))

活動

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

參數


範例程式碼

GuiObject 輸入變更範例

local UserInputService = game:GetService("UserInputService")
local gui = script.Parent
local function printMovement(input)
print("位置:", input.Position)
print("移動增量:", 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)

MouseLeave

參數


MouseMoved

參數


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

參數

touchPositions: Array

範例程式碼

移動 UI 元素與長按觸控

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

參數

touchPositions: Array
totalTranslation: Vector2
velocity: Vector2

範例程式碼

平移 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

參數

touchPositions: Array
scale: number
velocity: number

範例程式碼

捏合/拉伸縮放

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

參數

touchPositions: Array
rotation: number
velocity: number

範例程式碼

觸控旋轉

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

參數

swipeDirection: Enum.SwipeDirection
numberOfTouches: number

範例程式碼

彈跳顏色選擇器

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

參數

touchPositions: Array

範例程式碼

點擊透明度切換

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)