UIDragDetector 實例可以協助和鼓勵與體驗中的 2D 用戶介面元素的互動,例如滑塊、旋轉器和更多。關鍵功能包括:
在任何 GuiObject 實例下放置 UIDragDetector 來使其可以通過所有使用者輸入拖曳而不需要一行代碼。
從多個 DragStyle 選項中選擇,定義對象如何通過 ResponseStyle 回應運動,並可選擇應用軸、移動限制或拖動邊界。
腳本可以回應拖曳對象的操作來驅動邏輯回應,例如調整設置。
在 Studio 的編輯和播放模式工作,直到你不使用 選擇 , 移動 , 縮放 或 旋轉 工具,或某些插件或 Studio 的 UI 編輯工具。
讓介面元素可拖曳
要使任何 GuiObject 實例可拖曳,只需添加一個 UIDragDetector 作為直接後代。
從選單中插入 UIDragDetector 。
預設情況下,對象現在將在 LayerCollector 介面上可拖曳。
自訂介面拖動偵測器
拖式風格
UIDragDetectors 將鼠標移動轉換為計算建議的2D運動和/或旋轉。通過 DragStyle 屬性,您可以從不同的映射中選擇滿足您需求的一個。例如, Enum.UIDragDetectorDragStyle.TranslatePlane 在 LayerCollector 的 2D 平面產生翻譯,而 Enum.UIDragDetectorDragStyle.Rotate 通常會產生旋轉而不是翻譯。
設置 | 說明 |
---|---|
TranslateLine | 沿著偵測器的 DragAxis 1D 運動。 |
TranslatePlane | 在 LayerCollector 的飛機平面上的 2D 運動。 |
Rotate | 預設情況下,旋轉探測器父輩的絕對中心位置GuiObject。如果 ReferenceUIInstance 被設定,旋轉發生在那個實個體、實例的絕對中心位置上。 |
Scriptable | 通過 SetDragStyleFunction() 提供的自訂函數計算所需的運動。 |
拖動方向
預設情況下,2D運動和相關的DragStyle地圖到祖先的空間LayerCollector。然而,您可能想在建立不同的使用者介面組件時變更 ReferenceUIInstance 或 DragAxis 。
設置 | 說明 | 預設 |
---|---|---|
ReferenceUIInstance | 一個 GuiObject 實例,其本地空間和絕對中心位置是偵測器的參考空間和起源。設置此參考會影響屬性,例如 DragUDim2 , DragRotation , 以及 DragAxis 的行為。 | nil |
DragAxis | Vector2 值,定義拖動對象的移動軸當 DragStyle 設為 Enum.UIDragDetectorDragStyle.TranslateLine 時。軸在 未定義,否則軸將在該實例的本地空間中定義,在那種情況下,軸將在該實個體、實例的本地空間中定義。 | (1, 0) |
回應動作
UIDragDetector.ResponseStyle 屬性指定對物件的位置值如何被提出的動作所更改。自訂回應風格讓您可以使用結果的 UIDragDetector.DragUDim2 和 UIDragDetector.DragRotation 值,無需讓偵測器的父執行提出的動作。
設置 | 說明 |
---|---|
Offset | 由偵測器的父元素值 值移動。這是預設設定。 |
Scale | 移動到偵測器的父元素值 Scale 值的 GuiObject.Position 值。 |
CustomOffset | 使用者介面元素不會移全部 所有一毫米,但偵測器的 值仍將更新,偵測器的事件仍會發觸發,允許您以任何方式回應拖動操作。 |
CustomScale | 使用者介面元素不會移全部 所有一毫米,但偵測器的 值仍將更新,偵測器的事件仍會發觸發,允許您以任何方式回應拖動操作。 |
翻譯與旋轉限制
預設情況下,DragStyle 的內在限制後面的 2D 運動沒有限制。若需要,可以使用以下屬性來宣言最小和最大翻譯和旋轉限制。此外,您可以定義拖曳對象如何在指定的 GuiObject 範圍內受限,例如 Frame 。
屬性 | 說明 | 預設 |
---|---|---|
MinDragTranslation MaxDragTranslation | 在每個維度拖動翻譯的限制,由 UDim2 值定義。如果 MaxDragTranslation 大於 MinDragTranslation,翻譯將被限制在該範圍內。 | 0, 0, 0, 0 |
MinDragAngle MaxDragAngle | 只有相關,如果 DragStyle 設為 Enum.UIDragDetectorDragStyle.Rotate 或如果通過 SetDragStyleFunction() 或 AddConstraintFunction() 設置的函數定義了旋轉值。如果 MaxDragAngle 大於 MinDragAngle,旋轉將被限制在該範圍內。 | 0 |
BoundingBehavior | 決定當 UIDragDetector 被設設定時,BoundingUI 實個體、實例的綁定行為。將此設為 EntireObject 將整個拖動的用戶介面限制在 BoundingUI 內,而將它設為 HitPoint 將拖動的用戶介面僅限於準確命中/抓取點和其相應位置後翻譯/旋轉。為了方便,默认值 模擬了 對於 UI 對象的行為,其全部包含在 中,或者部分位於 之外的 UI 對象。 | Automatic |
速度調整
通過 SelectionModeDragSpeed 和 SelectionModeRotateSpeed , 您可以調整偵測器的最大拖動/旋轉速度。此外,通過 UIDragSpeedAxisMapping , 您可以根據偵測器的 **** 調整 X / SelectionModeDragSpeed 維度拖動速度。
屬性 | 說明 |
---|---|
SelectionModeDragSpeed | 定義翻譯的最大拖動速度為第一個祖先 和 的組合,或 或 屬於最後一個祖先。 |
SelectionModeRotateSpeed | 定義在每秒鐘的最大角度,在那裡 UIDragDetector 可以旋轉。 |
UIDragSpeedAxisMapping | 根據偵測器的 SelectionModeDragSpeed 決定 X / Y 維度拖動速度,基於偵測器的拖動速度。預設值是 , 意味著 X 和 Y 軸速度是基於 X 和 Y 軸速度的,分別為 / 或 / respectively。替代方案是 和 , 意味著兩個軸速度都基於 X ( ) 或 Y ( ) 軸值,而 值仍適用於各自的軸。例如,如果第一個祖先 的尺寸為 800×600 和 是 ,那麼 的設置會導致 X / Y 拖動速度為 / ,而 會導致 / (注意值在兩種情況下都相同)。 |
點擊和拖動的腳本回應
通過 事件信號、屬性變更、Scriptable和自訂函數,腳本可以回應拖動到的用戶介面元素的操作來驅動各種設置或做出邏輯決定,例如調整音樂和音效音量獨立的滑塊。
事件信號
透過以下事件訊號,您可以偵測使用者開始、繼續和結束拖曳物件的時間。
事件 | 說明 |
---|---|
DragStart | 當使用者開始拖動對物件時發生火災。 |
DragContinue | 當使用者繼續拖動對象後 DragStart 已被啟動時,發生火災。 |
DragEnd | 當使用者停止拖動對物件時發生火災。 |
下列滑塊將容器指定為 BoundingUI 來限制其在容器區域內的移動,允許基於比例的 TranslateLine 拖動被限制到容器全寬度,而無需額外的編寫腳本。
UIDragDetector - 事件訊號透明變更
-- 層級是滑塊容器 > 處理 > UIDragDetector > (此指令碼)
local sliderContainer = script.Parent.Parent.Parent
local handle = sliderContainer:FindFirstChild("Handle")
local uiDragDetector = handle:FindFirstChildWhichIsA("UIDragDetector")
uiDragDetector.ResponseStyle = Enum.UIDragDetectorResponseStyle.Scale -- 以比例設定拖動
uiDragDetector.DragStyle = Enum.UIDragDetectorDragStyle.TranslateLine -- 限制拖曳到線
uiDragDetector.BoundingUI = sliderContainer
-- 初始設定容器透明度為手把理器的 X 縮放值
sliderContainer.BackgroundTransparency = 1 - handle.Position.X.Scale
-- 擴展手柄邊緣以指示抓取啟動
uiDragDetector.DragStart:Connect(function(inputPosition)
handle:FindFirstChildWhichIsA("UIStroke").Thickness = 6
end)
-- 透明度變更由拖入的比例決定
uiDragDetector.DragContinue:Connect(function(inputPosition)
sliderContainer.BackgroundTransparency = 1 - handle.Position.X.Scale
end)
-- 將手柄邊界返回以指示抓取結束
uiDragDetector.DragEnd:Connect(function(inputPosition)
handle:FindFirstChildWhichIsA("UIStroke").Thickness = 4
end)
位置與旋轉變更
除了 事件信號 外,您還可以直接監控偵測器的 DragUDim2 和/或 DragRotation 屬性變更。
下列偵測器將其 DragStyle 設置為 Rotate ,允許使用者拖動手柄在色度旋轉環上移動,同時偵測拖動旋轉通過 Instance:GetPropertyChangedSignal() 。
UIDragDetector - 拖動旋轉變更
local handle = script.Parent.Parent -- 可拖曳的介面元素
local uiDragDetector = handle:FindFirstChildWhichIsA("UIDragDetector")
uiDragDetector.DragStyle = Enum.UIDragDetectorDragStyle.Rotate -- 設置拖動風格以旋轉
local function changeHue()
local currAngle = (math.fmod(handle.Rotation, 360)) / 360
if currAngle < 0 then
currAngle += 1
end
handle.BackgroundColor3 = Color3.fromHSV(currAngle, 1, 1)
end
-- 初始設定顏色來處理旋轉
changeHue()
-- 將連接功能連接到偵測器的拖動旋轉的 GetPropertyChangedSignal()
uiDragDetector:GetPropertyChangedSignal("DragRotation"):Connect(changeHue)
腳本拖式風格
如果您將偵測器的 UIDragDetector.DragStyle 設為 Enum.UIDragDetectorDragStyle.Scriptable ,您可以提供自己的功能,該功能會接收輸入位置的 Vector2 並返回一個 UDim2 (位置) 和一個浮點(旋轉)。偵測器會將對象更新到計算位置/旋轉,取決於返回值、DragSpace 屬性和 DragRelativity 屬性。
預設情況下,返回的 和漂浮將是偵測器父元素空間的本地位置/旋轉的最終位置/旋轉。現有的翻譯/旋轉限制仍然適用,也會受到指定的 BoundingUI 個體、實例例設置的邊界限制。
下面的例子拖動一個用於跟隨變化 X 坐標輸入計算的三角波後的 UI 元素。請注意,偵測器的 DragSpace 設為 Enum.UIDragDetectorDragSpace.Relative 。
UIDragDetector - 拖動跟隨的三角波
local frame = script.Parent -- 可拖曳的介面元素
local uiDragDetector = frame:FindFirstChildWhichIsA("UIDragDetector")
local initialXValue = 0
local maxHeightChange = 200
local pixelsPerRadian = 75 -- 降低此值以增加頻率
uiDragDetector.DragStart:Connect(function(inputPosition)
initialXValue = inputPosition.X
end)
local function computeSinWaveCoordinate(inputPosition)
local deltaX = inputPosition.X - initialXValue
-- 負 Y 刪除以便它在屏幕上上升「正 Y」變更
local deltaY = -math.sin(deltaX / pixelsPerRadian) * maxHeightChange
return UDim2.fromOffset(deltaX, deltaY)
end
uiDragDetector:SetDragStyleFunction(computeSinWaveCoordinate)
自訂約束功能
UIDragDetectors 沒有內置網格和捕捉規則,但您可以註冊自定義約束函數來編輯偵測器的 UIDragDetector.DragUDim2 和 UIDragDetector.DragRotation ,在它們被應用之前。例如,您可以將位置回合到特定增量來保持網格上的運動,或定義允許的運動區域。請注意,此操作 在 任何現有翻譯/旋轉限制之前進行。
下面的例子使用一個限制函數,將平面拖動限制到 X / Y 網格,根據行和列的數量。請注意,偵測器的 ResponseStyle 設為 Enum.UIDragDetectorResponseStyle.Scale ,其 BoundingUI 設為網格容器。
UIDragDetector - 在網格上拖動,抓住瓷磚
-- 層級是 GridContainer ⟩ 處理 ⟩ UIDragDetector ⟩ (此指令碼)
local gridContainer = script.Parent.Parent.Parent
local handle = gridContainer:FindFirstChild("Handle") -- 可拖曳的介面元素
local uiDragDetector = handle:FindFirstChildWhichIsA("UIDragDetector")
uiDragDetector.ResponseStyle = Enum.UIDragDetectorResponseStyle.Scale -- 以比例設定拖動
uiDragDetector.DragRelativity = Enum.UIDragDetectorDragRelativity.Relative
uiDragDetector.BoundingUI = gridContainer
local NUM_COLUMNS = 10
local NUM_ROWS = 5
local xScaleIncrement = 1 / NUM_COLUMNS
local yScaleIncrement = 1 / NUM_ROWS
local initialParentPosition = uiDragDetector.Parent.Position
uiDragDetector.DragStart:Connect(function()
initialParentPosition = uiDragDetector.Parent.Position
end)
local function dragToGridOnly(proposedPosition, proposedRotation)
local griddedXScale = math.round(proposedPosition.X.Scale / xScaleIncrement) * xScaleIncrement
local griddedYScale = math.round(proposedPosition.Y.Scale / yScaleIncrement) * yScaleIncrement
return UDim2.fromScale(griddedXScale, griddedYScale), proposedRotation
end
uiDragDetector:AddConstraintFunction(1, dragToGridOnly)