蓝图配置器
-
准备csv格式配置文件
-
创建蓝图struct,命名为FSpeakerInfoLine,导入表配置,得到Speaker的DataTablet
????????
-
使用配置数据
- 创建UI并把其中的文本bind为SpeakerChoice的变量
- 在GameMode中显示UI和获得SpeakerController的引用,通过对象引用进行初始化
- InitSpeakerController:SpeakerController进行初始化,通过Speacker类型的array的Speackers设置Speaker中的SpeakerController
调用UpdataUI,更新UI
- 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/
|