在Unreal Engine中,你可以使用HTTP模块来获取图片。以下是一个简单的示例代码,展示如何通过HTTP请求获取图像并将其加载到UE的材质中:
#include "HttpModule.h"
#include "IImageWrapperModule.h"
#include "IImageWrapper.h"
void AYourActor::GetImageFromURL(FString URL)
{
TSharedRef<IHttpRequest, ESPMode::ThreadSafe> HttpRequest = FHttpModule::Get().CreateRequest();
HttpRequest->SetVerb("GET");
HttpRequest->SetURL(URL);
HttpRequest->OnProcessRequestComplete().BindUObject(this, &AYourActor::OnImageReceived);
if (HttpRequest->ProcessRequest())
{
UE_LOG(LogTemp, Warning, TEXT("Request sent!"));
}
}
void AYourActor::OnImageReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
if (bWasSuccessful && Response.IsValid())
{
TArray<uint8> ImageData = Response->GetContent();
IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
EImageFormat ImageFormat = ImageWrapperModule.DetectImageFormat(ImageData.GetData(), ImageData.Num());
if (ImageFormat != EImageFormat::Invalid)
{
TSharedPtr<IImageWrapper> ImageWrapper = ImageWrapperModule.CreateImageWrapper(ImageFormat);
if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(ImageData.GetData(), ImageData.Num()))
{
const TArray<uint8>* RawImageData = nullptr;
if (ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, RawImageData))
{
FString TextureBasePath = FPaths::ProjectContentDir() + "/Textures/";
FString TexturePath = TextureBasePath + "YourTexture.png";
FFileHelper::SaveArrayToFile(*RawImageData, *TexturePath);
UTexture2D* LoadedTexture = LoadTextureFromDisk(TexturePath);
if (LoadedTexture)
{
// 使用LoadedTexture来设置材质等
UE_LOG(LogTemp, Warning, TEXT("Image loaded successfully!"));
}
}
}
}
}
}
UTexture2D* AYourActor::LoadTextureFromDisk(const FString& FilePath)
{
UTexture2D* LoadedTexture = nullptr;
TArray<uint8> FileData;
if (!FFileHelper::LoadFileToArray(FileData, *FilePath))
{
UE_LOG(LogTemp, Error, TEXT("Failed to load file: %s"), *FilePath);
return nullptr;
}
LoadedTexture = UTexture2D::CreateTransient(1, 1);
LoadedTexture->SRGB = true;
if (LoadedTexture->IsValidLowLevel())
{
EImageFormat ImageFormat = EImageFormat::Invalid;
if (FPaths::GetExtension(FilePath) == ".png")
ImageFormat = EImageFormat::PNG;
else if (FPaths::GetExtension(FilePath) == ".jpg" || FPaths::GetExtension(FilePath) == ".jpeg")
ImageFormat = EImageFormat::JPEG;
TSharedPtr<IImageWrapper> ImageWrapper = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper")).CreateImageWrapper(ImageFormat);
if (!ImageWrapper.IsValid() || !ImageWrapper->SetCompressed(FileData.GetData(), FileData.Num()))
{
UE_LOG(LogTemp, Error, TEXT("Failed to create image wrapper or set compressed data!"));
return nullptr;
}
TArray<uint8> RawData;
if (!ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, RawData))
{
UE_LOG(LogTemp, Error, TEXT("Failed to get raw image data!"));
return nullptr;
}
LoadedTexture->Source.Init(1, 1, 0, 1, ETextureSourceFormat::TSF_BGRA8, RawData.GetData());
LoadedTexture->UpdateResource();
}
return LoadedTexture;
}
这段代码首先创建了一个HTTP请求并发送到指定的URL。当请求完成时,会调用OnImageReceived
函数来处理响应数据。在该函数中,通过解析图像数据并保存为本地文件。然后,可以使用LoadTextureFromDisk
函数加载图像文件为UE的纹理对象。
请确保引入了正确的模块头文件,并将URL替换为你要获取图像的URL地址。此外,还需注意加载和保存纹理时的路径设置和错误处理。
希望以上代码能够帮助你在UE中通过HTTP获取图片!
内容由零声教学AI助手提供,问题来源于学员提问