UEUE插件的Config管理
李孟岩Config管理
1 Config类
  继承自UObject,
  UCLASS关键字,类和属性都要带config。
  config=就是你的配置的名字。默认会存在项目Saved/Config/WindowEditor下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
   | 
  #pragma once
  #include "CoreMinimal.h" #include "UObject/NoExportTypes.h"
  #include "ULSAssetToolConfig.generated.h"
  UCLASS(config=LSAssetsTool, Defaultconfig) class LSASSETSTOOL_API ULSAssetToolConfig : public UObject {     GENERATED_BODY()
  public:
      ULSAssetToolConfig (const FObjectInitializer& obj);
      UPROPERTY(Config, EditAnywhere, Category = "LSAssetsToolSettings")     bool bDownLoadCopyAsset; };
 
 
  | 
 
1 2 3 4 5 6 7 8
   | 
  #include "Config/ULSAssetToolConfig.h"
  ULSAssetToolConfig::ULSAssetToolConfig(const FObjectInitializer& obj) {     bDownLoadCopyAsset = false; }
 
  | 
 
2 Modulue注册
  模块持有配置类的指针
  注册时初始化配置
  注销时销毁配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
   | 
  class FLSAssetsToolModule : public IModuleInterface { public: 	virtual void StartupModule() override;
  	virtual void ShutdownModule() override;
  	 	static inline FLSAssetsToolModule& Get() 	{ 		return FModuleManager::LoadModuleChecked<FLSAssetsToolModule>("LSAssetsTool"); 	} private: 	 	ULSAssetToolConfig* LSConfig = nullptr; #pragma region 配置相关 public: 	 	static ULSAssetToolConfig& GetSettings() 	{ 		FLSAssetsToolModule& Module = IsInGameThread() ? Get() : FModuleManager::LoadModuleChecked<FLSAssetsToolModule>("LSAssetsTool"); 		ULSAssetToolConfig* Settings = Module.GetSettingsInstance(); 		check(Settings); 		return *Settings; 	}
  protected: 	 	virtual ULSAssetToolConfig* GetSettingsInstance()  { return LSConfig; }
  #pragma endregion };
 
  | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
   | 
 
  #if WITH_EDITOR #include "ISettingsModule.h" #include "ISettingsSection.h" #endif 
 
  void FLSAssetsToolModule::StartupModule() { 	 	LSConfig = NewObject<ULSAssetToolConfig>(GetTransientPackage(), ULSAssetToolConfig::StaticClass()); 	LSConfig->AddToRoot();
  #if WITH_EDITOR 	if (ISettingsModule* SettingsModule = FModuleManager::GetModulePtr<ISettingsModule>("Settings")) 	{ 		SettingsModule->RegisterSettings("Project", "Plugins", "LSAssetsTool", 			LOCTEXT("RuntimeSettingsName", "LSAssetsTool"), 			LOCTEXT("RuntimeSettingsDescription", "Configure the LSAssetsTool plugin"), 			LSConfig); 	} #endif  }
  void FLSAssetsToolModule::ShutdownModule() { 	 	if (LSConfig) 	{ #if WITH_EDITOR 		if (ISettingsModule* SettingsModule = FModuleManager::GetModulePtr<ISettingsModule>("Settings")) 		{ 			SettingsModule->UnregisterSettings("Project", "Plugins", "LSAssetsTool"); 		} #endif  }
 
  | 
 
3 配置读取与写入
1 2 3 4 5 6 7
   | bool TempBool = FLSAssetsToolModule::GetSettings().bDownLoadCopyAsset;
 
 
  FLSAssetsToolModule::GetSettings().bDownLoadCopyAsset = true;    FLSAssetsToolModule::GetSettings().SaveConfig();
 
   |