UE 4 SplineコンポーネントC++サンプル
3502 ワード
#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<:type> 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 , !