.1.创建一个数据表
需要改成UTF-8编码
2.创建一个Actor
.h
UFUNCTION(BlueprintCallable, Category = "MySocket")
TMap<int, FString> GetCsvContents(FString csvPath, int LieNum);
.cpp
#include "Runtime/Core/Public/Serialization/Csv/CsvParser.h"
#include "Runtime/Core/Public/Misc/Paths.h"
TMap<int, FString> AMyActor::GetCsvContents(FString csvPath, int LieNum)
{
FString csvFile = FPaths::ProjectContentDir() + csvPath;
TMap<int, FString> csvContent;
if (FPaths::FileExists(csvFile))
{
FString FileContent;
//Read the csv file
FFileHelper::LoadFileToString(FileContent, *csvFile);
FCsvParser* csvfiles = new FCsvParser(FileContent);
TArray<FString> t_str;
TArray<TArray<const TCHAR*>> content = csvfiles->GetRows();
for (TArray<const TCHAR*>& ite : content)
{
for (const TCHAR*& chr : ite)
{
//aa.Append(chr);
t_str.Add(chr);
}
}
/*for (int i = 0; i < t_str.Num(); i++)
{
UE_LOG(LogTemp, Warning, TEXT("Info= %s"), *t_str[i]);
}*/
int key = 0;
for (int i = 0; i < t_str.Num(); i++)
{
if (i % LieNum == 0)
{
FString rowValue = t_str[i];
for (int j = 1; j < LieNum; j++)
{
rowValue.Append("," + t_str[i+j]);
}
csvContent.Add(key, rowValue);
key++;
}
}
return csvContent;
}
else
return csvContent;
}
datatable
USTRUCT(BlueprintType)
struct FAssetAttribute : public FTableRowBase
{
GENERATED_USTRUCT_BODY()
public:
FAssetAttribute()
: AssetName(LOCTEXT("CylinderBuilderName", "Cylinder"))
, Category("123")
, AssetDataPath(TEXT(StaticMesh'/Game/model/aa.aa'))
, ICONpath(TEXT("AssetToolEdMode.BoxBrush"))
, ToolTip(LOCTEXT("CylinderBuilderName", "Cylinder"))
, Type(AssetType::A_StaticMesh)
, netPath(TEXT("AssetToolEdMode.BoxBrush"))
{
}
public:
UPROPERTY(BlueprintReadWrite, EditAnywhere)
FText AssetName;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
FString Category;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
FString AssetDataPath;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
FString ICONpath;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
FText ToolTip;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
AssetType Type;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
FString netPath;
};
FString tablePath = "DataTable'/MyAsset/DataAsset.DataAsset'";
UDataTable* pDataTable = LoadObject<UDataTable>(NULL, *tablePath);
TArray<FName> RowNames;
FString ContextString;
RowNames = pDataTable->GetRowNames();
for (auto& name : RowNames)
{
FAssetAttribute* pRow = pDataTable->FindRow<FAssetAttribute>(name, ContextString);
if (pRow)
{
//LogDebug("read by row name --- RowName:%s, ID:%d, Name:%s, HP:%d", *(name.ToString()), pRow->ID, *pRow->Name, pRow->HP);
}
}