目录
Actor的创建与销毁
创建
UClass*
TSubclassOf
SpawnActor()
销毁
直接销毁:Destroy()
设置生命周期
Component 的创建与销毁
创建
构造函数创建:CreateDefaultSubobject
Runtime创建:NewObject、RegisterComponent()
加载资源
FObjectFinder构造函数加载资源
LoadObject加载资源
设置组件层级关系
销毁组件
各类组件创建与初始化
UStaticMenshComponent
UBoxComponent
USphereComponent
USkeletalMeshComponent
USpringArmComponent和UCameraComponent
UPhysicsThrusterComponent
UTexRenderComponent
UProjectileMovementComponent
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; // 取类的实例
销毁
直接销毁:Destroy()
MissileIns->Destroy();
设置生命周期
构造函数设置InitialLifeSpan
InitialLifeSpan = 8.0f;
BeginPlay里使用SetLifeSpan()设置声明周期
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()
文章来源:https://www.toymoban.com/news/detail-410272.html
加载资源
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加载资源
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);
销毁组件
- DestoryComponent()
- UnregisterComponent()使用RegisterComponent创建,需要先注销再销毁
Mesh->UnregisterComponent();
Mesh->DestroyComponent();
各类组件创建与初始化
UStaticMenshComponent
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);
UTexRenderComponent
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)));
UProjectileMovementComponent
文章来源地址https://www.toymoban.com/news/detail-410272.html
projectileMovementComp = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("projectileMovement"));
projectileMovementComp->InitialSpeed = 500.0f;
到了这里,关于UE4 C++:Actor与Component的创建、销毁的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!