GuiObject 是一個抽象類別(就像 BasePart 一樣),用於 2D 使用者介面物件。它定義了與圖形使用者介面 (GUI) 物件顯示相關的所有屬性,如 Size 和 Position。它還具有一些有用的唯讀屬性,如 AbsolutePosition、AbsoluteSize 和 AbsoluteRotation。
要以特殊方式操作 GUI 物件的佈局,您可以使用布局結構,如 list/flex 或 grid,並且您可以透過 appearance modifiers 對它們進行樣式設定。
雖然使用 InputBegan 和 InputEnded 可以檢測到任何 GUI 物件上的滑鼠按鈕事件,但只有 ImageButton 和 TextButton 有方便的專用事件,如 Activated 來檢測點擊/按壓。
概要
屬性
決定此 UI 元素是否接收輸入。
決定 GuiObject 的原點,根據其絕對大小。
決定是否根據子內容進行大小調整。
決定 GuiObject 背景顏色。
決定 GuiObject 背景和邊界的透明度。
決定 GuiObject 邊界的顏色。
決定 GuiObject 邊界的佈局方式,根據其尺寸。
決定 GuiObject 邊界的像素寬度。
決定父 GUI 元素邊界之外的子 GuiObjects 是否應該繪製。
決定玩家的滑鼠是否正在主動按下 GuiObject。
控制在使用 UIGridStyleLayout 時,GuiObject 的排序順序。
設置當遊戲手柄選擇器下移時將選擇的 GuiObject。
設置當遊戲手柄選擇器向左移動時將選擇的 GuiObject。
設置當遊戲手柄選擇器向右移動時將選擇的 GuiObject。
設置當遊戲手柄選擇器向上移動時將選擇的 GuiObject。
決定 GuiObject 的像素和比例位置。
決定 GuiObject 旋轉的度數。
決定 GuiObject 是否可以被遊戲手柄選擇。
覆蓋用於遊戲手柄的預設選擇裝飾。
由遊戲手柄 UI 選擇選擇的 GuiObjects 的順序。
決定 GuiObject 的像素和比例大小。
一個混合屬性,包含 BackgroundTransparency 和 TextTransparency。
決定 GuiObject 及其後代是否會被繪製。
決定 GuiObject 相對於其他物件的繪製順序。
屬性
方法
- 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 元素上滾動滑鼠滾輪向前時觸發。
當 GUI 物件正在被遊戲手柄選擇器聚焦時觸發。
當遊戲手柄選擇器停止聚焦於 GUI 物件時觸發。
當玩家開始、持續和停止長按 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
範例程式碼
-- 將此 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
範例程式碼
-- 輸出文本標籤/字體/大小的數組
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
GuiState
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
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
參數
返回
範例程式碼
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
參數
返回
範例程式碼
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
參數
返回
範例程式碼
-- 將 frame 的大小和位置進行動畫
local frame = script.Parent.Frame
frame:TweenSizeAndPosition(UDim2.new(0, 0, 0, 0), UDim2.new(0, 0, 0, 0))
活動
InputBegan
參數
範例程式碼
-- 為了使用 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
參數
範例程式碼
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
參數
範例程式碼
-- 使用 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
參數
範例程式碼
local guiObject = script.Parent
guiObject.MouseEnter:Connect(function(x,
y)
print("使用者的滑鼠游標已進入 GuiObject,位置為", x, ",", y)
end)
SelectionGained
範例程式碼
local guiObject = script.Parent
local function selectionGained()
print("使用者已使用遊戲控制器選擇此按鈕。")
end
guiObject.SelectionGained:Connect(selectionGained)
SelectionLost
範例程式碼
local guiObject = script.Parent
local function selectionLost()
print("用戶不再用遊戲手柄選擇此項目。")
end
guiObject.SelectionLost:Connect(selectionLost)
TouchLongPress
參數
範例程式碼
local frame = script.Parent
frame.Active = true
local dragging = false
local basePosition
local startTouchPosition
local borderColor3
local backgroundColor3
local function onTouchLongPress(touchPositions, state)
if state == Enum.UserInputState.Begin and not dragging then
-- 開始拖曳
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
參數
範例程式碼
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
參數
範例程式碼
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
參數
範例程式碼
local innerFrame = script.Parent
local outerFrame = innerFrame.Parent
outerFrame.BackgroundTransparency = 0.75
outerFrame.Active = true
outerFrame.Size = UDim2.new(1, 0, 1, 0)
outerFrame.Position = UDim2.new(0, 0, 0, 0)
outerFrame.AnchorPoint = Vector2.new(0, 0)
outerFrame.ClipsDescendants = true
local dragging = false
local baseRotation = innerFrame.Rotation
local function onTouchRotate(_touchPositions, rotation, _velocity, state)
if state == Enum.UserInputState.Begin and not dragging then
dragging = true
baseRotation = innerFrame.Rotation
outerFrame.BackgroundTransparency = 0.25
elseif state == Enum.UserInputState.Change then
innerFrame.Rotation = baseRotation + rotation
elseif state == Enum.UserInputState.End and dragging then
dragging = false
outerFrame.BackgroundTransparency = 0.75
end
end
outerFrame.TouchRotate:Connect(onTouchRotate)
TouchSwipe
參數
範例程式碼
local frame = script.Parent
frame.Active = true
-- 須在成功的滑動上讓框架彈跳的距離
local BOUNCE_DISTANCE = 50
-- 框架的當前狀態
local basePosition = frame.Position
local hue = 0
local saturation = 128
local function updateColor()
frame.BackgroundColor3 = Color3.fromHSV(hue / 256, saturation / 256, 1)
end
local function onTouchSwipe(swipeDir, _touchCount)
-- 根據滑動方向改變 BackgroundColor3
local deltaPos
if swipeDir == Enum.SwipeDirection.Right then
deltaPos = UDim2.new(0, BOUNCE_DISTANCE, 0, 0)
hue = (hue + 16) % 255
elseif swipeDir == Enum.SwipeDirection.Left then
deltaPos = UDim2.new(0, -BOUNCE_DISTANCE, 0, 0)
hue = (hue - 16) % 255
elseif swipeDir == Enum.SwipeDirection.Up then
deltaPos = UDim2.new(0, 0, 0, -BOUNCE_DISTANCE)
saturation = (saturation + 16) % 255
elseif swipeDir == Enum.SwipeDirection.Down then
deltaPos = UDim2.new(0, 0, 0, BOUNCE_DISTANCE)
saturation = (saturation - 16) % 255
else
deltaPos = UDim2.new()
end
-- 更新顏色並稍微彈跳框架
updateColor()
frame.Position = basePosition + deltaPos
frame:TweenPosition(basePosition, Enum.EasingDirection.Out, Enum.EasingStyle.Bounce, 0.7, true)
end
frame.TouchSwipe:Connect(onTouchSwipe)
updateColor()
TouchTap
參數
範例程式碼
local frame = script.Parent
frame.Active = true
local function onTouchTap()
-- 切換背景透明度
if frame.BackgroundTransparency > 0 then
frame.BackgroundTransparency = 0
else
frame.BackgroundTransparency = 0.75
end
end
frame.TouchTap:Connect(onTouchTap)