虚幻C++简单屏幕截图功能

记录备用

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintAsyncActionBase.h"
#include "MyFunctions.generated.h"

UCLASS()
class EVENTTEST_API UMyFunctions : public UBlueprintAsyncActionBase
{
	GENERATED_BODY()

	static FDelegateHandle DelegateHandle;
	UFUNCTION(BlueprintCallable)
	static void CaptureScreenshot();
	static void OnScreenshotCompleteToPng(int32 InWidth, int32 InHeight, const TArray<FColor>& InColor);
	static void OnScreenshotCompleteToJpg(int32 InWidth, int32 InHeight, const TArray<FColor>& InColor);
};
// Fill out your copyright notice in the Description page of Project Settings.


#include "MyFunctions.h"

#include "IImageWrapper.h"
#include "IImageWrapperModule.h"
#include "ImageUtils.h"
FDelegateHandle UMyFunctions::DelegateHandle = {};

void UMyFunctions::CaptureScreenshot()
{
	if(GEngine && GEngine->GameViewport){}
	//DelegateHandle =  UGameViewportClient::OnScreenshotCaptured().AddStatic(&OnScreenshotCompleteToPng);
	DelegateHandle =  UGameViewportClient::OnScreenshotCaptured().AddStatic(&OnScreenshotCompleteToJpg);
	GEngine->GameViewport->Exec(nullptr, TEXT("HighResShot 1080x720"), *GLog);
}

void UMyFunctions::OnScreenshotCompleteToPng(int32 InWidth, int32 InHeight, const TArray<FColor>& InColors)
{
	FString ScreenShotName = FPaths::ProjectContentDir()+ TEXT("test.png");
	TArray<uint8> CompressBitmap;
	FImageUtils::CompressImageArray(InWidth,InHeight,InColors,CompressBitmap);
	FFileHelper::SaveArrayToFile(CompressBitmap,*ScreenShotName);
	UE_LOG(LogTemp,Warning,TEXT("maoguo->保存成功"));
	UGameViewportClient::OnScreenshotCaptured().Remove(DelegateHandle);
}

inline void UMyFunctions::OnScreenshotCompleteToJpg(int32 InWidth, int32 InHeight, const TArray<FColor>& InColors)
{
	FString ScreenShotName = FPaths::ProjectContentDir()+ TEXT("test.jpg");
	IImageWrapperModule& ImageWrapperModule = FModuleManager::Get().LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
	IImageWrapperPtr ImageWrapper = ImageWrapperModule.CreateImageWrapper(EImageFormat::JPEG);
	if(ImageWrapper->SetRaw(InColors.GetData(),InColors.Num() * sizeof(FColor),
								InWidth,InHeight,ERGBFormat::BGRA,8))
	{
		int32 Quality = 100;
		FFileHelper::SaveArrayToFile(ImageWrapper->GetCompressed(Quality),*ScreenShotName);
	}
	UGameViewportClient::OnScreenshotCaptured().Remove(DelegateHandle);
}

需要注意的地方:

  • 如果编译报错,有关于ImageWrapper的,那么在项目配置中加入ImageWrapper模块
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore","ImageWrapper"});

原文说要引入这个模块,但是我UE4.27.2中,没有引入也没有报错

2. 蓝图函数库中要暴露给蓝图使用的函数必须为static(非static函数,蓝图调用的时候必须要有通过对象,就是我们在大部分节点中看到的第一个参数Target),例如代码中的CaptureScreenshot(),还有代码中AddStatic中的函数也必须时static

### 如何在虚幻引擎中使用C++实现函数输出 为了实现在虚幻引擎中的函数输出,开发者通常依赖`UE_LOG`宏来打印调试信息到控制台或日志文件。此方法不仅简单而且高效,在开发过程中非常有用。 下面是一个简单的例子展示如何定义并调用一个可以输出字符串至屏幕的日志记录器: ```cpp #include "YourClass.h" #include "Runtime/Engine/Public/TimerManager.h" // 定义一个新的类别用于日志管理 DEFINE_LOG_CATEGORY_STATIC(MyLogCategory, Log, All); void AYourClass::BeginPlay() { Super::BeginPlay(); // 使用 UE_LOG 输出一条消息 UE_LOG(MyLogCategory, Warning, TEXT("This is a warning message from BeginPlay.")); } ``` 上述代码片段展示了怎样创建自定义日志分类以及通过`UE_LOG`发送警告级别的通知[^2]。 对于更复杂的场景,比如希望将变量的内容也一并显示出来,则可以直接把它们作为参数传递给`UE_LOG`: ```cpp int32 MyVariable = 42; UE_LOG(MyLogCategory, Display, TEXT("The value of my variable is %d"), MyVariable); ``` 这里利用了格式化字符串的能力,使得能够灵活地组合固定文本与动态数据一起呈现。 如果目标是在游戏运行期间持续更新某些状态信息,那么还可以考虑结合定时器功能定期触发此类操作: ```cpp FTimerHandle TimerHandle_PrintMessage; void AYourClass::StartPeriodicLogging(float Interval) { GetWorld()->GetTimerManager().SetTimer(TimerHandle_PrintMessage, this, &AYourClass::PrintStatus, Interval, true); } void AYourClass::PrintStatus() { int32 CurrentTickCount = TickCounter++; UE_LOG(MyLogCategory, Verbose, TEXT("Current tick count: %d"), CurrentTickCount); } ``` 这段程序说明了设置周期性事件的方式,并且每次计时结束都会执行一次指定的成员函数来进行相应的日志记录工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值