UE5.5 C++ 增强输入 快速上手
一.5.1以后直接模块自带了,所以直接包含头文件。我习惯先前置声明再包。
//增强输入三件套
#include "InputActionValue.h" //输入映射Value值的头文件
#include "EnhancedInputComponent.h" //增强映射的头文件
#include "EnhancedInputSubsystems.h" //增强子系统的头文件
class USpringArmComponent;
class UCameraComponent;
class UInputMappingContext;
class UInputAction;
struct FInputActionValue;
让后在引擎里,把绑定的Action 和 键位上下文 .Action里设置ValueType,就是指定你的键输入,传值到函数是单float,还是二维输入,或者三维。
MappingContext 里设置这个,调用这个Action的键,这里是鼠标中建
二.这里直接做 镜头 拉近拉远。
首先声明,上面两个资源。
class USpringArmComponent;
class UCameraComponent;
class UInputMappingContext;
class UInputAction;
struct FInputActionValue;UCLASS()
class GIS_API ATestPawn : public APawn
{GENERATED_BODY()public:// Sets default values for this pawn's propertiesATestPawn();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public: // Called every framevirtual void Tick(float DeltaTime) override;// Called to bind functionality to inputvirtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = MySceneComponent)USpringArmComponent* SpringArm;UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = MySceneComponent)UCameraComponent* Camera;UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "EnhancedInput", meta = (AllowPrivateAccess = "true"))TObjectPtr<UInputMappingContext> InputMappingContext;UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "EnhancedInput|Action", meta = (AllowPrivateAccess = "true"))TObjectPtr<UInputAction> IA_ZoomTest;void Zoom(const FInputActionValue& inputValue);
};
然后,绑定Zoom函数到Action上。参数要这么声明,值才能传对。
接着在开始绑定,把老版本InputComponent,转换增强输入子系统的 ,和它的组件EnhancedInputComponent。
void ATestPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{Super::SetupPlayerInputComponent(PlayerInputComponent); //// 先添加IMC还是绑定IA不重要,因为IMC代表的只是一个从按键到Action的关系,下面的Action是怎么到一个回调if (APlayerController* PC = CastChecked<APlayerController>(GetController())){if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PC->GetLocalPlayer())){Subsystem->AddMappingContext(InputMappingContext, 0);}}if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent)){if (IA_ZoomTest){EnhancedInputComponent->BindAction(IA_ZoomTest, ETriggerEvent::Triggered, this, &ATestPawn::Zoom);}}
}
三.测试
完整CPP 如下
// Fill out your copyright notice in the Description page of Project Settings.#include "TestPawn.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
//增强输入三件套
#include "InputActionValue.h" //输入映射Value值的头文件
#include "EnhancedInputComponent.h" //增强映射的头文件
#include "EnhancedInputSubsystems.h" //增强子系统的头文件// Sets default values
ATestPawn::ATestPawn()
{// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.PrimaryActorTick.bCanEverTick = true;RootComponent = CreateDefaultSubobject<USceneComponent>("Root");SpringArm = CreateDefaultSubobject<USpringArmComponent>("SpringArm");Camera = CreateDefaultSubobject<UCameraComponent>("Camera");SpringArm->TargetArmLength = 300;SpringArm->bDoCollisionTest = false;SpringArm->SetupAttachment(RootComponent);Camera->SetupAttachment(SpringArm);}// Called when the game starts or when spawned
void ATestPawn::BeginPlay()
{Super::BeginPlay();}// Called every frame
void ATestPawn::Tick(float DeltaTime)
{Super::Tick(DeltaTime);}// Called to bind functionality to input
void ATestPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{Super::SetupPlayerInputComponent(PlayerInputComponent); //// 先添加IMC还是绑定IA不重要,因为IMC代表的只是一个从按键到Action的关系,下面的Action是怎么到一个回调if (APlayerController* PC = CastChecked<APlayerController>(GetController())){if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PC->GetLocalPlayer())){Subsystem->AddMappingContext(InputMappingContext, 0);}}if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent)){if (IA_ZoomTest){EnhancedInputComponent->BindAction(IA_ZoomTest, ETriggerEvent::Triggered, this, &ATestPawn::Zoom);}}
}void ATestPawn::Zoom(const FInputActionValue& inputValue)
{float ZommValue = inputValue.Get<float>();SpringArm->TargetArmLength += ZommValue * 100;
}
用蓝图继承这个C++,并在编辑器里初始化,IMC 和 IA。即可
再放场景里,就可以镜头拉经,拉远了