Image Generation
Generate AI images in Unreal Engine using natural language prompts
Image Generation
UPlayKitImageClient is an ActorComponent that generates images from text prompts. Results are delivered as Base64-encoded PNG data via the OnImageGenerated event. Use Base64ToTexture2D to convert them for engine use.
Setup
Add UPlayKitImageClient to your Actor in the editor (Add Component β PlayKit Image Client), or in C++:
#include "Client/PlayKitImageClient.h"
// In Actor header:
UPROPERTY(VisibleAnywhere)
UPlayKitImageClient* ImageClient;
void AMyActor::BeginPlay()
{
Super::BeginPlay();
ImageClient = FindComponentByClass<UPlayKitImageClient>();
ImageClient->OnImageGenerated.AddDynamic(this, &AMyActor::HandleImageGenerated);
ImageClient->OnError.AddDynamic(this, &AMyActor::HandleError);
}Generating Images
Basic Generation
ImageClient->GenerateImage(TEXT("A mystical forest with glowing mushrooms at twilight"));The result arrives in your OnImageGenerated handler:
void AMyActor::HandleImageGenerated(const FPlayKitGeneratedImage& Image)
{
if (!Image.bSuccess)
{
UE_LOG(LogTemp, Error, TEXT("Image failed: %s"), *Image.ErrorMessage);
return;
}
// Convert Base64 to a usable texture
UTexture2D* Texture = UPlayKitImageClient::Base64ToTexture2D(Image.ImageBase64);
if (Texture)
{
// Apply to material
if (UMaterialInstanceDynamic* DynMat = MeshComponent->CreateDynamicMaterialInstance(0))
DynMat->SetTextureParameterValue(TEXT("BaseTexture"), Texture);
}
}Generate with Seed
Use a fixed seed to reproduce similar images for the same prompt:
ImageClient->GenerateImageWithSeed(TEXT("A red dragon"), 12345);Advanced Options
Use GenerateImagesAdvanced to control size, batch count, and transparency:
FPlayKitImageOptions Options;
Options.Size = TEXT("1792x1024"); // Widescreen landscape
Options.Count = 4; // Generate 4 images
Options.Seed = -1; // Random seed
ImageClient->GenerateImagesAdvanced(
TEXT("Fantasy character portraits, digital art"),
Options
);Batch results arrive via the OnImagesGenerated event:
void AMyActor::HandleImagesGenerated(const TArray<FPlayKitGeneratedImage>& Images)
{
for (const FPlayKitGeneratedImage& Image : Images)
{
if (Image.bSuccess)
{
UTexture2D* Texture = UPlayKitImageClient::Base64ToTexture2D(Image.ImageBase64);
// Use texture...
}
}
}Image Sizes
Set the default size on the component property ImageSize, or pass it in FPlayKitImageOptions:
| Size String | Description |
|---|---|
"256x256" | Small thumbnail |
"512x512" | Medium |
"1024x1024" | Large square (default) |
"1792x1024" | Widescreen landscape |
"1024x1792" | Tall portrait |
Using Generated Images
As Material Texture
UTexture2D* Texture = UPlayKitImageClient::Base64ToTexture2D(Image.ImageBase64);
if (UMaterialInstanceDynamic* DynMaterial = MeshComponent->CreateDynamicMaterialInstance(0))
DynMaterial->SetTextureParameterValue(TEXT("BaseTexture"), Texture);As UI Image
if (UImage* ImageWidget = Cast<UImage>(WidgetComponent->GetWidget()))
ImageWidget->SetBrushFromTexture(Texture);Saving to Disk
TArray<uint8> PNGData;
FImageUtils::CompressImageArray(Texture->GetSizeX(), Texture->GetSizeY(), PixelData, PNGData);
FString SavePath = FPaths::ProjectSavedDir() / TEXT("GeneratedImages/output.png");
FFileHelper::SaveArrayToFile(PNGData, *SavePath);Prompt Tips
// Specific and descriptive
TEXT("A photorealistic medieval castle on a cliff, sunset lighting, dramatic clouds")
// Art style specified
TEXT("Pixel art treasure chest with gold coins, 16-bit game aesthetic")
// Mood and atmosphere
TEXT("Dark fantasy dungeon entrance, torchlight, mysterious fog")Error Handling
ImageClient->OnError.AddDynamic(this, &AMyActor::HandleError);
void AMyActor::HandleError(const FString& ErrorMessage)
{
UE_LOG(LogTemp, Error, TEXT("Image generation error: %s"), *ErrorMessage);
// Show placeholder or error state
}Blueprint Nodes Reference
| Node | Description |
|---|---|
Generate Image | Generate one image from a prompt |
Generate Image With Seed | Generate with a reproducible seed |
Generate Images Advanced | Batch generation with full options |
Base64 To Texture 2D | Convert result to UTexture2D |
Set Image Size | Set default output dimensions |
Set Quality | Set "standard" or "hd" |
Cancel Request | Cancel the in-flight request |
Next Steps
- Learn about Speech Recognition for voice input
- Explore NPC Conversations for AI characters
- See API Reference for complete documentation