From 9c6955c9759354001af119f7cb9791c79c8a203a Mon Sep 17 00:00:00 2001 From: BlueRose <378100977@qq.com> Date: Fri, 29 Aug 2025 17:08:18 +0800 Subject: [PATCH] vault backup: 2025-08-29 17:08:18 --- .../various-complements/histories.json | 2 +- ...5 Lyra学习笔记(8)—EnhanceInput(UE5.5).md | 70 +++++++++++++++++-- 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/.obsidian/plugins/various-complements/histories.json b/.obsidian/plugins/various-complements/histories.json index 9f72dbf..4752cc1 100644 --- a/.obsidian/plugins/various-complements/histories.json +++ b/.obsidian/plugins/various-complements/histories.json @@ -1 +1 @@ -{"配置IA中的UserSetting选项。":{"配置IA中的UserSetting选项。":{"currentFile":{"count":1,"lastUpdated":1756439877872}}}} \ No newline at end of file +{"配置IA中的UserSetting选项。":{"配置IA中的UserSetting选项。":{"currentFile":{"count":1,"lastUpdated":1756439877872}}},"AddInputMappings":{"AddInputMappings":{"currentFile":{"count":1,"lastUpdated":1756453435438}}},"AddMappingContext":{"AddMappingContext":{"currentFile":{"count":1,"lastUpdated":1756453441609}}}} \ No newline at end of file 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 7741a9d..66ad8c6 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 @@ -150,11 +150,11 @@ void ULyraHeroComponent::InitializePlayerInput(UInputComponent* PlayerInputCompo { for (const FInputMappingContextAndPriority& Mapping : DefaultInputMappings) { - //获取UInputMappingContext并进行注册。 if (UInputMappingContext* IMC = Mapping.InputMapping.Get()) { if (Mapping.bRegisterWithSettings) { + //将IMC注册给UEnhancedInputUserSettings if (UEnhancedInputUserSettings* Settings = Subsystem->GetUserSettings()) { Settings->RegisterInputMappingContext(IMC); @@ -162,7 +162,8 @@ void ULyraHeroComponent::InitializePlayerInput(UInputComponent* PlayerInputCompo FModifyContextOptions Options = {}; Options.bIgnoreAllPressedKeysUntilRelease = false; - // Actually add the config to the local player + // Actually add the config to the local player + //实际添加IMC给LocalPlayer Subsystem->AddMappingContext(IMC, Mapping.Priority, Options); } } @@ -211,7 +212,68 @@ void ULyraHeroComponent::InitializePlayerInput(UInputComponent* PlayerInputCompo ## 步骤 1. 假设已经配置好了IMC以及IA。 - 1. 在Controller中注册IMC与IA? + 1. 在Controller/Character中注册IMC与IA? + 2. AddMappingContext()的选项OptionsNotifyUserSettings设置为true,会调用UEnhancedInputUserSettings::RegisterInputMappingContext() 2. ProjectSettings - Engine - EnhancedInput - UserSettings中的Enable User Settings 3. 配置IA中的UserSetting选项。 - 1. \ No newline at end of file + 1. 1 + +## 加载按键设置逻辑 +默认自动加载,载入逻辑位于UEnhancedInputUserSettings::LoadOrCreateSettings(),调用顺序是: +UEnhancedInputLocalPlayerSubsystem::Initialize +-> +UEnhancedInputLocalPlayerSubsystem::InitalizeUserSettings() +-> +UEnhancedInputUserSettings::LoadOrCreateSettings() + +```c++ +UEnhancedInputUserSettings* UEnhancedInputUserSettings::LoadOrCreateSettings(ULocalPlayer* LocalPlayer) +{ + UEnhancedInputUserSettings* Settings = nullptr; + + if (!LocalPlayer) + { + UE_LOG(LogEnhancedInput, Log, TEXT("Unable to determine an owning Local Player for the given Enhanced Player Input object")); + return nullptr; + } + + // If the save game exists, load it. + if (UGameplayStatics::DoesSaveGameExist(UE::EnhancedInput::SETTINGS_SLOT_NAME, LocalPlayer->GetLocalPlayerIndex())) + { + USaveGame* Slot = UGameplayStatics::LoadGameFromSlot(UE::EnhancedInput::SETTINGS_SLOT_NAME, LocalPlayer->GetLocalPlayerIndex()); + Settings = Cast(Slot); + } + + // If there is no settings save game object, then we can create on + // based on the class type set in the developer settings + const UEnhancedInputDeveloperSettings* DevSettings = GetDefault(); + UClass* SettingsClass = DevSettings->UserSettingsClass ? DevSettings->UserSettingsClass.Get() : UEnhancedInputUserSettings::StaticClass(); + + // This property is marked as "NoClear", so this should be impossible. + if (!ensureMsgf(SettingsClass, TEXT("Invalid Enhanced Input User settings class!"))) + { + return nullptr; + } + + UE_CLOG((Settings && (Settings->GetClass() != SettingsClass)), + LogEnhancedInput, + Log, + TEXT("[%hs] Enhanced Input User Settings class has been changed from '%s' to '%s'. A new settings save object will be created (your saved settings will be reset)"), + __func__, *GetNameSafe(Settings->GetClass()), *GetNameSafe(SettingsClass)); + + // If the settings are null (they dont exist yet) or the class has changed, we need to create a new object. + // The class can change if you modify it in the editor to be something else + if (Settings == nullptr || (Settings->GetClass() != SettingsClass)) + { + Settings = Cast(UGameplayStatics::CreateSaveGameObject(SettingsClass)); + } + + if (ensure(Settings)) + { + Settings->Initialize(LocalPlayer); + Settings->ApplySettings(); + } + + return Settings; +} +``` \ No newline at end of file