GamepadService
*该内容即将支持你的所选语言。
The GamepadService is internally responsible for handling inputs from various controllers, such as Xbox One or PlayStation DualShock controllers. It also handles APIs used with the gamepad virtual cursor. You can enable the gamepad cursor for your experience by setting Enum.VirtualCursorMode under StarterGui to Enabled.
概要
属性
The state of the gamepad virtual cursor.
方法
Disables the gamepad cursor, if currently enabled.
Enables the gamepad cursor or updates its position.
属性
GamepadCursorEnabled
This boolean is a read only variable that maintains the state of the gamepad virtual cursor.
代码示例
local gamepadService = game.GamepadService
gamepadService:GetPropertyChangedSignal("GamepadCursorEnabled"):Connect(function()
local enabled = gamepadService.GamepadCursorEnabled
if enabled then
-- Custom code when the virtual cursor is enabled
else
-- Custom code when the virtual cursor is disabled
end
end)
方法
DisableGamepadCursor
This function disables the gamepad cursor, if it's currently enabled.
返回
代码示例
local gamepadService = game.GamepadService
local userInputService = game.UserInputService
userInputService.InputBegan:Connect(function(inputObject, gameProcessed)
if inputObject.KeyCode == Enum.KeyCode.ButtonB then
gamepadService:DisableGamepadCursor()
end
end)
EnableGamepadCursor
This function enables the gamepad cursor if it's currently disabled. If the cursor is already enabled, calling the API updates the cursor's position. The function accepts a GuiObject parameter, but there are some invalid cases. Please note that in order to set the cursor to the default position, nil must be passed in as a parameter. Providing no argument will result in an error.
Input GuiObject | Cursor Starting Position | |
Nil | Default position | |
Not a GuiObject | Cursor does not appear, errors | |
Not a child of BasePlayerGui | Cursor does not appear, errors | |
GuiObject has Visible 2 set to false or any parent visible set to false | Default position with warning | |
Child of ScreenGui 1 with Enabled 1 set to false | Default position with warning | |
Child of BillboardGui 4 or SurfaceGui | Default position with warning | |
GuiObject outside the viewport (Or, any part of the gui object that is off screen) | Default position with warning | |
Child of ScrollingFrame with clipping set to false: GuiObject outside of scrolling frame (Object child of scrolling frame and not visible on screen) | Default position with warning | |
Child of ScrollingFrame with clipping set to true: GuiObject outside scrolling frame window but inside viewport | GuiObject moves into the scrolling frame and starts the cursor centered over the object | |
GuiObject inside the frame and visible | Cursor starts centered on the GuiObject |
参数
返回
代码示例
local gamepadService = game.GamepadService
local userInputService = game.UserInputService
local startObject = script.Parent
userInputService.InputBegan:Connect(function(inputObject, gameProcessed)
if inputObject.KeyCode == Enum.KeyCode.ButtonA then
gamepadService:EnableGamepadCursor(startObject)
end
end)