UE4C++学习篇(十八)-- SceneCaptureComponent组件处理小地图

本文介绍了如何在Unity中利用SceneCaptureComponent2D组件创建小地图系统。通过创建C++类,设置摄像机、RenderTarget和材质,实现实时拍摄场景并在UI上显示小地图。同时,确保角色位置在高处时小地图的正确显示,并展示了如何更新角色在小地图上的图标位置。
摘要由CSDN通过智能技术生成

一般在我们的地图上面会有对应场景的小地图,做法有很多种,这次分享一下使用SceneCaptureComponent2D这个组件去处理小地图。

这个的原理是使用摄像机,创建一个RenderTarget和对应材质,通过实时拍摄画面,传递到创建的UI中。

1.创建拍摄带有SceneCaptureComponent2D组件的C++类:

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

#pragma once

#include "CoreMinimal.h"
#include "Engine/SceneCapture2D.h"
#include "MiniMapActor.generated.h"

class ARoleBase;

/**
 * 
 */
UCLASS()
class RPGGAME_API AMiniMapActor : public ASceneCapture2D
{
	GENERATED_BODY()

public:

	AMiniMapActor();

	virtual void BeginPlay() override;

	/*更新小地图Actor位置*/
	void UpdateMiniMapActorLocation();

protected:
	virtual void Tick( float DeltaSeconds) override;

protected:
	ARoleBase* PlayerRef;

	UPROPERTY(EditAnywhere,Category = "Properties")
	float CameraDistance;
	
};
// Fill out your copyright notice in the Description page of Project Settings.


#include "Other/MiniMapActor.h"
#include "Kismet/GameplayStatics.h"
#include "Gameplay/RoleBase.h"
#include "Components/SceneCaptureComponent2D.h"

AMiniMapActor::AMiniMapActor()
{
	PrimaryActorTick.bCanEverTick = true;
	GetCaptureComponent2D()->SetRelativeRotation(FRotator(-90.0f,0.0f,0.0f));
	CameraDistance = 1000.0f;
}

void AMiniMapActor::BeginPlay()
{
	Super::BeginPlay();
	PlayerRef = Cast<ARoleBase>(UGameplayStatics::GetPlayerPawn(this,0));
}

void AMiniMapActor::UpdateMiniMapActorLocation()
{
	if (!PlayerRef)
	{
		GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Red,TEXT("11111111111"));
		return;
	}
	const FVector PlayerLocation = PlayerRef->GetActorLocation();
	const FVector NewLocation = FVector(PlayerLocation.X,PlayerLocation.Y,PlayerLocation.Z + CameraDistance);
	SetActorLocation(NewLocation);
}

void AMiniMapActor::Tick(float DeltaSeconds)
{
	Super::Tick(DeltaSeconds);
	UpdateMiniMapActorLocation();
}

2.创建渲染使用的RenderTarget:

并创建出对应的材质:

 注意选择使用遮罩和UI,注意圆形遮罩的中心点。

 3.创建UserWidget,将RenderTarget材质赋值:

 上述的代码中有将摄像机和角色绑定为相对位置,这样角色到一些地图的高处的时候就仍然会保持相对位置,不会穿模。

效果图:

 上面有一个对应角色位置的小图标,我这里是用代码每帧处理的:

获取到角色引用之后,每帧设置提示小图标角度即可。

const FRotator PlayerRotation = PlayerRef->GetActorRotation();
PlayerTip->SetRenderTransformAngle(PlayerRotation.Yaw);

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卡西莫多说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值