用ai写了个UE5插件

文章目录

实际需求

这个需求来源于之前的一个项目,当时用了一个第三方插件,里边有一些绘制线段的代码,c++层用的是drawdebugline,当时看底层,觉得应该没问题,不应该在release上跑不起来,由于种种原因,没有时间去搞这块,后来丢给其他同事,也解决了,但是没有按照我的思路来。

最近在玩各种ai工具,觉得有点意思,就去解决点实际问题,就拿当时的问题练练手。

1.头文件

cpp 复制代码
#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "RuntimeLineDrawer.generated.h"

UCLASS()
class YOURMODULE_API URuntimeLineDrawer : public UBlueprintFunctionLibrary
{
    GENERATED_BODY()

public:
    /** 在运行时(含Shipping)绘制一条线 */
    UFUNCTION(BlueprintCallable, Category="RuntimeDraw")
    static void DrawLineRuntime(
        UObject* WorldContextObject,
        FVector Start,
        FVector End,
        FLinearColor Color = FLinearColor::Red,
        float Thickness = 2.f,
        float LifeTime = 0.f,
        bool bDepthTest = true
    );
};

2.源文件

cpp 复制代码
#include "RuntimeLineDrawer.h"
#include "Engine/World.h"
#include "Engine/Engine.h"
#include "Components/LineBatchComponent.h"
#include "Engine/EngineTypes.h" // SDPG_World, SDPG_Foreground

void URuntimeLineDrawer::DrawLineRuntime(
    UObject* WorldContextObject,
    FVector Start,
    FVector End,
    FLinearColor Color,
    float Thickness,
    float LifeTime,
    bool bDepthTest
)
{
    if (!WorldContextObject) return;

    UWorld* World = WorldContextObject->GetWorld();
    if (!World) return;

    // 确保世界里有一个持久的 LineBatcher(发布版也可用)
    ULineBatchComponent* LineBatcher = World->PersistentLineBatcher;
    if (!LineBatcher)
    {
        LineBatcher = NewObject<ULineBatchComponent>(World);
        LineBatcher->RegisterComponentWithWorld(World);
        World->PersistentLineBatcher = LineBatcher;
    }

    // 构建一条线(FBatchedLine)
    // DepthPriority: 0=SDPG_World, 1=SDPG_Foreground
    const uint8 DepthPriority = bDepthTest ? (uint8)SDPG_World : (uint8)SDPG_Foreground;

    TArray<FBatchedLine> Lines;
    Lines.Emplace(Start, End, Color, Thickness, LifeTime, DepthPriority);

    LineBatcher->DrawLines(Lines);
    LineBatcher->MarkRenderStateDirty(); // 提示渲染更新
}

3.用法

cpp 复制代码
URuntimeLineDrawer::DrawLineRuntime(this, FVector(0,0,100), FVector(100,0,100),
                                    FLinearColor::Green, 3.f, /*LifeTime*/2.f, /*bDepthTest*/true);

小结

这只是个例子,完整的代码托管在完整代码,可以看下目录结构,如下:

vbnet 复制代码
YourProject/
└─ Plugins/
   └─ RuntimeLineDraw/
      ├─ RuntimeLineDraw.uplugin
      └─ Source/
         └─ RuntimeLineDraw/
            ├─ RuntimeLineDraw.Build.cs
            ├─ Public/
            │  └─ RuntimeLineDrawer.h
            └─ Private/
               ├─ RuntimeLineDrawer.cpp
               └─ RuntimeLineDrawModule.cpp

有兴趣可以去看看我这是以插件的形式给出的,具体,可以参考上图放到你自己的项目工程中。

相关推荐
hsjkdhs1 小时前
万字详解C++之构造函数析构函数
开发语言·c++
SELSL2 小时前
SQLite3的API调用实战例子
linux·数据库·c++·sqlite3·sqlite实战
什么半岛铁盒2 小时前
C++项目:仿muduo库高并发服务器-------Channel模块实现
linux·服务器·数据库·c++·mysql·ubuntu
闭着眼睛学算法2 小时前
【华为OD机考正在更新】2025年双机位A卷真题【完全原创题解 | 详细考点分类 | 不断更新题目 | 六种主流语言Py+Java+Cpp+C+Js+Go】
java·c语言·javascript·c++·python·算法·华为od
ShineSpark3 小时前
C++面试11——指针与引用
c++·面试
杨小码不BUG3 小时前
CSP-J/S初赛知识点精讲-图论
c++·算法·图论··编码·csp-j/s初赛
初圣魔门首席弟子3 小时前
flag使用错误出现bug
c++·bug
Mr_WangAndy4 小时前
C++设计模式_创建型模式_原型模式Prototype
c++·设计模式·原型模式
奔跑吧邓邓子4 小时前
【C++实战㊷】C++ 原型模式实战:从概念到高效应用
c++·实战·原型模式
奔跑吧邓邓子4 小时前
【C++实战㊶】C++建造者模式:复杂对象构建的秘密武器
c++·实战·建造者模式