ContextActionService

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

无法创建
服务

允许体验将用户输入绑定到上下文行动或仅在某些条件或时间段内启用的行动。例如,允许玩家仅在靠近时打开门。在代验证码中,一个动作是服务用于区分独特动作的简单字符串(动作名称)。行动字符串提供给 BindActionUnbindAction 等其他成员函数。如果两个行动绑定到同一个输入,最近绑定的将取得优先权。当最新的行动被解除绑定时,之前被绑定的行动再次获得控制。由于此服务涉及用户输入,您只能在客户端 LocalScripts 使用它。

上下文和行动

一个 上下文 是玩家可以执行某些动作的条件。一些例子包括持有一个 Tool , 在汽车里做 seated 或站在门附近。无论情况如何,都取决于您的 LocalScripts 在上下文输入时调用 BindAction ,在上下文离开时调用 UnbindAction

一个 行动 是在该上下文中玩家可以执行的一些输入。这样的操作可以打开/关闭一些菜单,触发二级工具行动或使用 RemoteFunction:InvokeServer() 向服务器发送请求。一个行动由独特的字符串识别为 both BindActionUnbindAction 的第一个参数。字符串可以是任何东西,但它应该反映出正在执行的 操作,而不是使用的输入 。例如,不要使用“KeyH”作为行动名称 - 使用“CarHorn”代替。最好将您的行动定义为脚本顶部的常量,因为您将在代验证码中使用它的至少三个不同位置。

在特定上下文中绑定行动

大多数情况下,使用 ContextActionService 的 BindActionUserInputService.InputBegan 更好。对于 UserInputService.InputBegan , 您的连接函数必须检查玩家是否在执行的行动上下文中。在大多数情况下,这比单纯调用函数时输入/离开上下文更难。例如,如果你想让 H 键触发汽车喇叭声音,当玩家坐在它里面时,玩家可能会在聊天中输入“你好”或使用 H 键做其他事情。更难确定是否有其他东西使用了 H 键(例如聊天) - 汽车可能会鸣响,当玩家没有意图时。如果你使用 BindActionUnbindAction 当玩家进入/离开汽车时, ContextActionService 将确保 H 键按只触发喇叭行动,当它是最近绑定的行动作时。如果另一件事(例如聊天)接管控制,你就不必担心检查这一点。

检查边界行动

要查看行动列表和其绑定输入,您可以在开发者控制台(游戏中的 F9)中检查“行动绑定”选项卡。这显示了所有绑定,包括由 Roblox 核心脚本和默认相机/控制脚本绑定的内容。这对于在正确的时间绑定/解除绑定您的操作或其他操作从您的操作中偷取输入有用。例如,如果你正在尝试绑定 W A S D ,那么默认角色移动脚本可能会绑定到这些相同的键上。同样,相机控制脚本可以偷走右键输入,如果脚本在你之后运行。

无键盘输入

该服务对支持游戏手柄和触摸输入特别有用。对于游戏手柄输入,您可能会选择将 B 按钮绑定到返回用户到上一个菜单时的动作。对于触摸,屏幕上的触摸按钮可以用于代替按键操作:这些按钮仅在操作绑定时显示,位置、文本和/或图像可以通过此服务配置。他们在此服务提供的自定义量有一定限制;通常使用 ImageButtonTextButton 制作自己的屏幕按钮会更好。

代码示例

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.

ContextActionService Tool Reload

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

()

将一个动作绑定到用户输入,给出一个处理动作函数。匹配输入执行后,行动处理器函数将调用以下列参数。有效输入枚举项目包括以关注中/正在关注内容:Enum.KeyCodeEnum.UserInputTypeEnum.PlayerActions。当玩家 进入可以执行操作的上下文时,调用此函数 当玩家离开上下文时,使用相同的 UnbindAction 调用 actionName .您可以使用 CallFunction 手动调用行动的处理功能。

下面的代码示例显示了一个 Sound 如何在按下键 ( H )、游戏手柄按钮或触摸屏按钮时保持 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.UserInput状态</code></td>
<td>输入状态(开始、更改、结束或取消*)</td>
</tr>
<tr>
<td>3</td>
<td><code>输入对象</code></td>
<td>包含输入信息的对象(根据用户输入类型而异)</td>
</tr>
#

† 这可以让一个函数同时处理多个操作,如果需要。※如果有输入正在进行中,另一个行动绑定到进行中的输入,或进行中的绑定行动是 unbound ,将发送取消。

行动绑定堆

行动绑定会像堆一样行动:如果两个行动绑定到同一个用户输入,那么最近绑定的 行动处理器 将被使用。如果一个动作处理器返回 Enum.ContextActionResult.Pass ,下一个最近绑定的动作处理器将被调用,直到一个处理器沉没输入(通过返回 nilEnum.ContextActionResult.Sink )。当 UnbindAction 被调用时,行动处理器将从堆中移除。这种堆栈行为可以使用 BindActionAtPriority 来覆盖,其中在 createTouchButton 之后的额外优先级参数可能会覆盖行动绑定的顺序(从高到低)。

触摸按钮

除了输入类型外,这个函数的第三个参数控制是否为 TouchEnabled 设备创建按钮。在第一个触摸按钮创作品后,一个名为“ContextActionGui”的ScreenGui被添加到PlayerGui。屏幕界面内部有一个 Frame 被称为“上下文按钮框架”的添加。在这个框架中,ImageButtons 用于绑定操作的父级;您可以使用 GetButton 来检索这些按钮以进行自定义。

参数

actionName: string

代表正在执行的操作的字符串(例如“HonkHorn”或“OpenDoor”)。

默认值:""
functionToBind: function

当绑定输入触发时,调用以下参数调用行动处理函数:string (actionName)、Enum.UserInputState 和一个输入对象。

默认值:""
createTouchButton: boolean

是否需要为触摸输入设备上的操作创建图形用户界面按钮。

默认值:""
inputTypes: Tuple

任何数量的 Enum.KeyCodeEnum.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.

ContextActionService Tool Reload

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".

General Action Handler

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.

Stacked Action Handlers

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 的普通“堆”行为。

参数

actionName: string

代表正在执行的操作的字符串(例如“HonkHorn”或“OpenDoor”)。

默认值:""
functionToBind: function

当绑定输入触发时,调用以下参数调用行动处理函数:string (actionName)、Enum.UserInputState 和一个输入对象。

默认值:""
createTouchButton: boolean

是否需要为触摸输入设备上的操作创建图形用户界面按钮。

默认值:""
priorityLevel: number

行动应该绑定到的优先级(从高到低考虑)。

默认值:""
inputTypes: Tuple

任何数量的 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.

ContextActionService BindAction Priorities

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 事件、ToolsGuiButtons 。当给定的键/按钮按下时,它会触发Mouse.Button1Down事件在鼠标发送到Tool.Equipped上。这反过来会触发 Tool.Activated 事件,如果 Tool.ManualActivationOnly 未设置为真实。对于游戏手柄输入,此函数由默认控制脚本调用,以便绑定按钮 R2 Enum.KeyCode

请注意,指定的 Enum.UserInputType 必须是 KeyboardGamepad1 通过 Gamepad8 才能有效。

参数

userInputTypeForActivation: Enum.UserInputType

必须是键盘或游戏手柄1通过游戏手柄8。

默认值:""
keyCodesForActivation: Tuple
默认值:""

返回

()

GetAllBoundActionInfo

GetAllBoundActioninfo 返回一个表,将所有行动的名称 (原本传递到 BindAction ) 映射到由 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 如果相关方法未被调用给出的动作。

参数

actionName: string
默认值:""

返回

GetCurrentLocalToolIcon

获取当前本地工具图标将返回 BackpackItem.TextureIdTool 目前 equippedPlayernil 如果没有此工具或玩家缺少 Character


返回

来自工具图像ID的内容字符串,或 nil 如果无法找到,则可以使用。

SetDescription

()

设置描述将设置由 BindAction 绑定的行动的描述。在可用行动列表中,这将是描述给定行动作的文本。

虽然名称可能暗示这种方法与定制触摸按钮的功能家族有关(SetTitleSetImageSetPosition),但此方法不会影响这样的按钮。这个方法仅设置了一个动作动的文本描述,无更多内容。

参数

actionName: string

行动的名称最初传给了 BindAction。

默认值:""
description: string

行动作的文本描述,例如“鸣响汽车的喇叭”或“打开道具”。

默认值:""

返回

()

代码示例

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.

ContextActionService Touch Button

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 返回了 null,这个函数什么也没有返回,也没有错误发生。

此函数是动作动的触摸按钮自定义家族的一部分。该家族的其他成员包括 SetPositionSetTitle

参数

actionName: string

行动的名称最初传给了 BindAction。

默认值:""
image: string

图像属性应设置为的值。

默认值:""

返回

()

代码示例

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.

ContextActionService Touch Button

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 返回了 null,这个函数什么也没有返回,也没有错误发生。

此函数是动作动的触摸按钮自定义家族的一部分。该家族的其他成员包括 SetImageSetTitle

参数

actionName: string

行动的名称最初传给了 BindAction。

默认值:""
position: UDim2

在 ContextButtonFrame 内的位置。

默认值:""

返回

()

代码示例

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.

ContextActionService Touch Button

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 返回了 null,这个函数什么也没有返回,也没有错误发生。

此函数是动作动的触摸按钮自定义家族的一部分。该家族的其他成员包括 SetImageSetPosition

参数

actionName: string

行动的名称最初传给了 BindAction。

默认值:""
title: string

按钮上显示的文本。

默认值:""

返回

()

代码示例

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.

ContextActionService Touch Button

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 或开发者控制台的“行动绑定”选项卡,您可以找出目前已绑定的行动。

参数

actionName: string
默认值:""

返回

()

代码示例

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.

ContextActionService Tool Reload

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.KeyCodeEnum.UserInputType 激活一个 Tool (或一个 HopperBin ) 使用 BindActivate 。该函数本质上撤销由该函数执行的操作。

参数

userInputTypeForActivation: Enum.UserInputType

最初发送到 BindActivate 的相同的用户输入类型。

默认值:""
keyCodeForActivation: Enum.KeyCode

最初发送到 BindActivate 的相同 KeyCode。

默认值:"Unknown"

返回

()

UnbindAllActions

()

移除所有绑定的函数。不会留下任何行动名称。所有触摸按钮将被移除。如果按钮是手动操作的,没有保证它会被清理。


返回

()

GetButton

暂停

获取按钮返回由 ImageButton 创建的 BindAction 如果其第三个参数为真,设备是 TouchEnabled 。这个函数的唯一参数必须与原始发送到 BindAction 的行动名称完全匹配。

如果没有绑定该操作或按钮未创建,该函数返回 nil

参数

actionName: string

行动的名称最初传给了 BindAction。

默认值:""

返回

由 BindAction 创建的图像按钮。

活动

LocalToolEquipped

当当前玩家装备了 Tool 时发生火焰。

参数

toolEquipped: Instance

LocalToolUnequipped

当当前玩家卸下 Tool 时发生火焰。

参数

toolUnequipped: Instance