UE5.3 C++ USTRUCT的规范使用和制作简单的画线插件

发布于:2025-02-18 ⋅ 阅读:(82) ⋅ 点赞:(0)

一.创造一个USTRUCT

1.首先需要创建一个,None。

#include "LineDataStruct.generated.h"

FTPAData里加入GENERATED_USTRUCT_BODY();

 //Topic=DDS_TPA_Data, 预测航迹线,单次事件
USTRUCT()
struct FTPAData
{
	GENERATED_USTRUCT_BODY();

	int16 header;                              // 包头
	int16 flag;                                // 标志位
	int64 timestamp;                           // 时间戳
	int16 validPointCount;                     // 轨迹有效点数  最大值1000,轨迹有效点数为N1时读取N1个轨迹点数据
	int16 reserved1;                           // 预留接口1
	int16 reserved2;                           // 预留接口2
	int16 reserved3;                           // 预留接口3
	int32 reserved4;                           // 预留接口4
	int32 reserved5;                           // 预留接口5

	FTPAData_Point TPAData_Point[1000];	// 轨迹点数组
	int16 checksum;                            // 校验值
};

#pragma pack()

如果想让蓝图认识,需要加BlueprintType。如果想让变量能在蓝图中访问,可用Break/Make,需要使用UPROPERTY修饰符(BlueprintReadWrite)。

二.画线插件

不多废话,直接上代码

UCLASS()
class HMSLINESYSTEM_API UDrawLineSubsystem : public UGameInstanceSubsystem
{
	GENERATED_BODY()
public:

	
	void UpdateLinePath(int id,FVector First,FVector Second);
	//
	void UpdateLinePath2(int id,FVector First, FVector Second);
	//void LinePatch(FVector Fi);
private:
	void InitialGeoreference();
public:
private:
	ACesiumGeoreference* Georeference;
	//航迹线,航迹线id
	TMap<int8, ASplineActor*> LinePathMap;
	TMap<int8, ALinePatchActor*> LinePatchPathMap;
	UMaterialParameterCollectionInstance* M_ParamCollection;
	int num = 100.f;
	
};

先介绍,这种简单一点的通过,UE自带的LinePatchComponent进行画线,给一头一尾就能画。将Cesium的点换为UE坐标系的点,再去画。

//void UpdateLinePath2(int id,FVector First, FVector Second);
void UDrawLineSubsystem::UpdateLinePath2(int id, FVector First, FVector Second)
{
	int32 tmpPathID = id;
	if (LinePatchPathMap.Find(tmpPathID))
	{
		if (!Georeference)
			InitialGeoreference();
		if (!Georeference) return;

		if (!First.IsZero())
		{
			//处理点位信息
			FVector tmpDataPosition = FVector(First);
			FVector FirstUELocation = Georeference->TransformLongitudeLatitudeHeightPositionToUnreal(tmpDataPosition);

			LinePatchPathMap[tmpPathID]->SetActorLocation(FirstUELocation);
			First = FirstUELocation;
		}
		if (!Second.IsZero())
		{
			//处理点位信息
			FVector tmpDataPosition = FVector(Second);
			FVector SecondUELocation = Georeference->TransformLongitudeLatitudeHeightPositionToUnreal(tmpDataPosition);
			Second = SecondUELocation;
		}
		LinePatchPathMap[tmpPathID]->CreateLine(First, Second);
		//M_ParamCollection->SetScalarParameterValue(FName("CollisionHeightLow"), CollisionRender.HightLow * 100.f);
	}
	else
	{
		ALinePatchActor* LinePatchActor = GetWorld()->SpawnActor<ALinePatchActor>();
		if (!LinePatchActor)
		{
			UE_LOG(LogTemp, Error, TEXT("failed to spawn LinePathActor! UDrawLineSubsystem::UpdateLinePath2"));
			return;
		}
		else
		{
			LinePatchPathMap.Add(tmpPathID, LinePatchActor);
		}
	}
}

这个是画线的,把头和组件加上就行了。

#include "Components/LineBatchComponent.h"
// Sets default values
ALinePatchActor::ALinePatchActor()
{
 	// 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;
	SetRootComponent(CreateDefaultSubobject<USceneComponent>(TEXT("DefaultSceneRoot")));
	LineBatchComponent = CreateDefaultSubobject<ULineBatchComponent>(TEXT("LineBatchComponent"));
	LineBatchComponent->SetupAttachment(RootComponent);
}
void ALinePatchActor::CreateLine(FVector First, FVector Second)
{
	LineBatchComponent->Flush();
	LineBatchComponent->DrawLine(First, Second, FColor::Purple, 1.f, 50.f, 0); // Adjust the colors and thickness as needed
}

用UGameInstanceSubsystem做插件的好处,有很多。自己能管理生命周期,获取方便。

把模块和头加上就能从GameInstance里获得GameSubsystem,再转换为自己的system。

UDrawLineSubsystem* LineSystem = GetWorld()->GetGameInstance()->GetSubsystem<UDrawLineSubsystem>();


网站公告

今日签到

点亮在社区的每一天
去签到