C++ 里创建 SplineMeshComponent
void ASplineActor::OnConstruction(const FTransform& Transform){
...
AddSMComponent(tran);
...
}
void ASplineActor::SetupSpline()
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, "called in setup spline");
if (ASplineActor::Mesh != nullptr)
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, "has a static mesh");
UWorld* World = this->GetWorld();
for (int32 i = 0; i < (MySpline->GetNumberOfSplinePoints() - 2); i++)
{
USplineMeshComponent* SplineMesh = NewObject<USplineMeshComponent>(this,
USplineMeshComponent::StaticClass());
SplineMesh->SetStaticMesh(Mesh);
SplineMesh->RegisterComponentWithWorld(World);
SplineMesh->CreationMethod = EComponentCreationMethod::UserConstructionScript;
SplineMesh->SetMobility(EComponentMobility::Movable);
SplineMesh->SetForwardAxis(splineForward, true);
SplineMesh->AttachToComponent(MySpline, FAttachmentTransformRules::KeepRelativeTransform);
FVector SP = MySpline->GetLocationAtSplinePoint(i, ESplineCoordinateSpace::Type::Local);
FVector ST = MySpline->GetTangentAtSplinePoint(i, ESplineCoordinateSpace::Type::Local);
FVector EP = MySpline->GetLocationAtSplinePoint(i + 1, ESplineCoordinateSpace::Type::Local);
FVector ET = MySpline->GetTangentAtSplinePoint(i + 1, ESplineCoordinateSpace::Type::Local);
SplineMesh->SetStartAndEnd(SP, ST, EP, ET, true);
SplineMesh->SetCollisionEnabled(ECollisionEnabled::Type::QueryAndPhysics);
}
}
}