UE4动态加载数据表

 

.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);
		}
	}

 

 

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页