【UE4 C++】Actor 与 Component —— 创建、销毁

Component 的创建与销毁

创建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;

posted @ 2021-05-05 16:11  砥才人  阅读(4009)  评论(0编辑  收藏  举报