UE插件的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
//.h

#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
//.cpp

#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
//.h

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
//.cpp


#if WITH_EDITOR
#include "ISettingsModule.h"
#include "ISettingsSection.h"
#endif // WITH_EDITOR

// #tag 模块启动
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 // WITH_EDITOR
}
// #tag 模块销毁
void FLSAssetsToolModule::ShutdownModule()
{
//清理配置
if (LSConfig)
{
#if WITH_EDITOR
if (ISettingsModule* SettingsModule = FModuleManager::GetModulePtr<ISettingsModule>("Settings"))
{
SettingsModule->UnregisterSettings("Project", "Plugins", "LSAssetsTool");
}
#endif // WITH_EDITOR
}

3 配置读取与写入

1
2
3
4
5
6
7
bool TempBool = FLSAssetsToolModule::GetSettings().bDownLoadCopyAsset;


//写入
FLSAssetsToolModule::GetSettings().bDownLoadCopyAsset = true;
FLSAssetsToolModule::GetSettings().SaveConfig();

  ‍