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<ESplineMeshAxis::Type> ForwardAxis;
 
private:
 
    //生成線條Mesh
    void CreateSplineMesh();
 
 
public:
 
    //設置Spline的頂點
    void SetSplinePoints(const TArray<FVector> &SplinePoints);
 
};//本文爲CSDN博主執手畫眉彎原創,未經允許不得轉載!
#include "MySplineActor.h"
#include "Components/SplineComponent.h"
 
 
 
AMySplineActor::AMySplineActor()
{
    Spline = CreateDefaultSubobject<USplineComponent>(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<USplineMeshComponent>(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<FVector>& 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<AMySplineActor>(SplineClass, SpawnTransform);
if (pSplineActor)
{
    pSplineActor->SetSplinePoints(WallPointList);
    UGameplayStatics::FinishSpawningActor(pSplineActor, SpawnTransform);
}//本文爲CSDN博主執手畫眉彎原創,未經允許不得轉載!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章