1117 字
6 分钟
Enhanced Input——UE5 的新输入框架

一、传统输入绑定有什么问题#

UE4 时代的标准做法——在 SetupPlayerInputComponent 里硬编码:

void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) {
PlayerInputComponent->BindAxis("MoveForward", this, &AMyCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &AMyCharacter::MoveRight);
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AMyCharacter::StartJump);
PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &AMyCharacter::Fire);
}

问题:

  • 按键和逻辑代码耦合——想改跳跃键从 Space 到 A?得改代码或 Project Settings
  • 不同场景不同按键没法优雅处理——开车时 Space 是刹车、走路时是跳跃——需要手写条件判断
  • 组合键/长按/双击需要手写计时器逻辑
  • 手柄 vs 键鼠的切换麻烦

二、三层架构#

Enhanced Input 把输入拆成三层:

InputAction (资产) → "做什么"(Jump / Move / Fire / Interact)
↓ 被包含在
InputMappingContext (资产) → "什么时候这些 Action 生效"(OnFoot / InVehicle / InMenu)
↓ 被消费时经过
Trigger + Modifier → "怎么做"(Tap / Hold / Chord / DeadZone)
↓ 绑定到
C++ / BP Callback → 最终执行逻辑

1 InputAction:定义一个”操作”#

InputAction 不绑定按键——只声明”存在这样一个操作”:

IA_Jump → 跳跃
IA_Move → 移动(2D Axis)
IA_Look → 视角(2D Axis)
IA_Fire → 开火
IA_Interact → 交互
IA_Skill_Q → 技能 Q

ValueType:Bool / Axis1D / Axis2D / Axis3D。

2 InputMappingContext:按键到 Action 的映射#

这才是”哪个按键触发哪个 Action”的地方。可以动态切换——在车里加载 IMC_Vehicle,下车加载 IMC_OnFoot

IMC_OnFoot:
W/A/S/D → IA_Move
Mouse XY → IA_Look
Space → IA_Jump
E → IA_Interact
IMC_Vehicle:
W/S → IA_Throttle
A/D → IA_Steer
Space → IA_Handbrake ← 一样的 Space 键!不同 Context 不同行为
E → IA_ExitVehicle

切换方式

// 下车时
Subsystem->RemoveMappingContext(IMC_Vehicle);
Subsystem->AddMappingContext(IMC_OnFoot, Priority);

不需要改任何按键判断代码——换一个 Mapping Context 就行。

3 Trigger 和 Modifier#

每个按键映射可以配置 Trigger(触发条件)和 Modifier(值修改):

Trigger行为
Pressed按下触发
Released松开触发
Hold按住超过 HoldTimeThreshold 持续触发
Tap快速按下松开(TapTimeThreshold)
Chord组合键——按住 Ctrl 的同时按另一个键
Modifier行为
Dead Zone手柄摇杆死区——小于阈值的输入忽略
Negate反转值(把向右变成向左)
Swizzle Axis交换 XY 轴
Scale乘一个系数(灵敏度设置)
// 示例:瞄准时降低视角灵敏度
IMC_OnFoot:
Mouse XY → IA_Look
├── Modifier: DeadZone(0.05) ← 手柄摇杆死区
└── Trigger: Hold(0.0) ← 按住持续触发

三、C++ 侧的绑定#

void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) {
// 获取 Enhanced Input 子系统
UEnhancedInputComponent* EnhancedInput = CastChecked<UEnhancedInputComponent>(PlayerInputComponent);
// 绑定 InputAction
EnhancedInput->BindAction(IA_Jump, ETriggerEvent::Triggered, this, &AMyCharacter::Jump);
// 带值的绑定(Axis)
EnhancedInput->BindAction(IA_Move, ETriggerEvent::Triggered, this, &AMyCharacter::Move);
// 不同触发事件
EnhancedInput->BindAction(IA_Fire, ETriggerEvent::Started, this, &AMyCharacter::StartFire);
EnhancedInput->BindAction(IA_Fire, ETriggerEvent::Completed, this, &AMyCharacter::StopFire);
}
void AMyCharacter::Move(const FInputActionValue& Value) {
FVector2D MovementVector = Value.Get<FVector2D>();
AddMovementInput(GetActorForwardVector(), MovementVector.Y);
AddMovementInput(GetActorRightVector(), MovementVector.X);
}

不用在 Project Settings 里配置任何 Axis/Action 映射了——全在资产里。


四、与 GAS 集成:InputTag 自动绑定技能#

GAS 可以监听 InputTag——当 Enhanced Input 触发一个 Action 时,如果该 Action 的 Tag 匹配了某个技能的 InputTag,ASC 自动激活该技能:

IA_Skill_Q → 触发 → Action Tag = InputTag.Skill.Q
↓ 匹配
GA_RevealTwo (配置了 InputTag = InputTag.Skill.Q) → ASC 自动激活

不需要手写

// 旧方式——每个技能手动绑定
EnhancedInput->BindAction(IA_Skill_Q, ..., this, &AMyCharacter::OnSkillQ);
void OnSkillQ() {
ASC->TryActivateAbilityByTag(Tag_Ability_Sofia_Reveal);
}
// 新方式——什么都不写
// 只需要在技能里配 InputTag = InputTag.Skill.Q
// EnhancedInput 触发时,GAS 自动找到对应技能激活

五、实战:RPG 角色的输入配置#

IMC_Character:
左键点击 → IA_Interact (交互)
鼠标左键 → IA_LightAttack (轻攻击)
鼠标右键 → IA_Aim (瞄准)
数字键 1-5 → IA_SelectAbility (选择技能)
R → IA_Reload (装填)
Esc → IA_OpenMenu (打开菜单)
Q → IA_Ability_Q (技能 Q)
E → IA_Ability_E (技能 E)
Tab → IA_ShowQuestLog (查看任务日志)
Mouse Scroll → IA_ZoomCamera (缩放相机)

不需要在角色代码里到处写 if 判断”当前是不是战斗状态” ——探索、战斗、载具和菜单各自添加/移除对应的 Mapping Context。进入战斗时,Q 激活技能;打开地图时,Q 可能是”关闭地图”。


六、总结#

概念一句话
InputAction”做什么”——不绑定按键的纯逻辑操作
MappingContext”什么时候做”——按键到操作的一对一映射,可动态切换
Trigger”怎么做”——Tap、Hold、Chord 等触发条件
Modifier值的预处理——DeadZone、Scale、Swizzle
GAS 集成InputAction 的 Tag → 匹配技能 InputTag → 自动激活

Enhanced Input 的设计思想就是两个字:解耦。按键和操作解耦、操作和逻辑解耦、触发条件和值的预处理也单独解耦。改按键不需要程序员、加手柄支持不需要改代码——这些都是引擎工具链成熟化的标志。

Enhanced Input——UE5 的新输入框架
https://www.m4doka.xyz/posts/ue/ue-6-enhanced-input/
作者
m4doka
发布于
2026-08-12
许可协议
CC BY-NC-SA 4.0