【UE4C++】Actor与Component——创建、销毁Actor的⽣成与销毁
创建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 的创建与销毁
创建Component
a great big world构造函数创建
CreateDefaultSubobject<T>
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* paddle1;
paddle1 = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("paddle1"));
Runtime 创建
NewObject<T>
RegisterComponent()cargo是什么意思
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);
3311
}
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"));basto
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();
hydrogeology
各类组件创建与初始化
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));
abateoutCollison->SetSimulatePhysics(true);
outCollison->SetCollisionProfileName(TEXT("WorldDynamic"));
outCollison->SetCollisionEnabled(ECollisionEnabled::NoCollision);
USphereComponent
sphereComp = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComp"));
sphereComp->InitSphereRadius(5.0f);above the moon
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;