目录
Runtime创建:NewObject、RegisterComponent()
USpringArmComponent和UCameraComponent
- UPROPERTY(EditAnywhere, Category = "init")
- UClass* bullet0; // 取类
-
- UPROPERTY(EditAnywhere, Category = "init")
- TSubclassOf
bullet; //取Amissile类及其派生类 -
- UPROPERTY(EditAnywhere, Category = "init")
- Amissile* bullet2; // 取类的实例
MissileIns->Destroy();
构造函数设置InitialLifeSpan
InitialLifeSpan = 8.0f;
BeginPlay里使用SetLifeSpan()设置声明周期
- UPROPERTY(VisibleAnywhere)
- UStaticMeshComponent* paddle1;
-
- paddle1 = CreateDefaultSubobject
(TEXT("paddle1"));
- Mesh = NewObject
(this,TEXT("MyMesh")); - Mesh->SetupAttachment(root);
- Mesh->RegisterComponent(); // 注册渲染/物理状态
- // 可配合 LoadObject()
- static ConstructorHelpers::FObjectFinder
paddleMesh(TEXT("StaticMesh'/Game/Demo_Drone/SM/paddle.paddle'")) ; - if (paddleMesh.Succeeded())
- {
- paddle1->SetStaticMesh(paddleMesh.Object);
- }
- UStaticMesh* bodyMesh = LoadObject
(this,TEXT("StaticMesh'/Game/Demo_Drone/SM/drone.drone'")); - if (bodyMesh)
- {
- body->SetStaticMesh(bodyMesh);
- }
- 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);
- Mesh->UnregisterComponent();
- Mesh->DestroyComponent();
- paddle1 = CreateDefaultSubobject
(TEXT("paddle1")); - auto paddleMesh = ConstructorHelpers::FObjectFinder
(TEXT("StaticMesh'/Game/Demo_Drone/SM/paddle.paddle'")); - if (paddleMesh.Object != nullptr)
- {
- paddle1->SetStaticMesh(paddleMesh.Object);
- }
- outCollison = CreateDefaultSubobject
(TEXT("outCollison")); - outCollison->SetBoxExtent(FVector(60, 60, 15));
- outCollison->SetSimulatePhysics(true);
- outCollison->SetCollisionProfileName(TEXT("WorldDynamic"));
- outCollison->SetCollisionEnabled(ECollisionEnabled::NoCollision);
- sphereComp = CreateDefaultSubobject
(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
(TEXT("SkeletalMeshComp")); - SkeletalMeshComp->SetGenerateOverlapEvents(true);
- SkeletalMeshComp->SetSimulatePhysics(true);
- springArmComp = CreateDefaultSubobject
(TEXT("SpringArm")); - springArmComp->SetupAttachment(RootComponent);
- cameraComp = CreateDefaultSubobject
(TEXT("Camera")); - cameraComp->SetupAttachment(springArmComp);
- upThrusterComp = CreateDefaultSubobject
(TEXT("upThrusterComp")); - upThrusterComp->SetupAttachment(RootComponent);
- upThrusterComp->SetWorldRotation(UKismetMathLibrary::MakeRotFromX(FVector(-this->GetActorUpVector())));
- upThrusterComp->ThrustStrength = 980.0f;
- upThrusterComp->SetAutoActivate(true);
- CountdownText = CreateDefaultSubobject
(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
(TEXT("projectileMovement")); - projectileMovementComp->InitialSpeed = 500.0f;