UE中Csv配置器

该博客介绍了如何在Unreal Engine中通过CSV文件配置数据,创建蓝图struct并绑定到UI,以便在游戏中动态显示数据。具体步骤包括创建蓝图、加载DataTable、初始化Controller以及更新UI。示例代码展示了从CSV加载数据并在游戏中显示的方法。
摘要由CSDN通过智能技术生成

蓝图配置器

  1. 准备csv格式配置文件

  1. 创建蓝图struct,命名为FSpeakerInfoLine,导入表配置,得到Speaker的DataTablet

        

  1. 使用配置数据

    1. 创建UI并把其中的文本bind为SpeakerChoice的变量

  1. 在GameMode中显示UI和获得SpeakerController的引用,通过对象引用进行初始化

  1. InitSpeakerController:SpeakerController进行初始化,通过Speacker类型的array的Speackers设置Speaker中的SpeakerController

调用UpdataUI,更新UI

  1. UpdateUI:通过活动上述datatablet的speaker的数据,来显示在最后的UI上

4. c++实现

CsvPaster.h继承Actor,然后派生出蓝图类,拖到场景中就调用了

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Engine/DataTable.h"
#include "CsvPaster.generated.h"
//USTRUCT(BlueprintType)
struct FHudTextCsvRow :public FTableRowBase
{
	//GENERATED_USTRUCT_BODY()

	UPROPERTY(BlueprintReadOnly, Category = "CsvData")
		FString ShortDescription;

	UPROPERTY(BlueprintReadOnly, Category = "CsvData")
		FString LongDescription;

	UPROPERTY(BlueprintReadOnly, Category = "CsvData")
		int Price;
};

UCLASS()
class CONFIGURATOR_API ACsvPaster : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ACsvPaster();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
public:
	UDataTable* HudTextDataTable = nullptr;

};

CsvPaster.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "CsvPaster.h"
#include "Serialization/Csv/CsvParser.h"
//#include"CsvDataCenter.h"
// Sets default values
ACsvPaster::ACsvPaster()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	HudTextDataTable = LoadObject<UDataTable>(NULL, UTF8_TO_TCHAR("DataTable'/Game/Data/Cabinett.Cabinett'"));
	check(HudTextDataTable);
}

// Called when the game starts or when spawned
void ACsvPaster::BeginPlay()
{
	Super::BeginPlay();
	FHudTextCsvRow* dataRow = HudTextDataTable->FindRow<FHudTextCsvRow>(TEXT("2"), TEXT(""));
	if (dataRow)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, dataRow->ShortDescription);
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, dataRow->LongDescription);
		//GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, dataRow->Price);
	}
}

// Called every frame
void ACsvPaster::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

导入的数据必须是通过这个HudTextCsvRow结构

 

 字段标签必须与struct中的属性相同命名

参考文献:
https://docs.unrealengine.com/4.27/zh-CN/InteractiveExperiences/DataDriven/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值