概要
方法
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 = "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 = "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(`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
-- 在不指定優先級的情況下,最近綁定的行為會最先調用,
-- 所以按下 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 = "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
參數