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

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值