본문 바로가기

Unreal

[Unreal] C++ 클래스 생성 및 기초 프로퍼티 설명

h 파일

//#include "CoreMinimal.h" // 최소 기능만 선언
#include "EngineMinimal.h"
#include "GameFramework/Actor.h"
#include "Fountain.generated.h"

UCLASS()
class UNREAL_01_SETTING_API AFountain : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AFountain();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
	virtual void PostInitializeComponents() override;

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

	UPROPERTY(VisibleAnywhere)
		UStaticMeshComponent *Body;

	UPROPERTY(VisibleAnywhere)
		UStaticMeshComponent *Water;

	UPROPERTY(VisibleAnywhere)
		UPointLightComponent *Light;

	UPROPERTY(VisibleAnywhere)
		UParticleSystemComponent *Splash;

	UPROPERTY(EditAnywhere, Category=ID)
		int32 ID;


private:
	UPROPERTY(EditAnywhere, Category = Stat, Meta = (AllowPrivateAccess = true))
		float RotateSpeed;
};

 

UPROPERTY(VisibleAnywhere) : 프로퍼티 디테일 창에 보이도록 설정

UPROPERTY(EditAnywhere, Category=ID) : 수정도 가능하도록 설정, 프로퍼티 카테고리도 지정

UPROPERTY(EditAnywhere, Category = Stat, Meta = (AllowPrivateAccess = true)) : Private 속성이지만 디테일창에 보이고 수정도 가능하도록 설정

 

 

cpp 파일

// Fill out your copyright notice in the Description page of Project Settings.


#include "Fountain.h"

// Sets default values
AFountain::AFountain()
{
 	// 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"));
	Water = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Water"));
	Light = CreateDefaultSubobject<UPointLightComponent>(TEXT("LIGHT"));
	Splash = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("SPLASH"));

	RootComponent = Body;
	Water->SetupAttachment(Body);
	Light->SetupAttachment(Body);
	Splash->SetupAttachment(Body);

	Water->SetRelativeLocation(FVector(0.0f, 0.0f, 135.0f));
	Light->SetRelativeLocation(FVector(0.0f, 0.0f, 195.0f));
	Splash->SetRelativeLocation(FVector(0.0f, 0.0f, 195.0f));

	static ConstructorHelpers::FObjectFinder<UStaticMesh> SM_BODY(TEXT("해당매쉬경로"));
	if (SM_BODY.Succeeded())
	{
		Body->SetStaticMesh(SM_BODY.Object);
	}

	static ConstructorHelpers::FObjectFinder<UStaticMesh> SM_WATER(TEXT("해당매쉬경로"));
	if (SM_WATER.Succeeded())
	{
		Water->SetStaticMesh(SM_WATER.Object);
	}

	static ConstructorHelpers::FObjectFinder<UParticleSystem> SM_SPLASH(TEXT("해당파티클시스템경로"));
	if (SM_SPLASH.Succeeded())
	{
		Splash->SetTemplate(SM_SPLASH.Object);
	}

	RotateSpeed = 30.0f;
}

void AFountain::PostInitializeComponents()
{
	Super::PostInitializeComponents();
}

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

void AFountain::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
	Super::EndPlay(EndPlayReason);
}



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

	AddActorLocalRotation(FRotator(0.0f, RotateSpeed * DeltaTime, 0.0f));
}

 

해당 컴포넌트 초기화

Body = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BODY"));

해당 컴포넌트를 루트 컴포넌트로 지정.

RootComponent = Body;

현재 클래스의 하위에 해당 컴포넌트 배치함.

Water->SetupAttachment(Body);

해당 컴포넌트의 시작 위치를 지정.

Water->SetRelativeLocation(FVector(0.0f, 0.0f, 135.0f));