主题
InputBinding
概览
| 字段 | 值 |
|---|---|
| Kind | Unit · 单位 Unit |
| Realm | common |
继承关系
- Unit(5 属性 / 28 函数 / 6 事件)
- [InputBinding](21 属性 / 1 函数)
继承成员
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
- 属性:
InputBinding 负责把一路具体的硬件输入映射为其所属输入动作的信号来源:可以用 KeyCode 指定主按键,或通过 Up、Down、Left、Right、Forward、Backward 组合出方向输入,并用 PrimaryModifier、SecondaryModifier 追加修饰键。它还提供 PressedThreshold 与 ReleasedThreshold 控制按下和松开的判定阈值,并借助 Scale、Vector2Scale、Vector3Scale 以及 ClampMagnitudeToOne 对输出幅值做统一缩放或分轴缩放。绑定目标既可以是键盘、鼠标或触摸指针(由 PointerIndex 区分),也可以指向 EUIButton 屏幕按钮,并用 UIModifier 作为按住才生效的门控。
适用场景
适用于需要自定义操作映射的场合,例如把一组按键拼成移动方向并挂到某个输入动作下,或把界面按钮设为触发源,让玩家点击屏幕按钮即可产生输入。
使用要点
先用 game:CreateUnit("InputBinding", config) 创建实例,服务端侧也可用 World:CreateUnit;再把它设为对应 InputAction 的子级。之后配置 KeyCode 或方向键、修饰键、阈值与各类缩放属性;若要让脚本自行驱动输入,可调用 Fire(state) 主动注入状态。
注意事项
InputBinding 只能通过游戏创建接口或公开接口的返回值获得,不能自行构造。它需要依附在正确的输入动作父级下才会参与输入解析,脱离父级的绑定不会生效。Fire 注入的状态与真实硬件输入共用同一套阈值和缩放配置,并且 Type 属性决定该绑定由硬件自动驱动还是由脚本驱动。
代码示例
创建 InputBinding 实例
lua
-- @runtime client
local World = game:GetService('World')
local inputBinding = World:CreateUnit('InputBinding', {})
if inputBinding == nil then return end
print('InputBinding 已创建:', inputBinding.Name)属性 (21)
| Name | 类型 | 默认值 | 说明 |
|---|---|---|---|
KeyCode | KeyCode | Enums.KeyCode.None | KeyCode 是 InputBinding 的主输入按键属性,类型为 KeyCode,用于指定该绑定主要响应的按键。 |
Type | InputBindingType | Enums.InputBindingType.Automatic | Type 是 InputBinding 的绑定类型属性,类型为 InputBindingType 枚举,用于区分绑定的工作方式。 |
Up | KeyCode | Enums.KeyCode.None | Up 是 InputBinding 的按键属性,类型为 KeyCode,用于指定该绑定在正 Y 方向上的输入按键。 |
Down | KeyCode | Enums.KeyCode.None | Down 是 InputBinding 的按键属性,类型为 KeyCode,用于指定该绑定在负 Y 方向上的输入按键。 |
Left | KeyCode | Enums.KeyCode.None | Left 是 InputBinding 的按键属性,类型为 KeyCode,用于指定该绑定在负 X 方向上的输入按键。 |
Right | KeyCode | Enums.KeyCode.None | Right 是 InputBinding 的按键属性,类型为 KeyCode,用于指定该绑定在正 X 方向上的输入按键。 |
Forward | KeyCode | Enums.KeyCode.None | Forward 是 InputBinding 的按键属性,类型为 KeyCode,用于指定该绑定在负 Z 方向上的输入按键。 |
Backward | KeyCode | Enums.KeyCode.None | Backward 是 InputBinding 的按键属性,类型为 KeyCode,用于指定该绑定在正 Z 方向上的输入按键。 |
PrimaryModifier | KeyCode | Enums.KeyCode.None | PrimaryModifier 是 InputBinding 的按键属性,类型为 KeyCode,用于指定主修饰键。 |
SecondaryModifier | KeyCode | Enums.KeyCode.None | SecondaryModifier 是 InputBinding 的按键属性,类型为 KeyCode,用于指定次修饰键。 |
PressedThreshold | Float | 0.5 | PressedThreshold 是 InputBinding 的浮点属性,表示判定为按下状态的阈值。 |
ReleasedThreshold | Float | 0.2 | ReleasedThreshold 是 InputBinding 的浮点属性,表示判定为松开状态的阈值。 |
Scale | Float | 1 | Scale 是 InputBinding 的浮点属性,用于对绑定输出的数值做统一缩放。 |
Vector2Scale | Vector2 | [1, 1] | Vector2Scale 是 InputBinding 的 Vector2 属性,用于对二维输入的 X、Y 两个轴分别缩放。 |
Vector3Scale | Vector3 | [1, 1, 1] | Vector3Scale 是 InputBinding 的 Vector3 属性,用于对三维输入的 X、Y、Z 三个轴分别缩放。 |
ClampMagnitudeToOne | Bool | true | ClampMagnitudeToOne 是 InputBinding 的布尔属性,用于控制是否把方向输入的幅值限制在 1 以内。 |
PointerIndex | Int | 0 | PointerIndex 是 InputBinding 的整数属性,用于指定该绑定关联的触摸指针索引。 |
DisplayName | String | "" | DisplayName 是 InputBinding 的字符串属性,用于存放该绑定的显示名称。 |
DisplayImage | String | "" | DisplayImage 是 InputBinding 的字符串属性,用于存放该绑定的显示图标资源标识。 |
UIButton | EUIButton | - | UIButton 是 InputBinding 的属性,类型为 EUIButton,表示作为该绑定输入源的屏幕按钮。 |
UIModifier | EUIButton | - | UIModifier 是 InputBinding 的属性,类型为 EUIButton,表示作为按住门控使用的 UI 修饰按钮。 |
关联类型
函数 (1)
Fire
签名:Fire(state: Any) -> void
Fire 是 InputBinding 的成员函数,用于手动触发该绑定,并向其传入一个状态值。
| 参数 | 类型 | 说明 |
|---|---|---|
state | Any | 按父动作类型传值。Bool 接受布尔值,Direction1D 接受数值,Direction2D 和 ViewportPosition 接受 Vector2,Direction3D 接受 Vector3。 |
返回值 void
示例代码
脚本主动触发 Scriptable 输入绑定
lua
-- @runtime client
local inputAction = game:CreateUnit("InputAction", { Type = Enums.InputActionType.Bool })
if inputAction == nil then return end
local inputBinding = game:CreateUnit("InputBinding", {
Type = Enums.InputBindingType.Scriptable,
Parent = inputAction,
})
if inputBinding == nil then return end
inputBinding:Fire(true)
print("调用完成", inputBinding)