ZBLOG

ue 通过http获取图片

在Unreal Engine中,你可以使用HTTP模块来获取图片。以下是一个简单的示例代码,展示如何通过HTTP请求获取图像并将其加载到UE的材质中:

  1. #include "HttpModule.h"
  2. #include "IImageWrapperModule.h"
  3. #include "IImageWrapper.h"
  4. void AYourActor::GetImageFromURL(FString URL)
  5. {
  6. TSharedRef<IHttpRequest, ESPMode::ThreadSafe> HttpRequest = FHttpModule::Get().CreateRequest();
  7. HttpRequest->SetVerb("GET");
  8. HttpRequest->SetURL(URL);
  9. HttpRequest->OnProcessRequestComplete().BindUObject(this, &AYourActor::OnImageReceived);
  10. if (HttpRequest->ProcessRequest())
  11. {
  12. UE_LOG(LogTemp, Warning, TEXT("Request sent!"));
  13. }
  14. }
  15. void AYourActor::OnImageReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
  16. {
  17. if (bWasSuccessful && Response.IsValid())
  18. {
  19. TArray<uint8> ImageData = Response->GetContent();
  20. IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
  21. EImageFormat ImageFormat = ImageWrapperModule.DetectImageFormat(ImageData.GetData(), ImageData.Num());
  22. if (ImageFormat != EImageFormat::Invalid)
  23. {
  24. TSharedPtr<IImageWrapper> ImageWrapper = ImageWrapperModule.CreateImageWrapper(ImageFormat);
  25. if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(ImageData.GetData(), ImageData.Num()))
  26. {
  27. const TArray<uint8>* RawImageData = nullptr;
  28. if (ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, RawImageData))
  29. {
  30. FString TextureBasePath = FPaths::ProjectContentDir() + "/Textures/";
  31. FString TexturePath = TextureBasePath + "YourTexture.png";
  32. FFileHelper::SaveArrayToFile(*RawImageData, *TexturePath);
  33. UTexture2D* LoadedTexture = LoadTextureFromDisk(TexturePath);
  34. if (LoadedTexture)
  35. {
  36. // 使用LoadedTexture来设置材质等
  37. UE_LOG(LogTemp, Warning, TEXT("Image loaded successfully!"));
  38. }
  39. }
  40. }
  41. }
  42. }
  43. }
  44. UTexture2D* AYourActor::LoadTextureFromDisk(const FString& FilePath)
  45. {
  46. UTexture2D* LoadedTexture = nullptr;
  47. TArray<uint8> FileData;
  48. if (!FFileHelper::LoadFileToArray(FileData, *FilePath))
  49. {
  50. UE_LOG(LogTemp, Error, TEXT("Failed to load file: %s"), *FilePath);
  51. return nullptr;
  52. }
  53. LoadedTexture = UTexture2D::CreateTransient(1, 1);
  54. LoadedTexture->SRGB = true;
  55. if (LoadedTexture->IsValidLowLevel())
  56. {
  57. EImageFormat ImageFormat = EImageFormat::Invalid;
  58. if (FPaths::GetExtension(FilePath) == ".png")
  59. ImageFormat = EImageFormat::PNG;
  60. else if (FPaths::GetExtension(FilePath) == ".jpg" || FPaths::GetExtension(FilePath) == ".jpeg")
  61. ImageFormat = EImageFormat::JPEG;
  62. TSharedPtr<IImageWrapper> ImageWrapper = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper")).CreateImageWrapper(ImageFormat);
  63. if (!ImageWrapper.IsValid() || !ImageWrapper->SetCompressed(FileData.GetData(), FileData.Num()))
  64. {
  65. UE_LOG(LogTemp, Error, TEXT("Failed to create image wrapper or set compressed data!"));
  66. return nullptr;
  67. }
  68. TArray<uint8> RawData;
  69. if (!ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, RawData))
  70. {
  71. UE_LOG(LogTemp, Error, TEXT("Failed to get raw image data!"));
  72. return nullptr;
  73. }
  74. LoadedTexture->Source.Init(1, 1, 0, 1, ETextureSourceFormat::TSF_BGRA8, RawData.GetData());
  75. LoadedTexture->UpdateResource();
  76. }
  77. return LoadedTexture;
  78. }

这段代码首先创建了一个HTTP请求并发送到指定的URL。当请求完成时,会调用OnImageReceived函数来处理响应数据。在该函数中,通过解析图像数据并保存为本地文件。然后,可以使用LoadTextureFromDisk函数加载图像文件为UE的纹理对象。

请确保引入了正确的模块头文件,并将URL替换为你要获取图像的URL地址。此外,还需注意加载和保存纹理时的路径设置和错误处理。

希望以上代码能够帮助你在UE中通过HTTP获取图片!


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=12240

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?