diff --git a/03-UnrealEngine/Gameplay/Lyra/UE5 Lyra学习笔记(8)—EnhanceInput(UE5.5).md b/03-UnrealEngine/Gameplay/Lyra/UE5 Lyra学习笔记(8)—EnhanceInput(UE5.5).md index dddc7f0..bb302f6 100644 --- a/03-UnrealEngine/Gameplay/Lyra/UE5 Lyra学习笔记(8)—EnhanceInput(UE5.5).md +++ b/03-UnrealEngine/Gameplay/Lyra/UE5 Lyra学习笔记(8)—EnhanceInput(UE5.5).md @@ -22,6 +22,7 @@ rating: ⭐ - **Input Mapping Contexts**:输入Action的集合。可以为不同GameMode配置不同的 **Input Mapping Contexts**来满足自定义输入的需求。 - **Input Modifiers**: 是一种预处理器,能够修改UE接收的原始输入值,然后再将其发送给输入触发器(Input Trigger)。增强输入插件随附多种输入修饰器,可以执行各种任务,例如更改轴顺序、实现"死区"、将轴输入转换为世界空间。 - **Input Triggers**:用于确定用户输入在经过一系列可选输入修饰器的处理后,是否会激活输入映射上下文中的相应输入动作。 +- **UEnhancedInputUserSettings** (~~PlayerMappableInputConfig~~)**: - 多平台相关:位于Project Settings -> Enhanced Input -> Platform Settings -> Input Data - Mapping Context Redirect:将不同的输入设置用于不同的平台。 - Enhanced Input Platform Data:为你的游戏添加特定于平台的选项。 @@ -114,4 +115,88 @@ ETriggerState UInputTriggerHold::UpdateState_Implementation(const UEnhancedPlaye } ``` +# 按键设置 + # Lyra中的相关实现 + +ULyraHeroComponent中的输入绑定: +```c++ +void ULyraHeroComponent::InitializePlayerInput(UInputComponent* PlayerInputComponent) +{ + check(PlayerInputComponent); + + const APawn* Pawn = GetPawn(); + if (!Pawn) + { + return; + } + + const APlayerController* PC = GetController(); + check(PC); + + const ULyraLocalPlayer* LP = Cast(PC->GetLocalPlayer()); + check(LP); + + UEnhancedInputLocalPlayerSubsystem* Subsystem = LP->GetSubsystem(); + check(Subsystem); + + Subsystem->ClearAllMappings(); + + if (const ULyraPawnExtensionComponent* PawnExtComp = ULyraPawnExtensionComponent::FindPawnExtensionComponent(Pawn)) + { + if (const ULyraPawnData* PawnData = PawnExtComp->GetPawnData()) + { + if (const ULyraInputConfig* InputConfig = PawnData->InputConfig) + { + for (const FInputMappingContextAndPriority& Mapping : DefaultInputMappings) + { + if (UInputMappingContext* IMC = Mapping.InputMapping.Get()) + { + if (Mapping.bRegisterWithSettings) + { + if (UEnhancedInputUserSettings* Settings = Subsystem->GetUserSettings()) + { + Settings->RegisterInputMappingContext(IMC); + } + + FModifyContextOptions Options = {}; + Options.bIgnoreAllPressedKeysUntilRelease = false; + // Actually add the config to the local player + Subsystem->AddMappingContext(IMC, Mapping.Priority, Options); + } + } + } + + // The Lyra Input Component has some additional functions to map Gameplay Tags to an Input Action. + // If you want this functionality but still want to change your input component class, make it a subclass + // of the ULyraInputComponent or modify this component accordingly. + ULyraInputComponent* LyraIC = Cast(PlayerInputComponent); + if (ensureMsgf(LyraIC, TEXT("Unexpected Input Component class! The Gameplay Abilities will not be bound to their inputs. Change the input component to ULyraInputComponent or a subclass of it."))) + { + // Add the key mappings that may have been set by the player + LyraIC->AddInputMappings(InputConfig, Subsystem); + + // This is where we actually bind and input action to a gameplay tag, which means that Gameplay Ability Blueprints will + // be triggered directly by these input actions Triggered events. + TArray BindHandles; + LyraIC->BindAbilityActions(InputConfig, this, &ThisClass::Input_AbilityInputTagPressed, &ThisClass::Input_AbilityInputTagReleased, /*out*/ BindHandles); + + LyraIC->BindNativeAction(InputConfig, LyraGameplayTags::InputTag_Move, ETriggerEvent::Triggered, this, &ThisClass::Input_Move, /*bLogIfNotFound=*/ false); + LyraIC->BindNativeAction(InputConfig, LyraGameplayTags::InputTag_Look_Mouse, ETriggerEvent::Triggered, this, &ThisClass::Input_LookMouse, /*bLogIfNotFound=*/ false); + LyraIC->BindNativeAction(InputConfig, LyraGameplayTags::InputTag_Look_Stick, ETriggerEvent::Triggered, this, &ThisClass::Input_LookStick, /*bLogIfNotFound=*/ false); + LyraIC->BindNativeAction(InputConfig, LyraGameplayTags::InputTag_Crouch, ETriggerEvent::Triggered, this, &ThisClass::Input_Crouch, /*bLogIfNotFound=*/ false); + LyraIC->BindNativeAction(InputConfig, LyraGameplayTags::InputTag_AutoRun, ETriggerEvent::Triggered, this, &ThisClass::Input_AutoRun, /*bLogIfNotFound=*/ false); + } + } + } + } + + if (ensure(!bReadyToBindInputs)) + { + bReadyToBindInputs = true; + } + + UGameFrameworkComponentManager::SendGameFrameworkComponentExtensionEvent(const_cast(PC), NAME_BindInputsNow); + UGameFrameworkComponentManager::SendGameFrameworkComponentExtensionEvent(const_cast(Pawn), NAME_BindInputsNow); +} +``` \ No newline at end of file