概要
方法
BindAction(actionName: string,functionToBind: function,createTouchButton: boolean,inputTypes: Tuple):() |
BindActionToInputTypes(actionName: string,functionToBind: function,createTouchButton: boolean,inputTypes: Tuple):() |
BindActivate(userInputTypeForActivation: Enum.UserInputType,keyCodesForActivation: Tuple):() |
GetBoundActionInfo(actionName: string):Dictionary |
SetDescription(actionName: string,description: string):() |
SetPosition(actionName: string,position: UDim2):() |
UnbindAction(actionName: string):() |
UnbindActivate(userInputTypeForActivation: Enum.UserInputType,keyCodeForActivation: Enum.KeyCode):() |
UnbindAllActions():() |
活动
LocalToolEquipped(toolEquipped: Instance):RBXScriptSignal |
LocalToolUnequipped(toolUnequipped: Instance):RBXScriptSignal |
代码示例
上下文操作服务工具重新加载
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "重新加载"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("正在重新加载!")
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)API 参考
方法
BindAction
返回
()
代码示例
上下文操作服务工具重新加载
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "重新加载"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("正在重新加载!")
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)一般操作处理器
local ContextActionService = game:GetService("ContextActionService")
local function handleAction(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("处理操作: " .. actionName)
print(inputObj.UserInputType)
end
-- 由于这个函数不返回任何内容,因此这个处理器将
-- "沉没" 输入,之后将不会调用其他操作处理器
-- 在这个处理器之后。
end
ContextActionService:BindAction("BoundAction", handleAction, false, Enum.KeyCode.F)堆叠的操作处理程序
local ContextActionService = game:GetService("ContextActionService")
-- 为 FirstAction 定义操作处理程序
local function actionHandlerOne(actionName, inputState, _inputObj)
if inputState == Enum.UserInputState.Begin then
print("操作处理程序一: " .. actionName)
end
-- 此操作处理程序返回 nil,因此假定它
-- 正确处理了该操作。
end
-- 绑定操作 FirstAction(它在堆栈的底部)
ContextActionService:BindAction("FirstAction", actionHandlerOne, false, Enum.KeyCode.Z, Enum.KeyCode.X, Enum.KeyCode.C)
-- 为 SecondAction 定义操作处理程序
local function actionHandlerTwo(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("操作处理程序二: " .. actionName)
end
if inputObj.KeyCode == Enum.KeyCode.X then
return Enum.ContextActionResult.Pass
else
-- 返回 nil 隐性地沉没输入
return Enum.ContextActionResult.Sink
end
end
-- 在第一个操作之上绑定 SecondAction(由于它绑定得更接近,因此它在堆栈的顶部)
-- 请注意,SecondAction 使用的键与
ContextActionService:BindAction("SecondAction", actionHandlerTwo, false, Enum.KeyCode.Z, Enum.KeyCode.X)BindActionAtPriority
返回
()
代码示例
上下文操作服务绑定操作优先级
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(`操作 [{actionName}] 发生了。 KeyCode [{inputObject.KeyCode}] 被按下。`)
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(`操作 [{actionName}] 发生了。 KeyCode [{inputObject.KeyCode}] 被按下。`)
return Enum.ContextActionResult.Sink
end
-- 如果没有指定优先级,最近绑定的操作会优先调用,
-- 因此按下 INPUT_KEY1 会打印 "Punch",然后消耗输入。
ContextActionService:BindAction("DefaultThrow", handleThrow, false, INPUT_KEY1)
ContextActionService:BindAction("DefaultPunch", handlePunch, false, INPUT_KEY1)
-- 在这里,我们以与上述相同的顺序绑定两个函数,但明确交换了优先级。
-- 即,我们给 "Throw" 更高的优先级 2,这样它将首先被调用,
-- 尽管 "Punch" 仍然是最近绑定的。
-- 按下 INPUT_KEY2 打印 "Throw",然后消耗输入。
ContextActionService:BindActionAtPriority("PriorityThrow", handleThrow, false, 2, INPUT_KEY2)
ContextActionService:BindActionAtPriority("PriorityPunch", handlePunch, false, 1, INPUT_KEY2)BindActionToInputTypes
BindActivate
ContextActionService:BindActivate(
):()
参数
返回
()
GetBoundActionInfo
参数
SetDescription
返回
()
代码示例
上下文操作服务触摸按钮
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- 带 ? 的对话框图像
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("正在检查")
end
end
-- 对于触摸设备,通过第三个参数在屏幕上自动创建一个按钮
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- 我们可以使用这些函数来自定义按钮:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "查看")
ContextActionService:SetDescription(ACTION_INSPECT, "检查某物。")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- 我们可以直接使用 ContextActionService:GetButton 操作按钮
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- 请记住:非触摸设备将不会返回任何内容!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- 将 ImageButton 染成绿色
endSetImage
返回
()
代码示例
上下文操作服务触摸按钮
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- 带 ? 的对话框图像
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("正在检查")
end
end
-- 对于触摸设备,通过第三个参数在屏幕上自动创建一个按钮
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- 我们可以使用这些函数来自定义按钮:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "查看")
ContextActionService:SetDescription(ACTION_INSPECT, "检查某物。")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- 我们可以直接使用 ContextActionService:GetButton 操作按钮
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- 请记住:非触摸设备将不会返回任何内容!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- 将 ImageButton 染成绿色
endSetPosition
返回
()
代码示例
上下文操作服务触摸按钮
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- 带 ? 的对话框图像
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("正在检查")
end
end
-- 对于触摸设备,通过第三个参数在屏幕上自动创建一个按钮
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- 我们可以使用这些函数来自定义按钮:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "查看")
ContextActionService:SetDescription(ACTION_INSPECT, "检查某物。")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- 我们可以直接使用 ContextActionService:GetButton 操作按钮
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- 请记住:非触摸设备将不会返回任何内容!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- 将 ImageButton 染成绿色
endSetTitle
返回
()
代码示例
上下文操作服务触摸按钮
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- 带 ? 的对话框图像
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("正在检查")
end
end
-- 对于触摸设备,通过第三个参数在屏幕上自动创建一个按钮
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- 我们可以使用这些函数来自定义按钮:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "查看")
ContextActionService:SetDescription(ACTION_INSPECT, "检查某物。")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- 我们可以直接使用 ContextActionService:GetButton 操作按钮
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- 请记住:非触摸设备将不会返回任何内容!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- 将 ImageButton 染成绿色
endUnbindAction
参数
返回
()
代码示例
上下文操作服务工具重新加载
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "重新加载"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("正在重新加载!")
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
ContextActionService:UnbindActivate(
):()
参数
| 默认值:"Unknown" |
返回
()
UnbindAllActions
ContextActionService:UnbindAllActions():()
返回
()
活动
LocalToolEquipped
参数
LocalToolUnequipped
参数