카테고리:

업데이트:


1. 자식 컴포넌트 선언하기

// UserActor.h

#pragma once

#include "EngineMinimal.h"
#include "GameFramework/Actor.h"
#include "Fountain.generated.h"

UCLASS()
class UE4_STUDY_01_API AUserActor : public AActor
{
GENERATED_BODY()

public:
// Sets default values for this actor's properties
AUserActor();

protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;

public:
// Called every frame
virtual void Tick(float DeltaTime) override;

  UPROPERTY(VisibleAnywhere) // 언리얼에서 객체를 관리하기 위한 코드
  UStaticMeshComponent* Body; // StaticMesh Component 선언하기

  UPROPERTY(VisibleAnywhere)
  UPointLightComponent* Light; // Point Light Component 선언하기

  UPROPERTY(VisibleAnywhere)
  UParticleSystemComponent* Splash; // Particle System Component 선언하기

  UPROPERTY(EditAnywhere)
    int32 ID; // int형 선언
};

※ UPROPERTY : 언리얼 엔진이 개발자가 선언한 객체를 자동으로 관리하게 만들기 위한 지정

  • VisibleAnywhere : 엔진의 Detail에서 볼 수 있지만 값 수정이 안된다. (객체를 지정한 경우 객체의 속성값은 수정이 가능하지만 객체를 수정할 수는 없습니다.)
  • EditAnywhere : 엔진의 Detail에서 수정이 가능하다.
// UserActor.cpp
#include "UserActor.h"

// Sets default values
AUserActor::AUserActor()
{
    // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;

    Body = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BODY")); // 컴포넌트 초기화하기
    Light = CreateDefaultSubobject<UPointLightComponent>(TEXT("LIGHT")); // 컴포넌트 초기화하기
    Splash = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("SPLASH")); // 컴포넌트 초기화하기

    RootComponent = Body; // Root 컴포넌트 지정하기
    Light->SetupAttachment(Body); // 자식 컴포넌트 지정하기 (매개변수: 부모 컴포넌트)
    Splash->SetupAttachment(Body);

    Light->SetRelativeLocation(FVector(0.0f, 0.0f, 195.0f)); // 상대 위치 지정하기
    Splash->SetRelativeLocation(FVector(0.0f, 0.0f, 195.0f));

    // 오브젝트 가져오기 (항상 같은 오브젝트이기 때문에 Static으로 지정해 처음 한번만 초기화)
    static ConstructorHelpers::FObjectFinder<오브젝트 타입> Finded_Obejct(TEXT("오브젝트 경로"));

    if (Finded_Obejct.Succeeded()) { // 오브젝트를 찾은경우
        Body->SetStaticMesh(Finded_Obejct.Object); // Body 오브젝트에 찾은 오브젝트 지정하기
    }
}

// Called when the game starts or when spawned
void AUserActor::BeginPlay()
{
    Super::BeginPlay();
}

// Called every frame
void AUserActor::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
}

댓글남기기