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

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.xdnf.cn/news/18583.html

相关文章:

  • 恶劣天气下漏检率↓79%!陌讯多模态时序融合算法在道路事故识别的实战优化
  • 淘宝API实战应用:数据驱动商品信息实时监控与增长策略
  • DBeaver连接SQL Server时添加驱动后仍提示找不到驱动的解决方法
  • 51c自动驾驶~合集18
  • 学习记录(二十一)-Overleaf中图片文字间隔太大怎么办
  • java学习 + 一个向前端传流顺序不一致的一个解决思路
  • ubuntu中的nginx.conf和windows中的nginx.conf内容对比
  • 从栈到堆:深入理解C语言静态与动态链表的创建与管理
  • Flutter性能优化完全指南:构建流畅应用的实用策略
  • 如何安全解密受限制的PDF文件
  • [二维前缀和]1277. 统计全为 1 的正方形子矩阵
  • 【线性代数】常见矩阵类型
  • RandAR训练自己的数据集
  • ARINC 825板卡的应用
  • C++---双指针
  • Hyperledger Fabric官方中文教程-改进笔记(十五)-从通道中删除组织
  • Adobe CS6所有系列绿色免安装版,Photoshop 6 Adobe Illustrator CS6 等绿色版
  • 283. 移动零
  • 阿里云拉取dockers镜像
  • Wireshark USRP联合波形捕获(下)
  • 【Linux】Java线上问题,一分钟日志定位
  • 2024年CSP-S认证 CCF信息学奥赛C++ 中小学提高组 第一轮真题讲解 完善程序题解析
  • 面试题及解答:掌握Linux下常用性能分析工具
  • 使用Python实现DLT645-2007智能电表协议
  • 基于php的萌宠社区网站的设计与实现、基于php的宠物社区论坛的设计与实现
  • 【QT入门到晋级】进程间通信(IPC)-共享内存
  • 十六进制与内存地址,数值的差异为1,表示差1个字节,而不是数值差异2^8才表示差一个字节
  • 03-鸿蒙架构与编程模型
  • 《Linux 网络编程二:UDP 与 TCP 的差异、应用及问题应对》
  • Meta AI 剧变:汪滔挥刀重组,Llama 开源路线告急,超级智能梦碎还是重生?