允許體驗將使用者輸入綁定到上下文行動或只在某個條件或時間期間啟用的行動。例如,允許玩家只在靠近時打開門。在代碼中,行動簡單地是服務使用的字串(行動名稱),用於區分獨特的動作動。行動字串提供給 BindAction 和 UnbindAction 等其他會員功能。如果兩個行動綁定到同一輸入,最近綁定的將取得優先權。當最新的行動被解除綁定時,之前綁定的行動再次取得控制。由於此服務涉及用戶輸入,您只能在客戶端 LocalScripts 使用它。
上下文和行動
A 上下文 是玩家可以執行某些動作的條件。一些例子包括持有 Tool , 在汽車裡 seated 或站在門附近。無論案件如何,都取決於您的 LocalScripts 在上下文輸入時呼叫 BindAction ,在上下文離開時呼叫 UnbindAction 。
一個 行動 是在該上下文中玩家可以執行的某些輸入。這樣的行動可以打開/關閉一些選單,觸發次要工具行動或使用 RemoteFunction:InvokeServer() 向服務器發送請求。一個行動由獨特的字串識別為 BindAction 和 UnbindAction 的第一個參數。字串可以是任何東西,但應該反映執行的 行動,而不是使用的輸入 。例如,不要使用「KeyH」作為行動名稱;使用「CarHorn」代替。最好將您的行動定義為腳本頂部的常量,因為您將在代碼中使用它至少三個不同的地方。
在行動上下文中綁定行動
大多數情況下,使用 ContextActionService 的 BindAction 比 UserInputService.InputBegan 更好。對於 UserInputService.InputBegan , 您的連接函數必須檢查玩家是否在執行的行動上下文中。在大多數情況下,這比只呼叫功能時 Context 進入/離開時更困難。例如,如果你想讓 H 鍵啟動汽笛聲音,當玩家坐在裡面時,玩家可能會在聊天中輸入「你好」或使用 H 鍵做其他事情。更難判斷是否有其他東西使用了 H 鍵(例如聊天) - 汽車可能會響起,當玩家沒有意圖時。如果您使用 BindAction 和 UnbindAction 當玩家進入/離開車輛時,ContextActionService 將確保 H 按鍵按下僅在最近綁定的行動時啟動喇叭行動作。如果另一個東西(例如聊天)採取控制,你就不必擔心檢查那個了。
檢查綁定行動
若要查看行動和其綁定輸入的列表,您可以在開發者控制台(遊戲中的 F9)中檢查「行動綁定」標籤。這會顯示所有的綁定,包括被 Roblox 核心腳本和預設攝影機/控制腳本綁定的內容。這對於在正確的時間綁定/解除綁定您的操作或其他操作偷取您的操作輸入有用。例如,如果您嘗試綁定 W A S D,可能會發生情況,即預設角色移動腳本會綁定到這些相同的鍵上。相同地,攝影機控制腳本可以偷走右鍵輸入,如果腳本在你之後執行。
無鍵盤輸入
此服務特別適用於支持遊戲手柄和觸摸輸入。對於遊戲控制器輸入,您可能會選擇將 B 按鈕綁定到返回用戶到上一個選單時的動作。對於觸摸,在畫面上的觸摸按鈕可以用於取代按鍵操作:這些按鈕只在操作綁定時顯示,位置、文字和/或圖像可以通過此服務配置。他們在此服務提供的自訂量有一定限制;通常使用 ImageButton 或 TextButton 製作自己的屏幕按鈕會是更好的選擇。
範例程式碼
This example properly shows how to use ContextActionService in binding user input to a contextual action. The context is the tool being equipped; the action is reloading some weapon. Test this code sample by placing it in a LocalScript parented to a Tool. When the Tool is equipped, a "Reload" action is bound, and when the Tool is unequipped the "Reload" action is unbound. When the player presses R with the Tool equipped, the message "Reloading!" will appear.
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Reload"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Reloading!")
end
end
tool.Equipped:Connect(function()
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
tool.Unequipped:Connect(function()
ContextActionService:UnbindAction(ACTION_RELOAD)
end)
概要
方法
- BindAction(actionName : string,functionToBind : function,createTouchButton : boolean,inputTypes : Tuple):()
將使用者輸入綁定到給予行動處理功能的行動。
行為像 BindAction 但也允許指定優先級給覆蓋輸入類型的綁定行動(先高於低)。
用特定的 Enum.KeyCode 與 Enum.UserInputType 綁定一個,以觸發 Tool.Activation 和 ClickDetector 事件。
取得所有綁定行動的資訊表(鑰匙是傳給 BindAction 的名稱,值是在使用鍵時從 GetBoundActionInfo 獲得的表)。
取得有關綁定行動的資訊表,其原始名稱傳送到 BindAction 的表。
返回 BackpackItem.TextureId 的 Tool 目前 equipped 由 Player 。
根據觸摸按鈕的綁定行動名稱設置行動作的說明。
如果 actionName 鍵包含綁定的動作,則 image 設為觸摸按鈕的圖像。
根據觸摸按鈕的綁定行動名稱,設置按鈕在 ContextButtonFrame 中的位置。
根據觸摸按鈕的綁定行動名稱設置按鈕上顯示的文字。
從輸入中解除行動的綁定,並將其名稱更改為其名稱。
- UnbindActivate(userInputTypeForActivation : Enum.UserInputType,keyCodeForActivation : Enum.KeyCode):()
使用特定的 Enum.KeyCode 與 Enum.UserInputType 從觸發 Tool.Activation 當與 ContextActionService:BindActivate() 綁定時解除綁定。
移除所有已綁定的功能。沒有行動名稱會留下。所有觸摸按鈕將被移除。
取回一個 ImageButton 的 bound 行動,其中創建了一個觸摸輸入按鈕。
活動
屬性
方法
BindAction
將行動綁定到使用者輸入,並提供行動處理函數。當匹配的輸入執行時,行動處理器功能會被稱為以下列參數呼叫。有效輸入枚列項目包括以追蹤中項目:Enum.KeyCode、Enum.UserInputType或Enum.PlayerActions 。當玩家 進入可執行行動的上下文時,呼叫此函數 。當玩家離開上下文時,呼叫 UnbindAction 使用相同的 actionName 。您可以使用 CallFunction 手動呼叫行動處理功能的行動。
下面的代碼示例顯示了如何在按下鍵(H)、遊戲控制板按鈕或觸摸屏按鈕時,Sound可以成為played
local ContextActionService = game:GetService("ContextActionService")
-- 汽笛聲音
local honkSound = Instance.new("Sound", workspace)
honkSound.Looped = true
honkSound.SoundId = "rbxassetid://9120386436"
local function handleAction(actionName, inputState, inputObject)
if actionName == "HonkHorn" then
if inputState == Enum.UserInputState.Begin then
honkSound:Play()
else
honkSound:Pause()
end
end
end
-- 當玩家坐在車輛時:
ContextActionService:BindAction("HonkHorn", handleAction, true, Enum.KeyCode.H, Enum.KeyCode.ButtonY)
-- 當玩家離開時:
ContextActionService:UnbindAction("HonkHorn")
行動處理器參數
行動處理器功能會以下列參數呼叫:
<th>類型</th><th>說明</th></tr><tr><td>1</td><td><code>字串</code></td><td>最初傳送給 BindAction† 的相同字串</td></tr><tr><td>2</td><td><code>Enum.UserInputState</code></td><td>輸入狀態(開始、變更、結束或取消)*</td></tr><tr><td>3</td><td><code>輸入對象</code></td><td>包含輸入資訊的對象(根據使用者輸入類型而變化)</td></tr>
# |
† 這樣可以讓一個功能一次處理多個行動,如有需要。※如果某些輸入正在進行中,且另一個動作綁定到進行中的輸入,或進行中的綁定動作為 unbound ,則取消將被發送。
行動綁定堆疊
行動綁定會像棧子一樣行動:如果兩個行動綁定到同一個使用者輸入,最近綁定的 行動處理器 將被使用。如果行動處理器返回 Enum.ContextActionResult.Pass,下一個最近綁定的行動處理器將被呼叫,直到行動處理器將輸入沉沒(通過返回 nil 或 Enum.ContextActionResult.Sink)。當 UnbindAction 被呼叫時,行動處理器將從堆中移除。這種堆疊行為可以使用 BindActionAtPriority 來覆蓋,其中在 createTouchButton 之後的額外優先級參數可能會覆蓋行動綁定的順序(先高後低)。
觸摸按鈕
除了輸入類型外,此函數的第三個參數控制是否為 TouchEnabled 裝置創建按鈕。在第一個觸摸按鈕創作品時,一個名為"ContextActionGui"的ScreenGui被添加到PlayerGui。在 ScreenGui 內部添加了 Frame 稱為 "ContextButtonFrame" 的項目。在這個框架中,ImageButtons 用於綁定操作的父級;您可以使用 GetButton 來恢復這些按鈕以進行自訂。
參數
代表正在執行的動作的字串(例如「HonkHorn」或「OpenDoor」)。
當綁定輸入被觸發時,使用以下參數呼叫行動處理功能:string(actionName)、Enum.UserInputState 和一個輸入對象。
是否應為觸摸輸入裝置上的操作創建 GUI 按鈕。
任何數量的 Enum.KeyCode 或 Enum.UserInputType 代表要綁定到行動作的輸入。
返回
範例程式碼
This example properly shows how to use ContextActionService in binding user input to a contextual action. The context is the tool being equipped; the action is reloading some weapon. Test this code sample by placing it in a LocalScript parented to a Tool. When the Tool is equipped, a "Reload" action is bound, and when the Tool is unequipped the "Reload" action is unbound. When the player presses R with the Tool equipped, the message "Reloading!" will appear.
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Reload"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Reloading!")
end
end
tool.Equipped:Connect(function()
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
tool.Unequipped:Connect(function()
ContextActionService:UnbindAction(ACTION_RELOAD)
end)
This code sample uses ContextActionService to bind an action named "BoundAction" to a general action handler function on the F key. Place this in a LocalScript inside StarterPlayerScripts and press F to see the message "Handling action: BoundAction".
local ContextActionService = game:GetService("ContextActionService")
local function handleAction(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Handling action: " .. actionName)
print(inputObj.UserInputType)
end
-- Since this function does not return anything, this handler will
-- "sink" the input and no other action handlers will be called after
-- this one.
end
ContextActionService:BindAction("BoundAction", handleAction, false, Enum.KeyCode.F)
This code sample demonstrates how BindAction acts like a stack. It binds two actions, FirstAction (Z, X, and C keys) and SecondAction (Z and X keys) to two action handling functions. The second one will pass on a certain input (the X key).
Both actions use the Z and X keys, however the second handler will pass input only if X is pressed. So, when X is pressed, the second handler is called and then the first. The first action is also bound to the C key, and can be triggered even though the other two inputs are "covered" by the second action.
Test this code out by pasting it into a LocalScript within StarterPlayerScripts, then pressing Z, X and C. Observe which action handlers are called with what actions.
local ContextActionService = game:GetService("ContextActionService")
-- Define an action handler for FirstAction
local function actionHandlerOne(actionName, inputState, _inputObj)
if inputState == Enum.UserInputState.Begin then
print("Action Handler One: " .. actionName)
end
-- This action handler returns nil, so it is assumed that
-- it properly handles the action.
end
-- Binding the action FirstAction (it's on the bottom of the stack)
ContextActionService:BindAction("FirstAction", actionHandlerOne, false, Enum.KeyCode.Z, Enum.KeyCode.X, Enum.KeyCode.C)
-- Define an action handler for SecondAction
local function actionHandlerTwo(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Action Handler Two: " .. actionName)
end
if inputObj.KeyCode == Enum.KeyCode.X then
return Enum.ContextActionResult.Pass
else
-- Returning nil implicitly Sinks inputs
return Enum.ContextActionResult.Sink
end
end
-- Binding SecondAction over the first action (since it bound more recently, it is on the top of the stack)
-- Note that SecondAction uses the same keys as
ContextActionService:BindAction("SecondAction", actionHandlerTwo, false, Enum.KeyCode.Z, Enum.KeyCode.X)
BindActionAtPriority
BindActionAtPriority 會像 BindAction 一樣行動,但也允許指定優先級給綁定的動作動。如果多個行動綁定到相同的輸入,優先級更高的函數將無視行動綁定的順序來執行。換言之,此功能會覆蓋 BindAction 的正常「堆疊」行為。
參數
代表正在執行的動作的字串(例如「HonkHorn」或「OpenDoor」)。
當綁定輸入被觸發時,使用以下參數呼叫行動處理功能:string(actionName)、Enum.UserInputState 和一個輸入對象。
是否應為觸摸輸入裝置上的操作創建 GUI 按鈕。
行動應該綁定的優先級(先考慮高於低)。
任何數量的 Enum.KeyCode 或 Enum.UserInputType 代表要綁定到行動作的輸入。
返回
範例程式碼
This code sample demonstrates how ContextActionService:BindActionAtPriority() can be used to bind actions out of order yet still have the same priority levels. Normally, BindAction() would operate on order (last bound action has highest priority), but priority levels override this. You can test this code by pasting it into a Script with RunContext = Client in ReplicatedStorage.
local ContextActionService = game:GetService("ContextActionService")
local INPUT_KEY1 = Enum.KeyCode.Q
local INPUT_KEY2 = Enum.KeyCode.E
local function handleThrow(actionName: string, inputState: Enum.UserInputState, inputObject: InputObject)
if inputState ~= Enum.UserInputState.Begin then
return Enum.ContextActionResult.Pass
end
print(`Action [{actionName}] occurred. KeyCode [{inputObject.KeyCode}] pressed.`)
return Enum.ContextActionResult.Sink
end
local function handlePunch(actionName: string, inputState: Enum.UserInputState, inputObject: InputObject)
if inputState ~= Enum.UserInputState.Begin then
return Enum.ContextActionResult.Pass
end
print(`Action [{actionName}] occurred. KeyCode [{inputObject.KeyCode}] pressed.`)
return Enum.ContextActionResult.Sink
end
-- Without specifying priority, the most recently bound action is called first,
-- so pressing INPUT_KEY1 prints "Punch" and then sinks the input.
ContextActionService:BindAction("DefaultThrow", handleThrow, false, INPUT_KEY1)
ContextActionService:BindAction("DefaultPunch", handlePunch, false, INPUT_KEY1)
-- Here we bind both functions in the same order as above, but with explicitly swapped priorities.
-- That is, we give "Throw" a higher priority of 2 so it will be called first,
-- despite "Punch" still being bound more recently.
-- Pressing INPUT_KEY2 prints "Throw" and then sinks the input.
ContextActionService:BindActionAtPriority("PriorityThrow", handleThrow, false, 2, INPUT_KEY2)
ContextActionService:BindActionAtPriority("PriorityPunch", handlePunch, false, 1, INPUT_KEY2)
BindActivate
綁定一個 Enum.KeyCode 可以使用 Enum.UserInputType 來啟用 ClickDetector 事件、Tools 和 GuiButtons 。當指定的鍵/按鈕按下時,會發射Mouse.Button1Down事件到鼠標傳送到Tool.Equipped。這會引發 Tool.Activated 事件,如果 Tool.ManualActivationOnly 未設為真值。對於遊戲手柄輸入,此功能由預設控制腳本呼叫,以便綁定按鈕 R2 Enum.KeyCode 。
請注意,指定的 Enum.UserInputType 必須是 Keyboard 或 Gamepad1 通過 Gamepad8 才能有效。
參數
必須是鍵盤或遊戲控制器1通過遊戲控制器8。
返回
GetAllBoundActionInfo
GetAllBoundActioninfo 返回一個表,將所有行動的名稱 (原本傳送到 BindAction ) 映射到由 GetBoundActionInfo 返回的表,當以行動名稱本身呼叫時。使用此功能,您可以檢查所有目前已綁定的行動。在檢查優先級或堆疊命令時,這很有用。
返回
GetBoundActionInfo
GetBoundActionInfo 返回包含以下鑰匙的表,描述綁定的行動給予其名稱。要一次獲得所有行動的相同資訊,請使用 GetAllBoundActionInfo 。
<th>類型</th><th>說明</th></tr><tr><td><code>堆疊順序</code></td><td>數量</td><td>描述堆疊上的動作指數(增加)</td></tr><tr><td><code>優先級</code> \*</td> <td>數量</td><td>描述行動作的 <code>Class.ContextActionService:BindActionAtPriority()|priority</code> 等級</td></tr><tr><td><code>創建觸摸按鈕</code></td><td>bool</td><td>描述是否應在 <code>Class.UserInputService.TouchEnabled|TouchEnabled</code> 設備上創建觸摸按鈕</td></tr><tr><td><code>輸入類型</code></td> <td>表</td><td>傳送到 <code>Class.ContextActionService:BindAction()|BindAction</code> 的輸入類型,此動作將因此而發生</td></tr><tr><td><code>說明</code> †</td><td>字串</td><td>由 <code>Class.ContextActionService:SetDescription()|SetDescription</code> 設置的行動說明</td></tr><tr><td><code>標題</code> †</td><td>字串</td><td>由 <code>Class.ContextActionService:SetTitle()|SetTitle</code> 設置的動作標題</td></tr><tr><td><code>圖像</code> †</td><td>字串</td><td>由 <code>Class.ContextActionService:SetImage()|SetImage 設置的動作動觸發按鈕圖像</code></td></tr>
名稱 |
※優先級仍然會包含,即使BindActionAtPriority未使用 - 默認值為 2000。
† 指示此欄位將是 nil 如果相關方法未被呼叫到給定的動作動。
參數
返回
GetCurrentLocalToolIcon
取得當前本地工具圖示將返回 BackpackItem.TextureId 的 Tool 目前 equipped 由 Player 或 nil 如果沒有此工具或如果玩家缺少 Character 。
返回
來自工具紋理ID的內容字串,或nil如果找不到,則可以使用。
SetDescription
設置說明將設置由 BindAction 綁定的行動的說明。在可用行動列表中,這將是描述給定行動作的文字。
雖然名稱可能暗示這個方法與定制觸摸按鈕的功能家族相關(SetTitle、SetImage和SetPosition),但此方法並不影響這樣的按鈕。此方法僅設置一個動作動的文字說明,並沒有更多。
參數
行動的名稱原本傳送到 BindAction 。
行動作的文字說明,例如「鳴響汽車喇叭」或「開啟道具欄」。
返回
範例程式碼
This code sample demonstrates binding an "Inspect" action to a touch button created automatically by ContextActionService. The button is customized using SetImage(), SetTitle(), SetDescription() and SetPosition(). The button is further customized by using GetButton() to get a reference to the ImageButton itself and tinting it green by setting ImageButton.ImageColor3.
Paste this code into a LocalScript placed within StarterPlayerScripts to test it. In Studio, be sure to toggle the touch device emulator in order for the button to actually be created.
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Image of a speech bubble with ? in it
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecting")
end
end
-- For touch devices, a button is created on-screen automatically via the 3rd parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We can use these functions to customize the button:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Look")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspect something.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We can manipulate the button directly using ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Remember: non-touch devices won't return anything!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint the ImageButton green
end
SetImage
此方法設置由 BindAction() 創建的觸摸按鈕所顯示的圖像。特別是,它設置 ImageLabel.Image 屬性的 ImageLabel 內的 ImageButton 將由 GetButton 返回。如果沒有這樣的綁定行動 (例如GetButton 沒有返回任何內容),這個函數不做任何操作,並且沒有錯誤發生。
此功能是行動作的觸發按鈕自訂家庭的一部分。其他成員包括 SetPosition 和 SetTitle 。
參數
返回
範例程式碼
This code sample demonstrates binding an "Inspect" action to a touch button created automatically by ContextActionService. The button is customized using SetImage(), SetTitle(), SetDescription() and SetPosition(). The button is further customized by using GetButton() to get a reference to the ImageButton itself and tinting it green by setting ImageButton.ImageColor3.
Paste this code into a LocalScript placed within StarterPlayerScripts to test it. In Studio, be sure to toggle the touch device emulator in order for the button to actually be created.
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Image of a speech bubble with ? in it
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecting")
end
end
-- For touch devices, a button is created on-screen automatically via the 3rd parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We can use these functions to customize the button:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Look")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspect something.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We can manipulate the button directly using ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Remember: non-touch devices won't return anything!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint the ImageButton green
end
SetPosition
這個方法設置由 BindAction() 創建的觸摸按鈕的位置。特別是,它設置 GuiObject.Position 屬性的 ImageButton 將被 GetButton 返回。如果沒有這樣的綁定行動 (例如GetButton 沒有返回任何內容),這個函數不做任何操作,並且沒有錯誤發生。
此功能是行動作的觸發按鈕自訂家庭的一部分。其他成員包括 SetImage 和 SetTitle 。
參數
返回
範例程式碼
This code sample demonstrates binding an "Inspect" action to a touch button created automatically by ContextActionService. The button is customized using SetImage(), SetTitle(), SetDescription() and SetPosition(). The button is further customized by using GetButton() to get a reference to the ImageButton itself and tinting it green by setting ImageButton.ImageColor3.
Paste this code into a LocalScript placed within StarterPlayerScripts to test it. In Studio, be sure to toggle the touch device emulator in order for the button to actually be created.
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Image of a speech bubble with ? in it
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecting")
end
end
-- For touch devices, a button is created on-screen automatically via the 3rd parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We can use these functions to customize the button:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Look")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspect something.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We can manipulate the button directly using ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Remember: non-touch devices won't return anything!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint the ImageButton green
end
SetTitle
設置標題將設置由 BindAction 創建的觸摸按鈕所顯示的文字。特別是,這會設置 TextLabel.Text 屬性的 TextLabel 內的 ImageButton 將由 GetButton 返回。如果沒有這樣的綁定行動 (例如GetButton 沒有返回任何內容),這個函數不做任何操作,並且沒有錯誤發生。
此功能是行動作的觸發按鈕自訂家庭的一部分。其他成員包括 SetImage 和 SetPosition 。
參數
返回
範例程式碼
This code sample demonstrates binding an "Inspect" action to a touch button created automatically by ContextActionService. The button is customized using SetImage(), SetTitle(), SetDescription() and SetPosition(). The button is further customized by using GetButton() to get a reference to the ImageButton itself and tinting it green by setting ImageButton.ImageColor3.
Paste this code into a LocalScript placed within StarterPlayerScripts to test it. In Studio, be sure to toggle the touch device emulator in order for the button to actually be created.
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Image of a speech bubble with ? in it
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecting")
end
end
-- For touch devices, a button is created on-screen automatically via the 3rd parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We can use these functions to customize the button:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Look")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspect something.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We can manipulate the button directly using ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Remember: non-touch devices won't return anything!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint the ImageButton green
end
UnbindAction
取消行動會將名稱的行動從使用者輸入中解除鍊結,因此行動處理器功能將不再被呼叫。當某些動作的上下文不再適用時,請呼叫此功能,例如關閉使用者介面、離開汽車或 unequipping 退出 Tool 。見 BindAction 了解更多關於綁定行動操作的詳情。
這個功能 不會 在指定的字串沒有綁定任何行動時發出錯誤。使用 GetAllBoundActionInfo 或開發者控制台的「行動綁定」標籤,您可以找出目前已綁定的行動。
參數
返回
範例程式碼
This example properly shows how to use ContextActionService in binding user input to a contextual action. The context is the tool being equipped; the action is reloading some weapon. Test this code sample by placing it in a LocalScript parented to a Tool. When the Tool is equipped, a "Reload" action is bound, and when the Tool is unequipped the "Reload" action is unbound. When the player presses R with the Tool equipped, the message "Reloading!" will appear.
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Reload"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Reloading!")
end
end
tool.Equipped:Connect(function()
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
tool.Unequipped:Connect(function()
ContextActionService:UnbindAction(ACTION_RELOAD)
end)
UnbindActivate
取消使用啟用將使用 Enum.KeyCode 與 Enum.UserInputType 啟用一個 Tool (或一個 HopperBin ),使用 BindActivate 來啟用它。此功能本質上撤銷該功能所執行的行動。
參數
原本傳送給 BindActivate 的相同的使用者輸入類型。
原本傳送給 BindActivate 的相同的密碼碼。
返回
UnbindAllActions
移除所有已綁定的功能。不會留下任何行動名稱。所有觸摸按鈕將被移除。如果按鈕是手動操作的,沒有保證它會被清理。
返回
GetButton
取得按鈕返回由 ImageButton 創建的 BindAction 如果其第三個參數是真實的,裝置是 TouchEnabled 。這個函數的唯一參數必須與原本傳送給 BindAction 的動作名稱完全匹配。
如果沒有綁定此動作或按鈕未創建,此函數將返回 nil 。
參數
行動的名稱原本傳送到 BindAction 。
返回
由 BindAction 創建的圖像按鈕。