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;
CreateDefaultSubobject<T>
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* paddle1;
paddle1 = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("paddle1"));
NewObject<T>
RegisterComponent()
Mesh = NewObject<UStaticMeshComponent>(this,TEXT("MyMesh"));
Mesh->SetupAttachment(root);
Mesh->RegisterComponent(); // 注册渲染/物理状态
// 可配合 LoadObject()
ConstructorHelpers::FObjectFinder<T>
static ConstructorHelpers::FObjectFinder<UStaticMesh> paddleMesh(TEXT("StaticMesh'/Game/Demo_Drone/SM/paddle.paddle'"));
if (paddleMesh.Succeeded())
{
paddle1->SetStaticMesh(paddleMesh.Object);
}
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();
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);
}
outCollison = CreateDefaultSubobject<UBoxComponent>(TEXT("outCollison"));
outCollison->SetBoxExtent(FVector(60, 60, 15));
outCollison->SetSimulatePhysics(true);
outCollison->SetCollisionProfileName(TEXT("WorldDynamic"));
outCollison->SetCollisionEnabled(ECollisionEnabled::NoCollision);
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){}
SkeletalMeshComp = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("SkeletalMeshComp"));
SkeletalMeshComp->SetGenerateOverlapEvents(true);
SkeletalMeshComp->SetSimulatePhysics(true);
springArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
springArmComp->SetupAttachment(RootComponent);
cameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
cameraComp->SetupAttachment(springArmComp);
upThrusterComp = CreateDefaultSubobject<UPhysicsThrusterComponent>(TEXT("upThrusterComp"));
upThrusterComp->SetupAttachment(RootComponent);
upThrusterComp->SetWorldRotation(UKismetMathLibrary::MakeRotFromX(FVector(-this->GetActorUpVector())));
upThrusterComp->ThrustStrength = 980.0f;
upThrusterComp->SetAutoActivate(true);
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;
手机扫一扫
移动阅读更方便
你可能感兴趣的文章