UE4 Spline组件C++样例

#pragma once
 
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/SplineMeshComponent.h"
#include "MySplineActor.generated.h"
 
/**
 *
 */
UCLASS()
class MYRENDERAPP_API AMySplineActor : public AActor
{
    GENERATED_BODY()
     
public:
    AMySplineActor();
 
protected:
 
    virtual void OnConstruction(const FTransform& Transform);
 
protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;
 
protected:
 
    //线条组件
    UPROPERTY(VisibleDefaultsOnly, Category = "Spline", meta = (AllowPrivateAccess = true))
    class USplineComponent* Spline;
 
    //线条使用的静态mesh
    UPROPERTY(EditAnywhere, Category = "Spline", meta = (AllowPrivateAccess = true))
    class UStaticMesh* SplineStaticMesh;
 
    //Mesh的朝向
    UPROPERTY(EditAnywhere, Category = "Spline", meta = (AllowPrivateAccess = true))
    TEnumAsByte ForwardAxis;
 
private:
 
    //生成线条Mesh
    void CreateSplineMesh();
 
 
public:
 
    //设置Spline的顶点
    void SetSplinePoints(const TArray &SplinePoints);
 
};//本文为CSDN博主执手画眉弯原创,未经允许不得转载!
#include "MySplineActor.h"
#include "Components/SplineComponent.h"
 
 
 
AMySplineActor::AMySplineActor()
{
    Spline = CreateDefaultSubobject(TEXT("SplineComponent0"));
    RootComponent = Spline;
 
    ForwardAxis = ESplineMeshAxis::Type::X;
 
}
 
void AMySplineActor::OnConstruction(const FTransform& Transform)
{
    Super::OnConstruction(Transform);
    CreateSplineMesh();
}
 
void AMySplineActor::BeginPlay()
{
    Super::BeginPlay();
 
}
 
void AMySplineActor::CreateSplineMesh()
{
    if (Spline->GetNumberOfSplinePoints() < 2)
        return;
 
    for (int32 i = 0; i < Spline->GetNumberOfSplinePoints() - 1; i++)
    {
        USplineMeshComponent* pSplineMesh = NewObject(this, USplineMeshComponent::StaticClass());
        pSplineMesh->CreationMethod = EComponentCreationMethod::UserConstructionScript;
        pSplineMesh->SetStaticMesh(SplineStaticMesh);
        pSplineMesh->SetupAttachment(Spline);
        pSplineMesh->SetForwardAxis(ForwardAxis, false);
        FVector StartPos;
        FVector StartTangent;
        Spline->GetLocationAndTangentAtSplinePoint(i, StartPos, StartTangent, ESplineCoordinateSpace::Local);
        FVector EndPos;
        FVector EndTangent;
        Spline->GetLocationAndTangentAtSplinePoint(i + 1, EndPos, EndTangent, ESplineCoordinateSpace::Local);
        pSplineMesh->SetStartAndEnd(StartPos, StartTangent, EndPos, EndTangent);
    }
 
    //注册组件
    RegisterAllComponents();
}
 
void AMySplineActor::SetSplinePoints(const TArray& SplinePoints)
{
    Spline->SetSplineLocalPoints(SplinePoints);
}
//本文为CSDN博主执手画眉弯原创,未经允许不得转载!

生成该Actor的代码:

FVector Location = WallLocation;
FRotator Rotation(0.f, 0.f, 0.f);
FVector Scale(1.f, 1.f, 1.f);
FTransform SpawnTransform = FTransform(Rotation, Location, Scale);
AMySplineActor* pSplineActor = GetWorld()->SpawnActorDeferred(SplineClass, SpawnTransform);
if (pSplineActor)
{
    pSplineActor->SetSplinePoints(WallPointList);
    UGameplayStatics::FinishSpawningActor(pSplineActor, SpawnTransform);
}//本文为CSDN博主执手画眉弯原创,未经允许不得转载!

 

你可能感兴趣的:(UE4学习)