当前位置: 首页 > news >正文

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

一.创造一个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>();

相关文章:

  • 万字长文破解 AI 图片生成算法-Stable diffusion
  • flutter hive使用(复杂类)
  • 如何在Flask中处理静态文件
  • DeepSeek生成思维导图
  • cxxopts位置参数示例
  • SEO短视频矩阵系统源码开发概述
  • 一维差分算法篇:高效处理区间加减
  • 三维重建(十二)——3D先验的使用
  • VisoMaster整合包及汉化
  • 如何用 Linux 权限管理打造无懈可击的系统?
  • Python编程笔记
  • 微软AutoGen高级功能——Selector Group Chat
  • pt->onnx->rknn(量化) step by step FAQ
  • Linux vmstat 命令
  • 【自学笔记】机器学习基础知识点总览-持续更新
  • Spring Data Neo4j
  • DeepSeek+Excel 效率翻倍
  • 详细解释一下HTTPS握手过程中的密钥交换?
  • 【Qt】QObject类的主要功能
  • C语言交换排序之快速排序
  • 怎么在360网站做词条/crm软件
  • 做网站属于广告费吗/2023网站分享
  • 优普南通网站建设/收录查询工具
  • 美食鉴赏国内网站/网推接单平台
  • 网站答辩ppt怎么做/流量精灵
  • 学网站开发好找工作吗/武汉seo搜索优化