UE4利用C++实现简单的胜利点类Actor

本文介绍了如何在UE4中使用C++创建一个简单的胜利点类Actor。通过新建C++ Actor类,定义并实现重叠事件,以及在日志中打印调试信息。此外,还涉及到贴花(Decal)的使用来装饰胜利点,以及在C++中为Actor添加贴花组件的步骤。
摘要由CSDN通过智能技术生成
  1. 创建一个调试方案,打印在日志上,方便调试,用Overlap举例说明,打开UE,新建C++的Actor类,回到VS中,进行Actor的定义。.Cpp代码如下:

#include "FPSExtractionZone.h"


// Sets default values
AFPSExtractionZone::AFPSExtractionZone()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	OverlapComp = CreateDefaultSubobject<UBoxComponent>(TEXT("OverlapComp"));
	OverlapComp->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
	OverlapComp->SetCollisionResponseToAllChannels(ECR_Ignore);
	OverlapComp->SetCollisionResponseToChannel(ECC_Pawn, ECR_Overlap);
	OverlapComp->SetBoxExtent(FVector(200.f));
	RootComponent = OverlapComp;

	OverlapComp->SetHiddenInGame(false);

	OverlapComp->OnComponentBeginOverlap.AddDynamic(this, &AFPSExtractionZone::HandleOverlap);





}

// Called when the game star
UE4,你可以通过将截图功能封装成一个Actor实现。以下是一个简单C++实现: 1. 创建一个新的Actor,例如 "ScreenshotActor"。 2. 在 "ScreenshotActor" Blueprint 添加一个函数,例如 "TakeScreenshot"。 3. 在 "ScreenshotActor" 的 C++ 代码实现 "TakeScreenshot" 函数,如下所示: ```cpp void AScreenshotActor::TakeScreenshot() { // Get the player controller APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0); if (!PlayerController) { return; } // Take the screenshot FViewport* Viewport = GEngine->GameViewport->Viewport; TArray<FColor> Bitmap; bool bScreenshotSuccessful = Viewport->ReadPixels(Bitmap, FReadSurfaceDataFlags(), FIntRect(0, 0, Viewport->GetSizeXY().X, Viewport->GetSizeXY().Y)); if (!bScreenshotSuccessful) { return; } // Create the texture UTexture2D* Texture = UTexture2D::CreateTransient(Viewport->GetSizeXY().X, Viewport->GetSizeXY().Y, PF_B8G8R8A8); Texture->AddToRoot(); // Make sure the texture isn't garbage collected Texture->UpdateResource(); // Copy the bitmap data to the texture uint8* MipData = static_cast<uint8*>(Texture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE)); for (int32 Y = 0; Y < Viewport->GetSizeXY().Y; Y++) { for (int32 X = 0; X < Viewport->GetSizeXY().X; X++) { int32 Index = Y * Viewport->GetSizeXY().X + X; MipData[Index * 4 + 0] = Bitmap[Index].B; MipData[Index * 4 + 1] = Bitmap[Index].G; MipData[Index * 4 + 2] = Bitmap[Index].R; MipData[Index * 4 + 3] = Bitmap[Index].A; } } Texture->PlatformData->Mips[0].BulkData.Unlock(); // Save the texture to a file FString Filename = FPaths::ProjectDir() / TEXT("Screenshots") / FDateTime::Now().ToString(TEXT("yyyy-MM-dd_HH-mm-ss")) + TEXT(".png"); FFileHelper::SaveObjectAsPNG(Texture, *Filename); // Clean up Texture->RemoveFromRoot(); } ``` 4. 在 "ScreenshotActor" Blueprint 添加一个触发器或其他交互方式来触发 "TakeScreenshot" 函数。 此代码与前面的代码类似,但使用了 GetWorld() 函数来获取当前世界的引用,而不是使用传递给函数的 WorldContextObject 参数。 注意:在使用此代码之前,请确保在项目设置启用 "With Editor" 选项,以便在运行时允许截屏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值