我们先正常的创建UE C++项目,然后在UE编辑器中创建继承ACharacter的角色类(ACharacter_Y);
在void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
函数中绑定输入
这里是官方文档教的默认绑定方式
轴映射
PlayerInputComponent->BindAxis("MoveForward", this, &ACharacter_Y::MoveForward);
PlayerInputComponent->BindAxis(映射名,一般填当前对象,按键后调用的函数)
操作映射
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter_Y::StartJump);
PlayerInputComponent->BindAction(映射名,(输入事件)按钮按下/抬起,一般填当前对象,
输入事件发射后调用的函数)
在C++代码中写上这些后还需要在UE编辑器里 编辑》项目设置 》输入》绑定映射
以上都是默认的 你想想看每次修改添加都需要去编辑器里添加 头都大
所以下面有我目前知道的其它方法
一:FInputKeyBinding UInputComponent::KeyBindings FInputChord
要先填FInputKeyBinding 类
如上图 EKeys::R 这个就是按键R ,IE_Pressed这个是按键按下
.KeyDelegate.BindDelegate(this, &ACharacter_Y::Teleportation);绑定函数
.KeyDelegate.BindDelegate(一般填当前对象,输入事件发射后调用的函数);
填写完FInputKeyBinding 类只是前置操作
还要需要添加进UInputComponent::KeyBindings
UInputComponent::KeyBindings.Add(FInputKeyBinding 对象)
现在就可以按R键使用函数了
二:UInputComponent::BindKey
这个和上诉的差不多
三:FInputActionKeyMapping
UGameplayStatics::GetPlayerController(this, 0)/*获取玩家控制器*/->PlayerInput->AddActionMapping
UInputComponent::BindAction 这个是比较重要的
这个是和手动编辑器里的映射按键方法一样 不过变成代码写入了
注意构造函数 因为一定会想Shift Ctrl Alt Cmd 键呢 其实就在构造函数中 填true就可以了
FInputActionKeyMapping(const FName InActionName = NAME_None, const FKey InKey = EKeys::Invalid, const bool bInShift = false, const bool bInCtrl = false, const bool bInAlt = false, const bool bInCmd = false)
填写完也要添加
UGameplayStatics::GetPlayerController(this, 0)/*获取玩家控制器*/->PlayerInput->AddActionMapping(ActionKeyMap);
备注:以上都是操作映射的操作
四:轴映射 FInputAxisKeyMapping
UGameplayStatics::GetPlayerController(this, 0)/*获取玩家控制器*/->PlayerInput->AddAxisMapping
UInputComponent::BindAxis
是的这个C++就是轴映射 它三方法其实类似 都是绑定到一个名称