UE5C++使用增强输入

参考文章和视频:

UE5 – EnhancedInput(增强输入系统)

UE5虚幻引擎C++【第一期】使用增强输入系统

UE5使用了新的输入系统:增强输入系统

原系统的缺点:

  1. 实现的是基础简单的功能,想要复杂的输入机制就需要在User层做更多的工作。
  2. 过于简陋,需自己实现众多行为。例如按住、双击、联合输入等都需要用户自己实现。

增强输入系统:

它以模块化的方式解耦了从输入的按键配置到事件处理的逻辑处理过程,通过提供输入动作(UInputAction)、输入修改器(UInputModifier)、输入触发器(UInputTrigger)和输入映射环境(UInputMappingContext)这些可组合功能,在新的增强玩家输入(UEnhancedPlayerInput)和增强输入组件( UEnhancedInputComponent)的配合下提供了更灵活和更便利的输入配置和处理功能。

1.在xx.Build.cs中添加 EnhancedInput模块

PublicDependencyModuleNames.AddRange(new string[]
        {
	        "EnhancedInput"
        });

2. 在UEC++中创建相应的变量

xxxxcharacter.h中进行配置(自己的character中进行配置)

这里的UInputAction可以设置多个,因为我设置了一个键盘按键,所以只使用了一个

//character.h

virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
	

// input system
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Input")
TObjectPtr<UInputMappingContext> InputMappingContext;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Input")
TObjectPtr<UInputAction> StartMove;

创建后运行,打开相应的character蓝图细节,出现右侧的部分即为设置成功

image-20251219131042330

3. 在蓝图中创建对应的映射文件

配置文件为:InputMappingContextInputAction

创建文件如下

image-20251219131248737

新建输入映射情景(InputMappingContext)

命名为IMC_InputMapping,并进行一下配置

image-20251219153921122

image-20251219131310579

新建输入操作(InputAction)

命名为IA_startMove,并进行一下配置

image-20251219154028449

image-20251219131325144

创建好后回到character的细节中,设置UEC++中设置好的变量

image-20251219131442912

4. 返回UEC++设置配置函数

配置函数设置在xxxcharacter.cpp

配置的详细解释都在注释中

/**
 * @brief 设置玩家输入组件,初始化增强输入系统的映射上下文和动作绑定
 *
 * 该函数重写了父类的SetupPlayerInputComponent方法,用于配置角色的输入系统。
 * 主要功能包括:
 * - 添加输入映射上下文(Input Mapping Context)
 * - 绑定StartMove动作到MoveBySpline函数
 *
 * @param PlayerInputComponent 指向玩家输入组件的指针,由引擎自动传入
 */
void ACourseWorkTest1Character::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	/**
	 * @brief 初始化玩家输入系统并绑定移动动作
	 * 
	 * 该函数负责设置增强输入系统,包括添加输入映射上下文和绑定游戏动作。
	 * 首先获取玩家控制器,然后通过本地玩家获取增强输入子系统实例,
	 * 将指定的输入映射上下文添加到子系统中。
	 * 接着将输入组件转换为增强输入组件,并将StartMove动作与角色的MoveBySpline函数进行绑定。
	 * 
	 * @note 该代码段假设GetController()能正确返回控制器实例
	 * @note 需要确保InputMappingContext和StartMove已正确定义和初始化
	 */
	if (APlayerController* PlayerController = Cast<APlayerController>(GetController()))
	{
		// 获取增强输入子系统并添加映射上下文
		// 通过本地玩家获取UEnhancedInputLocalPlayerSubsystem子系统实例,
		// 如果获取成功,则将指定的输入映射上下文添加到子系统中
		if (UEnhancedInputLocalPlayerSubsystem* Subsystem =
			ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
		{
			Subsystem -> AddMappingContext(InputMappingContext, 0);
		}
		
		// 检查并转换输入组件为增强输入组件,如果转换成功则绑定移动动作
		// 该代码块的作用是将StartMove动作与角色的MoveBySpline函数进行绑定
		// 当触发StartMove动作时,会调用ACourseWorkTest1Character类的MoveBySpline方法
		if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
		{
			EnhancedInputComponent->BindAction(StartMove, ETriggerEvent::Triggered, this, &ACourseWorkTest1Character::MoveBySpline);
		}
	}
	
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值