UE4引擎的夸平臺(tái)讀寫(xiě)Excel的組件:DataTable
UE4自身提供的一種讀寫(xiě)文件的組件:UDataTable。好處就是不用自己寫(xiě)fopen、fclose等 c++ stl API相關(guān)的邏輯,避開(kāi)不同平臺(tái)的差異;壞處就是你想要的功能DataTable沒(méi)有實(shí)現(xiàn),那么還得用fopen自己發(fā)揮。
讀寫(xiě)excel需要導(dǎo)出為CSV文件,目前還不支持*.XLS格式。
下面官方文檔中關(guān)于用C++代碼定義行結(jié)構(gòu)的用法沒(méi)有具體說(shuō)明:
如果是藍(lán)圖創(chuàng)建DataTable,那么行結(jié)構(gòu)Struct也可以用UE4提供的Struct組件,創(chuàng)建方式是:add new -》 Blueprints -》 Structure,然后再這個(gè)Structure中設(shè)置行結(jié)構(gòu)。
如果是用C++代碼創(chuàng)建DataTable,直接new C++ class,選擇繼承DataTable。另外FTableRowBase可以直接定義在自定義DataTable的頭文件中,例如:
- #pragma once
- #include "Engine/DataTable.h"
- #include "CharactersDT.generated.h"
- USTRUCT(BlueprintType)
- struct FLevelUpData : public FTableRowBase
- {
- GENERATED_USTRUCT_BODY()
- public:
- FLevelUpData()
- : XPtoLvl(0)
- , AdditionalHP(0)
- {}
- /** The 'Name' column is the same as the XP Level */
- /** XP to get to the given level from the previous level */
- UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = LevelUp)
- int32 XPtoLvl;
- /** Extra HitPoints gained at this level */
- UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = LevelUp)
- int32 AdditionalHP;
- /** Icon to use for Achivement */
- UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = LevelUp)
- TAssetPtr<UTexture> AchievementIcon;
- };
Using excel to store gameplay data - DataTables
https://wiki.unrealengine.com/Using_excel_to_store_gameplay_data_-_DataTables
Data Driven Gameplay Elements
https://docs.unrealengine.com/latest/INT/Gameplay/DataDriven/index.html
Driving Gameplay with Data from Excel
https://forums.unrealengine.com/showthread.php?12572-Driving-Gameplay-with-Data-from-Excel
用藍(lán)圖操作DataTable的方法:
Unreal Engine, Datatables for Blueprints (build & Use)
https://www.youtube.com/watch?v=a8jMl69alrg
Excel to Unreal
https://www.youtube.com/watch?v=WLv67ddnzN0
如何用C++代碼動(dòng)態(tài)加載*.CSV
如果你的表格很少的話可以使用這個(gè)自帶的DataTable,如果表格很多且會(huì)頻繁改動(dòng),那么每次改動(dòng)后都要手動(dòng)在UE編輯器中一個(gè)一個(gè)操作,所以,建議用C++動(dòng)態(tài)加載*.csv:
- FString csvFile = FPaths::GameContentDir() + "Downloads\\DownloadedFile.csv";
- if (FPaths::FileExists(csvFile ))
- {
- FString FileContent;
- //Read the csv file
- FFileHelper::LoadFileToString(FileContent, *csvFile );
- TArray<FString> problems = YourDataTable->CreateTableFromCSVString(FileContent);
- if (problems.Num() > 0)
- {
- for (int32 ProbIdx = 0; ProbIdx < problems.Num(); ProbIdx++)
- {
- //Log the errors
- }
- }
- else
- {
- //Updated Successfully
- }
- }
參考自:
https://answers.unrealengine.com/questions/156354/how-to-load-the-csv-datatable-dynamically.html
-
分享到:
您需要登錄后才可以發(fā)帖 登錄 | 立即注冊(cè)
- 用戶名:
- 密 碼:
- 驗(yàn)證碼: 看不清? 點(diǎn)擊更換
- 忘記密碼?
全部評(píng)論:0條