【UE4 C++】Actor 与 Component —— 创建、销毁
阅读原文时间:2023年07月11日阅读:1

Actor的生成与销毁

  • UClass*

  • TSubclassOf<T>

  • SpawnActor()

    UPROPERTY(EditAnywhere, Category = "init")
        UClass* bullet0; // 取类
    
    UPROPERTY(EditAnywhere, Category = "init")
        TSubclassOf<Amissile> bullet; //取Amissile类及其派生类
    
    UPROPERTY(EditAnywhere, Category = "init")
        Amissile* bullet2; // 取类的实例
    
    
    GetWorld()->SpawnActor<Amissile>(bullet0, GetActorTransform());
    
    GetWorld()->SpawnActor<Amissile>(bullet, body->GetSocketTransform("FirePos"));
    
    Amissile* MissileIns = GetWorld()->SpawnActor<Amissile>(bullet2->StaticClass(), GetActorTransform());

直接销毁

  • 使用Destroy()

    MissileIns->Destroy();

设置生命周期

  • 构造函数设置 InitialLifeSpan

    InitialLifeSpan = 8.0f;
  • BeginPlay 里使用 SetLifeSpan() 设置生命周期

    InitialLifeSpan = 8.0f;

Component 的创建与销毁

构造函数创建

  • CreateDefaultSubobject<T>

    UPROPERTY(VisibleAnywhere)
        UStaticMeshComponent* paddle1;
    
    paddle1 = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("paddle1"));

Runtime 创建

  • NewObject<T>

  • RegisterComponent()

    Mesh = NewObject<UStaticMeshComponent>(this,TEXT("MyMesh"));
    Mesh->SetupAttachment(root);
    Mesh->RegisterComponent(); // 注册渲染/物理状态
    // 可配合 LoadObject()

FObjectFinder 构造函数加载资源

  • ConstructorHelpers::FObjectFinder<T>

    static ConstructorHelpers::FObjectFinder<UStaticMesh> paddleMesh(TEXT("StaticMesh'/Game/Demo_Drone/SM/paddle.paddle'"));
    if (paddleMesh.Succeeded())
    {
        paddle1->SetStaticMesh(paddleMesh.Object);
    }

LoadObject 加载资源

  • LoadObject<T>

    UStaticMesh* bodyMesh = LoadObject<UStaticMesh>(this,TEXT("StaticMesh'/Game/Demo_Drone/SM/drone.drone'"));
    if (bodyMesh)
    {
        body->SetStaticMesh(bodyMesh);
    }
  • 设置根组件 RootComponent 或者 SetRootComponent()

  • SetupAttachment

  • AttachToComponent

    RootComponent = outCollison;
    SetRootComponent(outCollison);
    
    paddle1->SetupAttachment(body, TEXT("paddle1"));
    paddle2->SetupAttachment(body, TEXT("paddle2"));
    paddle3->AttachToComponent(body, FAttachmentTransformRules::KeepRelativeTransform, TEXT("paddle3"));
    paddle4->AttachToComponent(body, FAttachmentTransformRules::KeepRelativeTransform, TEXT("paddle4"));
    
    childSceneComponent->AttachToComponent(root, FAttachmentTransformRules::SnapToTargetIncludingScale);

  • DestroyComponent()

  • UnregisterComponent() 使用 RegisterComponent 创建,需要先注销再销毁

    Mesh->UnregisterComponent();
    Mesh->DestroyComponent();

各类组件创建与初始化

UStaticMeshComponent

paddle1 = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("paddle1"));
auto paddleMesh = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Game/Demo_Drone/SM/paddle.paddle'"));
if (paddleMesh.Object != nullptr)
{
    paddle1->SetStaticMesh(paddleMesh.Object);
}

UBoxComponent

outCollison = CreateDefaultSubobject<UBoxComponent>(TEXT("outCollison"));
outCollison->SetBoxExtent(FVector(60, 60, 15));
outCollison->SetSimulatePhysics(true);
outCollison->SetCollisionProfileName(TEXT("WorldDynamic"));
outCollison->SetCollisionEnabled(ECollisionEnabled::NoCollision);

USphereComponent

sphereComp = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComp"));
sphereComp->InitSphereRadius(5.0f);
sphereComp->SetCollisionProfileName(TEXT("WorldDynamic"));
sphereComp->SetGenerateOverlapEvents(true);
sphereComp->OnComponentBeginOverlap.AddDynamic(this, &Amissile::Overlaphandler);


// overlap 函数绑定
void Amissile::Overlaphandler(UPrimitiveComponent* OverlappedComponent,
                AActor* OtherActor,
                UPrimitiveComponent* OtherComp,
                int32 OtherBodyIndex,
                bool bFromSweep,
                const FHitResult& SweepResult){}

USkeletalMeshComponent

SkeletalMeshComp = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("SkeletalMeshComp"));
SkeletalMeshComp->SetGenerateOverlapEvents(true);
SkeletalMeshComp->SetSimulatePhysics(true);

USpringArmComponent 和 UCameraComponent

springArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
springArmComp->SetupAttachment(RootComponent);
cameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
cameraComp->SetupAttachment(springArmComp);

UPhysicsThrusterComponent

upThrusterComp = CreateDefaultSubobject<UPhysicsThrusterComponent>(TEXT("upThrusterComp"));
upThrusterComp->SetupAttachment(RootComponent);
upThrusterComp->SetWorldRotation(UKismetMathLibrary::MakeRotFromX(FVector(-this->GetActorUpVector())));
upThrusterComp->ThrustStrength = 980.0f;
upThrusterComp->SetAutoActivate(true);

UTextRenderComponent

CountdownText = CreateDefaultSubobject<UTextRenderComponent>(TEXT("CountdownNumber"));
CountdownText->SetHorizontalAlignment(EHTA_Center);
CountdownText->SetWorldSize(150.0f);
CountdownText->AttachTo(RootComponent);
CountdownTime = 3;
CountdownText->SetText(FString::FromInt(FMath::Max(CountdownTime, 0)));


projectileMovementComp = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("projectileMovement"));
projectileMovementComp->InitialSpeed = 500.0f;