ContextActionService는 게임플레이 중 키보드, 마우스, 터치 등 다양한 입력 방식을 효과적으로 처리하고, 상황에 맞는 인터페이스를 손쉽게 구현할 수 있도록 도와줍니다. 이를 통해 플레이어 입력을 간편하게 관리하고, 게임 내 Context에 따라 유연하게 동작을 할당하거나 해제할 수 있습니다.
특징
키보드, 마우스, 터치 등 여러 입력 장치를 동일한 방식으로 처리할 수 있습니다.
PC 환경의 Studio와 모바일 플랫폼에서 호환되며, 입력 처리를 간소화합니다.
메뉴 화면, 게임플레이 등 특정 상황에 따라 필요한 입력만 활성화할 수 있습니다.
UserInputState를 통해 입력 상태를 구분해 세부적인 동작을 설정할 수 있습니다.
사용 방법
1. 액션 생성하기
LocalScript에서 BindAction 함수를 이용해 액션을 생성할 수 있습니다. 액션을 생성할 때, 액션의 이름과 TouchButton 생성 여부, PC에서 사용할 입력 키를 지정할 수 있습니다.
local ContextActionService = game:GetService("ContextActionService")
local ActionName = "JumpAction"
ContextActionService:UnbindAction(ActionName)
local function OnAction(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
print("Begin!")
elseif inputState == Enum.UserInputState.Change then
print("Change!")
elseif inputState == Enum.UserInputState.End then
print("End!")
elseif inputState == Enum.UserInputState.Cancel then
print("Cancel!")
end
end
ContextActionService:BindAction(ActionName, OnAction, IsCreateTouchButton, KeyCode)
local ActionButton = ContextActionService:GetButton(ActionName)
local ContextActionService = game:GetService("ContextActionService")
local AllActions = ContextActionService:GetAllBoundActionInfo()
for actionName, actionInfo in pairs(AllActions) do
print("Action Name : ", actionName)
print("Input Types : ", actionInfo.InputTypes)
end
local StarterGui = game:GetService("StarterGui")
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Joystick, false)
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.JumpButton, false)
local UserInputService = game:GetService("UserInputService")
local ActiveTouches = {} -- 동시에 발생하는 여러 터치 입력을 개별적으로 처리하기 위한 테이블
local function OnScreenTouchStart(input, _gameProcessed)
local keyCode = input.KeyCode
if keyCode == Enum.KeyCode.Joystick then
return
end
table.insert(ActiveTouches, input)
local inputState = input.UserInputState -- Begin
local inputType = input.UserInputType -- Touch
local delta = input.Delta
local pos = input.Position
-- Do Something
end
UserInputService.TouchStarted:Connect(OnScreenTouchStart)
local function OnScreenTouchMove(input, _gameProcessed)
local keyCode = input.KeyCode
if keyCode == Enum.KeyCode.Joystick then
return
end
for i = 1, #ActiveTouches do
-- 여러 터치 입력 중에서 현재 입력(input)에 해당하는 터치를 탐색
if input == ActiveTouches[i] then
local inputState = input.UserInputState -- Change
local inputType = input.UserInputType -- Touch
local delta = input.Delta
local pos = input.Position
-- Do Something
end
end
end
UserInputService.TouchMoved:Connect(OnScreenTouchMove)
local function OnScreenTouchEnd(input, _gameProcessed)
local keyCode = input.KeyCode
if keyCode == Enum.KeyCode.Joystick then
return
end
local i
for j = 1, #ActiveTouches do
if input == ActiveTouches[j] then
i = j
break
end
end
local inputState = input.UserInputState -- End
local inputType = input.UserInputType -- Touch
local delta = input.Delta
local pos = input.Position
-- Do Something
table.remove(ActiveTouches, i)
end
UserInputService.TouchEnded:Connect(OnScreenTouchEnd)
local UserInputService = game:GetService("UserInputService")
local function OnJoystickStart(input, _gameProcessed)
local keyCode = input.KeyCode
if keyCode ~= Enum.KeyCode.Joystick then
return
end
local inputState = input.UserInputState -- Begin
local inputType = input.UserInputType -- Touch
local delta = input.Delta
local pos = input.Position
-- Do Something
end
UserInputService.TouchStarted:Connect(OnJoystickStart)
local function OnJoystickMove(input, _gameProcessed)
local keyCode = input.KeyCode
if keyCode ~= Enum.KeyCode.Joystick then
return
end
local inputState = input.UserInputState -- Change
local inputType = input.UserInputType -- Touch
local delta = input.Delta
local pos = input.Position
-- Do Something
end
UserInputService.TouchMoved:Connect(OnJoystickMove)
local function OnJoystickEnd(input, _gameProcessed)
local keyCode = input.KeyCode
if keyCode ~= Enum.KeyCode.Joystick then
return
end
local inputState = input.UserInputState -- Change
local inputType = input.UserInputType -- Touch
local delta = input.Delta
local pos = input.Position
-- Do Something
end
UserInputService.TouchEnded:Connect(OnJoystickEnd)