配置文件路径如下:
按数据格式,把每项数据读取存放到数组,实现如下:
#include "Misc/FileHelper.h"//
USTRUCT(BlueprintType)
struct FAxisInfo
{
GENERATED_USTRUCT_BODY()
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "MotoShakeDll")
int32 axis1;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "MotoShakeDll")
int32 axis2;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "MotoShakeDll")
int32 axis3;
FAxisInfo()
{
axis1 = 0;
axis2 = 0;
axis3 = 0;
}
};
void XXXX::InitDataFile()
{
FString IniFilePath = FPaths::Combine(*FPaths::ProjectPluginsDir(), TEXT("AxisData.ini"));
FString Text;
AxisInfoArray.Empty();//清空之前的数据
if (FFileHelper::LoadFileToString(Text, *IniFilePath))//头文件#include "Misc/FileHelper.h"//
{
TArray
FString LeftS ;
FString RightS;
Text.Trim();//去掉所有空格
FString DataX, DataY, DataZ;
FAxisInfo CurInfo;
if (Text.Split("=", &LeftS, &RightS))//首字符"="
{
while (RightS.Split("=", &LeftS, &RightS) && !RightS.Equals("End"))
{
StrGroupArray.Add(LeftS);//每项都是0#0#0 8#8#8 16#16#16 等格式
}
UE_LOG(LogTemp, Warning, TEXT("InitDataFile: StrGroupArray=%d"), StrGroupArray.Num());
for (int32 i = 0; i < StrGroupArray.Num(); i++)
{
//每项都是0#0#0 8#8#8 16#16#16 等格式 每一项数字读取处理,两次切割每项字符串
if (StrGroupArray[i].Split("#", &LeftS, &RightS))
{
DataX = LeftS;
if(RightS.Split("#", &LeftS, &RightS))
{
DataY = LeftS;
DataZ = RightS;
}
}
//添加到自定义结构体数字
CurInfo.axis1 = FCString::Atoi(*DataX);
CurInfo.axis2 = FCString::Atoi(*DataY);
CurInfo.axis3 = FCString::Atoi(*DataZ);
AxisInfoArray.Add(CurInfo);
}
}
UE_LOG(LogTemp, Warning, TEXT("InitDataFile:AxisInfoArray= %d"), AxisInfoArray.Num());
}
else
{
UE_LOG(LogTemp, Warning, TEXT("InitDataFile: Null "));
}
}