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);
}
相关推荐
暮志未晚Webgl1 天前
UE5 修改项目名称 类的名称
ue5
Zhichao_971 天前
【UE5.1】Chaos物理系统基础——03 炸开几何体集
ue5
东风吹柳1 天前
UE5.2 AI实时抠像(无需绿幕) + OBS推流直播 全流程
ue5
会思考的猴子1 天前
UE5 04-重新加载当前场景
ue5
Zhichao_971 天前
【UE5.1】Chaos物理系统基础——02 场系统的应用
ue5
Zhichao_971 天前
【UE5.1 角色练习】13-枪械射击——拿出与收起武器
ue5
我的巨剑能轻松搅动潮汐2 天前
UE5 视频播放(自动播放和自动清除MediaTexture)
ue5
会思考的猴子3 天前
UE5 02-给物体一个扭矩力
ue5
会思考的猴子3 天前
UE5 03-物体碰撞检测
ue5
多方通行83 天前
UE5材质之HLSL:深度
开发语言·算法·ue5·图形渲染·材质