UE4 C++生成actor截图
时间: 2023-07-11 17:48:44 浏览: 167
ue4蓝图c++动态改变staticmeshactor材质例子动态修改纹理
你可以使用UE4的截图API来生成Actor的截图。以下是一些简单的步骤:
1. 在你的Actor类中添加一个函数,该函数将用于生成截图。例如:
```cpp
void MyActor::TakeScreenshot()
{
UWorld* World = GetWorld();
if (World)
{
// 获取GameViewportClient
UGameViewportClient* ViewportClient = World->GetGameViewport();
if (ViewportClient)
{
// 获取Viewport大小
int32 Width, Height;
ViewportClient->GetViewportSize(Width, Height);
// 创建RenderTarget
UTextureRenderTarget2D* RenderTarget = UTextureRenderTarget2D::CreateTransient(Width, Height);
// 创建SceneCaptureComponent2D
USceneCaptureComponent2D* SceneCaptureComponent = NewObject<USceneCaptureComponent2D>(this);
// 设置SceneCaptureComponent2D
SceneCaptureComponent->TextureTarget = RenderTarget;
SceneCaptureComponent->FOVAngle = 90.f;
SceneCaptureComponent->bCaptureEveryFrame = false;
SceneCaptureComponent->bCaptureOnMovement = false;
// 将SceneCaptureComponent2D添加到Actor中
SceneCaptureComponent->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
// 绘制Actor到RenderTarget中
ViewportClient->Draw();
SceneCaptureComponent->CaptureScene();
// 保存RenderTarget到PNG文件
FString FilePath = FPaths::ProjectSavedDir() / TEXT("Screenshots/MyActor.png");
FHighResScreenshotConfig Config;
Config.SetHDRCapture(false);
Config.SetResolutionMultiplier(1);
Config.SaveImage(FilePath, RenderTarget);
// 删除SceneCaptureComponent2D
SceneCaptureComponent->DetachFromComponent(FDetachmentTransformRules::KeepRelativeTransform);
SceneCaptureComponent->DestroyComponent();
}
}
}
```
2. 在需要截图的地方调用该函数。例如,你可以将该函数添加到Actor的OnClicked事件中:
```cpp
void MyActor::OnClicked()
{
TakeScreenshot();
}
```
3. 运行游戏,并在Actor上单击以生成截图。截图将保存在项目的Saved/Screenshots目录中。
希望这能够帮助到你!
阅读全文