UE5基于RumtimeFBXImport插件使用C++加载服务器上fbx文件方法

UE5的RumtimeFBXImport插件其实只能加载本机的fbx文件,要加载服务器上的fbx文件的话,需要先将该fbx文件下载到本地,然后再使用RumtimeFBXImport插件加载。

示例文件如下:

cpp 复制代码
#include "Loader/WebLoader.h"
#include "Misc/FileHelper.h"
#include "Misc/Paths.h"
#include "HttpModule.h"
#include "Http.h"
#include "FBXImportManager.h"

AWebLoader::AWebLoader(){}

void AWebLoader::BeginPlay()
{
	Super::BeginPlay();

	if (WebFbxPath.Len() > 0)
	{
		LoadFileFromServer(WebFbxPath);
	}
}

void AWebLoader::LoadFileFromServer(const FString& URL)
{
	TSharedRef<IHttpRequest, ESPMode::ThreadSafe> HttpRequest = FHttpModule::Get().CreateRequest();
	HttpRequest->SetURL(URL);
	HttpRequest->SetVerb("GET");
	HttpRequest->OnProcessRequestComplete().BindUObject(this, &AWebLoader::OnFileDownloadComplete);
	HttpRequest->ProcessRequest();
}

void AWebLoader::OnFileDownloadComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
	if (bWasSuccessful && Response.IsValid() && Response->GetResponseCode() == EHttpResponseCodes::Ok)
	{
		FString ProjectRoot = FPaths::Combine(FPaths::ProjectDir(), LocalRelatedPath);
		FString FileName = FPaths::GetCleanFilename(WebFbxPath);
		FString LocalFullPath = FPaths::Combine(ProjectRoot, FileName);

		if (!FFileHelper::SaveArrayToFile(Response->GetContent(), *LocalFullPath))
		{
			UE_LOG(LogTemp, Error, TEXT("File downloaded failed!"));
			return;
		}
		//
		LoadLocalFBX(LocalFullPath);
	}
	else
	{
		UE_LOG(LogTemp, Error, TEXT("File download failed."));
	}
}

void AWebLoader::LoadLocalFBX(FString LocalFullPath)
{
	if (!FBXImportManager)
	{
		FBXImportManager = GetWorld()->SpawnActor<AFBXImportManager>();
	}

	FBXImportManager->InitializeFBXLoad();
	FBXImportManager->ImportFBXFile(LocalFullPath, FVector::UpVector, BaseMaterial, EFBXCollisionType::MeshCollision);
}
相关推荐
笑鸿的学习笔记9 小时前
虚幻引擎5-Unreal Engine笔记之SET节点的输出引脚获取设置后的最新变量值
笔记·ue5·虚幻
Zhichao_9710 小时前
【UE5.1 C++】VS2022下载安装
ue5
m0_5522008221 小时前
《UE5_C++多人TPS完整教程》学习笔记37 ——《P38 变量复制(Variable Replication)》
c++·游戏·ue5
曹勖之21 小时前
在 UE5 蓝图中配置Actor类型的Asset以作为位置和旋转设置目标
ue5·机器人
曹勖之21 小时前
UE 5 和simulink联合仿真,如果先在UE5这一端结束Play,过一段时间以后**Unreal Engine 5** 中会出现显存不足错误
matlab·ue5·机器人
AgilityBaby21 小时前
UE5 2D角色PaperZD插件动画状态机学习笔记
笔记·学习·ue5
AgilityBaby21 小时前
UE5 创建2D角色帧动画学习笔记
笔记·学习·ue5
ue星空4 天前
UE音频中间件wwise插件
学习·ue5·音视频
AgilityBaby6 天前
UE5打包项目设置Project Settings(打包widows exe安装包)
c++·3d·ue5·游戏引擎·unreal engine
AgilityBaby6 天前
UE5蓝图暴露变量,在游戏运行时修改变量实时变化、看向目标跟随目标Find Look at Rotation、修改玩家自身弹簧臂
笔记·游戏·ue5·游戏引擎·蓝图