当前位置: 首页 > news >正文

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。即可

再放场景里,就可以镜头拉经,拉远了

http://www.dtcms.com/a/348609.html

相关文章:

  • nginx部署goaccess监控
  • JdbcTemplate和MyBatis的区别
  • 《支付回调状态异常的溯源与架构级修复》
  • 学习制作记录(选项UI以及存档系统)8.24
  • KVM虚拟化
  • Vue3 setup代替了vue2的哪些功能
  • 分布式事务的两种解决方案
  • MYSQL(DDL)
  • 前端 vs 后端请求:核心差异与实战对比
  • Qt——网络通信(UDP/TCP/HTTP)
  • 【Unity开发】Unity核心学习(二)
  • PAT 1081 Rational Sum
  • 【机器学习】8 Logistic regression
  • Power BI切片器自定义顺序
  • 智能油脂润滑系统:给设备一份 “私人定制” 的保养方案
  • Linux 学习笔记 - 集群管理篇
  • 【大模型LLM学习】Data Agent学习笔记
  • C++算法学习专题:二分查找
  • Kubernetes部署Prometheus+Grafana 监控系统NFS存储方案
  • Socket some functions
  • 让机器人“想象”未来?VLN导航迎来“理解力”新升级
  • 每日算法刷题Day64:8.24:leetcode 堆6道题,用时2h30min
  • 解密 Spring Boot 自动配置:原理、流程与核心组件协同
  • 人形机器人——电子皮肤技术路线:压电式电子皮肤及一种超越现有电子皮肤NeuroDerm的设计
  • 深度学习:CUDA、PyTorch下载安装
  • Leetcode 3659. Partition Array Into K-Distinct Groups
  • sqlite创建数据库,创建表,插入数据,查询数据的C++ demo
  • 商密保护迷思:经营秘密到底需不需要鉴定?
  • 对称二叉树
  • 机械学习综合练习项目