ClickDetector

顯示已棄用項目

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

點擊偵測器 允許 ScriptsLocalScripts 通過他們的 MouseClick 事件接收 3D 對象的指針輸入。當作為父處理 BasePartModelFolder 對象時,它們會工作。它們偵測基本滑鼠事件:輸入、離開、左鍵點擊和右鍵點擊。在 TouchEnabled

預設控制腳本會將 ButtonR2 綁定到與 ClickDetectors 使用 ContextActionService:BindActivate() 進行互動,也可用於覆蓋此。當使用遊戲控制器時,中心點會觸發 MouseHoverEnterMouseHoverLeave 。綁定的啟用按鈕發射 MouseClick

MaxActivationDistance 可用於限制玩家從點擊偵測器到點擊停止的距離。

ClickDetector 事件在客戶端和伺服器上發生。由於 LocalScript 只會在下降到 Player 或玩家 Character 時才會執行,因此通常不需要將 LocalScript 放置在 ClickDetector 內,因為腳本將無法運行或對象將無法點擊。如果您需要檢測 事件, 可能是更好的選擇。

輸入優先級

如果多個 ClickDetectors 可以偵測到使用者輸入,只有最深的會發射事件。如果兩個 ClickDetectors 是兄弟,第一個會取得優先權。

如果使用 ContextActionService 綁定的行動使用與 ClickDetector 相同的輸入,使用 ContextActionService 綁定的行動將取代點擊偵測器的事件。

UserInputService.InputBegan 將在 ClickDetector 事件之前發射。

範例程式碼

將此代碼放置在 Script 內的 ClickDetector 內。代碼示例創建一個參考到父級的引用並定義一個函數來顯示歡迎玩家的訊息。最後,它將 MouseClick 事件連接到定義的函數。

點擊偵測器範例

local clickDetector = script.Parent
local function onClicked(player)
-- 向玩家顯示訊息
local msg = Instance.new("Message")
msg.Parent = player:FindFirstChild("PlayerGui")
msg.Text = "Hello, " .. player.Name
wait(2.5)
msg:Destroy()
end
-- 將功能連接到滑鼠點擊事件
clickDetector.MouseClick:Connect(onClicked)

This code sample will allow a part to be clicked to toggle its anchored property. When toggled, the visual appearance of the part is updated (red means anchored, yellow means free).

Part Anchored Toggle

local part = script.Parent
-- Create a ClickDetector so we can tell when the part is clicked
local cd = Instance.new("ClickDetector", part)
-- This function updates how the part looks based on its Anchored state
local function updateVisuals()
if part.Anchored then
-- When the part is anchored...
part.BrickColor = BrickColor.new("Bright red")
part.Material = Enum.Material.DiamondPlate
else
-- When the part is unanchored...
part.BrickColor = BrickColor.new("Bright yellow")
part.Material = Enum.Material.Wood
end
end
local function onToggle()
-- Toggle the anchored property
part.Anchored = not part.Anchored
-- Update visual state of the brick
updateVisuals()
end
-- Update, then start listening for clicks
updateVisuals()
cd.MouseClick:Connect(onToggle)

概要

屬性

活動

屬性

CursorIcon

ContentId
平行讀取

將鼠標懸停在此 ClickDetectorDragDetector 的父處時顯示鼠標指標。如果此屬性為空白,偵測器將使用預設圖示。

若要變更指標圖示,將此屬性設為你想使用的圖像的資產ID。

MaxActivationDistance

平行讀取

此屬性控制 CharacterClickDetectorDragDetector 之間的最大距離,以厘米為單位,讓玩家能夠與它互動。例個體、實例,距離 ClickDetectorDragDetector 最長激活距離為 5 的角色內的 10 個單位內的角色將無法使用偵測器,因為他們處於範圍之外。

方法

活動

MouseClick

此事件從 ScriptLocalScript 發射時,玩家與 ClickDetectorDragDetector 通過以下輸入互動:

  • 在使用滑鼠的平台上,當玩家離開滑鼠點擊時。
  • TouchEnabled 平台上,當玩家點擊時。
  • GamepadEnabled 平台上,當中心點位於同一模型上且 A 按鈕被按下並釋放時。

請注意,玩家的 Character 必須在偵測器的 MaxActivationDistance 內。

參數

playerWhoClicked: Player

點擊父級的 PlayerClickDetectorDragDetector 的人,誰點擊了父級。


MouseHoverEnter

當父親的 或 被玩家掃過時,此事件會從 或 發射,當父親的 被掃過時。這不會涉及與偵測器的明確互動,您可以聆聽 MouseClickRightMouseClick 事件。

由於使用者輸入的性質,你不應該依賴所有 MouseHoverEnter 事件發射相應的 MouseHoverLeave 事件。

參數

playerWhoHovered: Player

那些開始懸停在 PlayerClickDetectorDragDetector 的父親上的 。


範例程式碼

下列代碼會在玩家的鼠標漂移到父級上時打印 "[PlayerName] 漂移到了我的父級!",當玩家的鼠標漂移到父級上時。當玩家的鼠標移開父處時,也會打印 "[PlayerName] 從我的父處移開!" 當玩家的鼠標移開父處時,也會打印 "[PlayerName] 從我的父處移開!"

為了這個例子正常運作,它必須放置在 ScriptLocalScript 的父處,其父親是 ClickDetector

懸停在和關閉點擊偵測器

local clickDetector = script.Parent:FindFirstChildOfClass("ClickDetector")
clickDetector.MouseHoverEnter:Connect(function(player)
print(player.Name .. " hovered over my parent!")
end)
clickDetector.MouseHoverLeave:Connect(function(player)
print(player.Name .. " hovered off my parent!")
end)

MouseHoverLeave

此事件從 ScriptLocalScript 中發射,當玩家的鼠標漂移到 ClickDetectorDragDetector 的父處時。這不會涉及與偵測器的明確互動,您可以聆聽 MouseClickRightMouseClick 事件。

由於使用者輸入的性質,你不應該依賴在相應的MouseHoverEnter之後發射的所有MouseHoverLeave

參數

playerWhoHovered: Player

那個 Player 的鼠標漂移到了 ClickDetectorDragDetector 的父處。


範例程式碼

下列代碼會在玩家的鼠標漂移到父級上時打印 "[PlayerName] 漂移到了我的父級!",當玩家的鼠標漂移到父級上時。當玩家的鼠標移開父處時,也會打印 "[PlayerName] 從我的父處移開!" 當玩家的鼠標移開父處時,也會打印 "[PlayerName] 從我的父處移開!"

為了這個例子正常運作,它必須放置在 ScriptLocalScript 的父處,其父親是 ClickDetector

懸停在和關閉點擊偵測器

local clickDetector = script.Parent:FindFirstChildOfClass("ClickDetector")
clickDetector.MouseHoverEnter:Connect(function(player)
print(player.Name .. " hovered over my parent!")
end)
clickDetector.MouseHoverLeave:Connect(function(player)
print(player.Name .. " hovered off my parent!")
end)

RightMouseClick

當玩家按一下滑鼠瞄準器在 ScriptLocalScript 上時,此事件會發射,因為它們正在按一下滑鼠瞄準器在 ClickDetectorDragDetector 。請注意,玩家的 Character 必須在偵測器的 MaxActivationDistance 內。

參數

playerWhoClicked: Player

那些 Player 使用右鍵點擊滑鼠指針在父級的 ClickDetectorDragDetector 上的人。