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 小时前
UE5中如何解决角色网格体“掉下去”的问题
运维·服务器·windows·容器·ue5
AI视觉网奇1 天前
ue5 插件 WebSocket
c++·ue5
AI视觉网奇1 天前
ue 自己制作插件 c++
c++·ue5
AI视觉网奇1 天前
ue5.7 配置 audio2face
笔记·ue5
zhangzhangkeji1 天前
UE5 C++(UObject 的实例化 19-3):类 UWorld,模板函数 NewObject<>(...),
ue5
zhangzhangkeji2 天前
UE5 C++(14-1):UPROPERTY 宏、属性说明符和元数据说明符, visible,edit,Blueprint读写,Category 存储目录
ue5
Zhichao_972 天前
【UE5.3 C++】ARPG游戏 05-准备角色攻击的武器和动画
游戏·ue5
zhangzhangkeji2 天前
UE5 C++(15-3):UFUNCTION 的 meta 元数据, DisplayName ,同 UPROPERTY 的 meta
ue5
夜色。2 天前
UE5 Error LNK2019 编译异常修复备忘
ue5
陈友松3 天前
UE5 表格文件动态导入导出插件
ue5