vault backup: 2025-08-29 17:08:18

This commit is contained in:
2025-08-29 17:08:18 +08:00
parent c78cfffdc1
commit 9c6955c975
2 changed files with 67 additions and 5 deletions

View File

@@ -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.
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<UEnhancedInputUserSettings>(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<UEnhancedInputDeveloperSettings>();
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<UEnhancedInputUserSettings>(UGameplayStatics::CreateSaveGameObject(SettingsClass));
}
if (ensure(Settings))
{
Settings->Initialize(LocalPlayer);
Settings->ApplySettings();
}
return Settings;
}
```