目标:输入绑定流程
(1)原生游戏玩法标签
定义输入标签
(2)输入配置数据资产
将输入标签映射到输入活动
(3)自定义增强输入组件
定义输入绑定方法
(4)绑定输入
(5)在编辑器里分配资产

现在执行第三步操作:定义输入绑定方法
1.通过UEnhancedInputComponent创建子类:UWarriorInputComponent
定义一个模板函数,来绑定回调函数

2.实现:

参数解释
-
InInputConfig (const UWarriorDataAssetInputConfig*):- 类型:指向
UWarriorDataAssetInputConfig类的常量指针。 - 说明:这是一个输入配置对象,包含了各种输入动作的配置信息。
- 类型:指向
-
InInputTag (const FGameplayTag&):- 类型:对
FGameplayTag的常量引用。 - 说明:用于标识特定的输入动作。
- 类型:对
-
TriggerEvent (ETriggerEvent):- 类型:枚举
ETriggerEvent。 - 说明:指定触发回调函数的事件类型,如按下、释放等。
- 类型:枚举
-
ContextObject (UserObject*):- 类型:指向
UserObject类型的指针。 - 说明:这是回调函数的上下文对象,即回调函数内部可能会使用的对象。
- 类型:指向
-
Func (CallbackFunc):- 类型:函数回调类型,具体类型由模板参数
CallbackFunc指定。 - 说明:这是需要绑定的回调函数。
- 类型:函数回调类型,具体类型由模板参数
函数功能
- 该模板函数用于将指定的回调函数
Func绑定到由InInputTag标识的输入动作上。 - 当输入动作在
TriggerEvent指定的事件发生时,将调用Func回调函数。 ContextObject作为回调函数的上下文对象,可以在回调函数内部使用。
Source/Warrior/Public/Components/Input/WarriorInputComponent.h:
// Copyright @ ChenChao
#pragma once
#include "CoreMinimal.h"
#include "EnhancedInputComponent.h"
#include "DataAssets/Input/WarriorDataAssetInputConfig.h"
#include "WarriorInputComponent.generated.h"
/**
*
*/
UCLASS()
class WARRIOR_API UWarriorInputComponent : public UEnhancedInputComponent
{
GENERATED_BODY()
public:
//模板函数: 绑定回调函数
template<class UserObject, typename CallbackFunc>
void BindNativeInputAction(const UWarriorDataAssetInputConfig* InInputConfig, const FGameplayTag& InInputTag,
ETriggerEvent TriggerEvent, UserObject* ContextObject, CallbackFunc Func);
};
//模板函数: 绑定回调函数
template <class UserObject, typename CallbackFunc>
void UWarriorInputComponent::BindNativeInputAction(const UWarriorDataAssetInputConfig* InInputConfig,
const FGameplayTag& InInputTag, ETriggerEvent TriggerEvent, UserObject* ContextObject, CallbackFunc Func)
{
//检查指针
checkf(InInputConfig,TEXT("InInputConfig 为空指针,无法继续执行绑定操作!"));
//通过标签查到输入动作
UInputAction* InputAction = InInputConfig->GetNativeInputActionsByTag(InInputTag);
//检查输入动作是否有效
if (InputAction == nullptr) return;
//绑定函数
BindAction(InputAction, TriggerEvent, ContextObject, Func);
}
3.编译,运行UE编辑器

改成自己的类
5063

被折叠的 条评论
为什么被折叠?



