虚幻引擎中提供了与平台无关的文件读写与访问接口,通过调用,可以完成一些文件的读写。比如文本文件,配置文件,json文件,xml文件等。
完成文件读写,首先需要获取文件路径等相关信息。对调用这些操作,我们需要包含头文件PlatformFilemanager.h和FileHelper.h。对于json文件读写,我们需要在.build.cs文件中添加Json和JsonUtilities两个模块;对于xml文件读取,我们需要在.build.cs文件中添加XmlParser模块。引用相关模块后,在具体实现需要引入对应的头文件。我们可以创建一个继承自UBlueprintFunctionLibrary的C++类,这样我们就可以在蓝图中调用我们在C++中声明BlueprintCallable创建的函数。
需要引入的头文件:
#include "PlatformFilemanager.h"
#include "FileHelper.h"
#include "Json.h"
#include "JsonObject.h"
#include "JsonSerializer.h"
#include"Runtime/XmlParser/Public/XmlFile.h"
#include"Runtime/XmlParser/Public/XmlNode.h"
函数方法声明(FunctionLIbrary.h):
//文本文件读写
UFUNCTION(BlueprintCallable, Category = "Test")
static void WriteText(FString path,FString data);
UFUNCTION(BlueprintCallable, Category = "Test")
static FString ReadText(FString path);
//配置文件读写
UFUNCTION(BlueprintCallable, Category = "Test")
static void WriteConfig(FString section, FString name, FString value, FString path);
UFUNCTION(BlueprintCallable, Category = "Test")
static FString ReadConfig(FString section, FString name, FString path);
//UE4读取json文件
UFUNCTION(BlueprintCallable, Category = "Test")
static void NonSeriWriteJson();
UFUNCTION(BlueprintCallable, Category = "Test")
static void SeriWriteJson();
UFUNCTION(BlueprintCallable, Category = "Test")
static TArray<FName> ReadJson();
//UE4读取xml文件
UFUNCTION(BlueprintCallable, Category = "Test")
static void CreateXmlFile();
UFUNCTION(BlueprintCallable, Category = "Test")
static void ReadXmlFile(const FString &XmlPath);

本文介绍了如何在虚幻引擎中实现多种类型的文件读写,包括文本文件、配置文件、JSON文件和XML文件等。文章详细阐述了所需包含的头文件、如何在C++类中实现读写功能并暴露给蓝图使用的具体步骤。
最低0.47元/天 解锁文章





