关于UE4创建自定义配置文件/配置项的记录

声明

本文不做过多深入的解释,以使用为目的。

使用方法

  1. 声明定义一个继承自UObject的配置类,类中需声明宏UCLASS(config = ConfigFileName, configdonotcheckdefaults);
  2. 声明定义配置参数,需带上宏UPROPERTY(Config, EditAnywhere, Category = Custom),如果需要设置默认值,可在类的构造函数中对变量进行赋值;
  3. 如果需要在editor中可见自定以的配置项目,可在模块的StartupModule和ShutdownModule对自定义配置进行注册与反注册;若需在主模块中进行上述动作,只需在ProjectName.cpp中实现,可参考如下代码:
// ProjectName.cpp
// Fill out your copyright notice in the Description page of Project Settings.

#include "CustomSettings.h"
#include "Modules/ModuleManager.h"

// Settings
#include "MySettings.h"
#include "Developer/Settings/Public/ISettingsModule.h"
#include "Developer/Settings/Public/ISettingsSection.h"
#include "Developer/Settings/Public/ISettingsContainer.h"

#define LOCTEXT_NAMESPACE "CustomSettings"

class FCustomSettingsModule : public FDefaultGameModuleImpl
{
	virtual void StartupModule() override
	{
		RegisterSettings();
	}

	virtual void ShutdownModule() override
	{
		if (UObjectInitialized())
		{
			UnregisterSettings();
		}
	}

	virtual bool SupportsDynamicReloading() override
	{
		return true;
	}

private:

	// Callback for when the settings were saved.
	bool HandleSettingsSaved()
	{
		UMySettings* Settings = GetMutableDefault<UMySettings>();
		bool ResaveSettings = false;

		// You can put any validation code in here and resave the settings in case an invalid
		// value has been entered

		if (ResaveSettings)
		{
			Settings->SaveConfig();
		}

		return true;
	}

	void RegisterSettings()
	{
		// Registering some settings is just a matter of exposing the default UObject of
		// your desired class, feel free to add here all those settings you want to expose
		// to your LDs or artists.

		if (ISettingsModule* SettingsModule = FModuleManager::GetModulePtr<ISettingsModule>("Settings"))
		{
			// Create the new category
			ISettingsContainerPtr SettingsContainer = SettingsModule->GetContainer("Project");

			SettingsContainer->DescribeCategory("CustomSettings",
				LOCTEXT("RuntimeWDCategoryName", "CustomSettings"),
				LOCTEXT("RuntimeWDCategoryDescription", "Game configuration for the CustomSettings game module"));

			// Register the settings
			ISettingsSectionPtr SettingsSection = SettingsModule->RegisterSettings("Project", "CustomSettings", "General",
				LOCTEXT("RuntimeGeneralSettingsName", "General"),
				LOCTEXT("RuntimeGeneralSettingsDescription", "Base configuration for our game module"),
				GetMutableDefault<UMySettings>()
			);

			// Register the save handler to your settings, you might want to use it to
			// validate those or just act to settings changes.
			if (SettingsSection.IsValid())
			{
				SettingsSection->OnModified().BindRaw(this, &FCustomSettingsModule::HandleSettingsSaved);
			}
		}
	}

	void UnregisterSettings()
	{
		// Ensure to unregister all of your registered settings here, hot-reload would
		// otherwise yield unexpected results.

		if (ISettingsModule* SettingsModule = FModuleManager::GetModulePtr<ISettingsModule>("Settings"))
		{
			SettingsModule->UnregisterSettings("Project", "CustomSettings", "General");
		}
	}
};

IMPLEMENT_PRIMARY_GAME_MODULE(FCustomSettingsModule, CustomSettings, "CustomSettings" );

#undef LOCTEXT_NAMESPACE
  1. 经过上述三个步骤后,即可在editor下看到配置项目,并可修改,如下:在这里插入图片描述
    并会自动生成自定义的配置文件。

补充说明

  1. 上述的第一点中,UCLASS(config = ConfigFileName, configdonotcheckdefaults),ConfigFileName可以是UE4自带的配置文件,比如Game、Engine等,也可以是完全自定以的文件名;configdonotcheckdefaults意思是不做默认配置的检查,不会生成DefaultCustomSettings.ini, 打包出来运行后会在Save/Config中生成CustomSettings.ini,可对该配置文件中的配置项进行修改,重启游戏后生效,若没有相关文件或配置项,可自己添加;defaultconfig则是生成DefaultCustomSettings.ini,打完包后不可修改。
  2. 还有另外一种方式是
    在这里插入图片描述
  • 不用注册,效果待验证。。

注意

  • 至于能不能实现游戏运行过程中修改配置项,读取到实时修改后的值,待研究?

以上的自我总结可能存在错误的地方,望指正!

参考资源

Unreal wiki上的文档
https://wiki.unrealengine.com/CustomSettings

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值