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);
}
相关推荐
暮志未晚Webgl3 小时前
UE使用内置功能查看性能
ue5
AI视觉网奇4 小时前
Epic linux 打包。
笔记·学习·ue5
伪善者7 小时前
UE5 打包插件
ue5·打包
AI视觉网奇7 小时前
ue5 开发 web socket server 实战2026
c++·学习·ue5
zhangzhangkeji21 小时前
UE5 C++(39):创建 TimeHandle 定时器
ue5
zhangzhangkeji21 小时前
UE5 C++(38):创建 Interface接口
ue5
zhangzhangkeji1 天前
UE5 C++(40):创建 3DWidget 并渲染到屏幕上,涉及类 UUserWidget 与 UWidgetCompopent
ue5
zhangzhangkeji1 天前
UE5 C++(41):创建 ApplyDamage 并接受伤害 TakeDamage
ue5
陈友松1 天前
UE5运行时操作撤销系统插件
ue5·ue4·运行时回退撤销
北冥没有鱼啊1 天前
UE5 离谱问题,角色动画不播放
游戏·ue5·ue4·游戏开发·虚幻