主题
InputAction
概览
| 字段 | 值 |
|---|---|
| Kind | Unit · 单位 Unit |
| Realm | common |
继承关系
- Unit(5 属性 / 28 函数 / 6 事件)
- [InputAction](4 属性 / 1 函数 / 3 事件)
继承成员
1 个来源 / 5 属性 / 28 函数 / 6 事件
- 来自 Unit(5 属性 / 28 函数 / 6 事件)
- 属性:
AssetId、Name、Desc、Parent、Tags - 函数:
ClearAllChildren、GetChildren、GetDescendants、IsAncestorOf、IsDescendantOf、FindFirstAncestor、FindFirstAncestorWhichIsA、FindFirstAncestorOfClass、FindFirstChild、FindFirstChildWhichIsA、FindFirstChildOfClass、HasChildren、GetChildCount、GetChildAtIndex、SetAttribute、GetAttribute、GetAllProps、Clone、IsA、Destroy、GetPropertyChangedSignal、GetAttributeChangedSignal、FindFromPath、GetFullPath、GetDebugId、HasTag、AddTag、RemoveTag - 事件:
ChildAdded、ChildRemoved、DescendantAdded、DescendantRemoving、AncestryChanged、Destroying
- 属性:
输入动作把玩法语义与具体按键或摇杆输入解耦,用类型、启用状态、显示名称和首选输入绑定描述一个可被查询的操作入口。它提供状态查询方法,并在按下、松开以及状态值变化时触发本地事件,让逻辑无需关心底层输入来源。
适用场景
适合需要统一管理操作入口的场合,例如为移动、跳跃等行为各声明一个输入动作,在界面提示或按键说明处读取其首选输入绑定,并在本地监听其状态变化来驱动表现层反馈。
使用要点
先通过 game:CreateUnit("InputAction", config) 或 World:CreateUnit("InputAction", config) 创建实例,并在配置中指定 Type 与 PreferredBinding,按需设置 Enabled、DisplayName。随后在本地连接 Pressed、Released 或 StateChanged 事件响应输入变化,也可以在每帧逻辑中调用 GetState 主动读取当前状态。
注意事项
该类型只能通过 game:CreateUnit 或 World:CreateUnit 创建,不能直接构造;三个事件均为本地事件,仅在本地触发,不要期望在服务端收到。PreferredBinding 需要指向一个已存在的 InputBinding 实例,未正确关联时动作可能无法获取输入。
代码示例
创建 InputAction 实例
lua
-- @runtime client
local World = game:GetService('World')
local inputAction = World:CreateUnit('InputAction', {})
if inputAction == nil then return end
print('InputAction 已创建:', inputAction.Name)属性 (4)
| Name | 类型 | 默认值 | 说明 |
|---|---|---|---|
Type | InputActionType | Enums.InputActionType.Bool | 动作类型,指定该输入动作所表达的输入形态,取值来自输入动作类型枚举。 |
Enabled | Bool | true | 是否启用,控制该输入动作是否处于启用状态。 |
DisplayName | String | "" | 显示名称,用于为该输入动作提供一个面向玩家展示的文本名称。 |
PreferredBinding | InputBinding | - | 首选输入绑定,表示该输入动作优先使用的输入绑定对象。 |
关联类型
事件 (3)
Pressed
签名:Pressed() 触发:local
按下事件,当该输入动作被判定为进入按下状态时触发,用于响应玩家的输入起始动作。
示例代码
监听 InputAction.Pressed 事件的示例。
lua
-- @runtime client
local inputAction = game:CreateUnit("InputAction", {})
inputAction.Pressed:Once(function()
print("Pressed event")
end)Released
签名:Released() 触发:local
释放事件,当该输入动作被判定为退出按下状态时触发,用于响应玩家的输入结束动作。
示例代码
监听 InputAction.Released 事件的示例。
lua
-- @runtime client
local inputAction = game:CreateUnit("InputAction", {})
inputAction.Released:Once(function()
print("Released event")
end)StateChanged
签名:StateChanged(value: Any) 触发:local
状态变化事件,当输入动作的当前状态发生改变时触发,并携带变化后的状态值。
| 回调参数 | 类型 | 说明 |
|---|---|---|
value | Any | 新的状态 |
示例代码
监听 InputAction.StateChanged 事件的示例。
lua
-- @runtime client
local inputAction = game:CreateUnit("InputAction", {})
inputAction.StateChanged:Once(function(value)
print("value: " .. tostring(value))
end)函数 (1)
GetState
签名:GetState() -> Any (当前状态)
获取输入动作当前的输入状态值。
返回值 Any (当前状态)
示例代码
调用 InputAction:GetState 的示例。
lua
-- @runtime client
local inputAction = game:CreateUnit("InputAction", {})
local result = inputAction:GetState() -- 返回 Any
if result ~= nil then
print("调用成功,结果: " .. tostring(result))
end