Unreal

[Unreal5] Component 추가 및 속성 변경

usingsystem 2024. 5. 30. 21:56
728x90

구조

헤더

	UPROPERTY(Category = Character, VisibleAnywhere, BlueprintReadOnly)
	TObjectPtr<class UCapsuleComponent> CapsuleComponent;
	
	UPROPERTY(Category = Character, VisibleAnywhere, BlueprintReadOnly)
	TObjectPtr<class USkeletalMeshComponent> Mesh;

	UPROPERTY( VisibleAnywhere, BlueprintReadOnly)
	TObjectPtr<class USpringArmComponent> SpringArm;

	UPROPERTY( VisibleAnywhere, BlueprintReadOnly)
	TObjectPtr<class UCameraComponent> Camera;

CPP

CreateDefaultSubobject는 클래스의 서브 오브젝트를 생성하는 데 사용되는 함수입니다. 이 함수는 주로 Actor나 Component 클래스의 생성자에서 호출되며, 게임 오브젝트의 초기 구성 요소를 설정할 수 있습니다.

주요 개념

  1. 기본 서브 오브젝트(Default Subobject): 게임 오브젝트가 생성될 때 자동으로 생성되고 초기화되는 서브 오브젝트입니다. 예를 들어, 캐릭터 클래스는 기본적으로 스켈레탈 메시 컴포넌트, 카메라 컴포넌트 등을 가질 수 있습니다.
  2. 생성 위치(Constructor): CreateDefaultSubobject는 일반적으로 클래스의 생성자에서 호출됩니다. 생성자는 클래스가 인스턴스화될 때 호출되므로, 기본 서브 오브젝트는 클래스 인스턴스가 생성될 때 함께 생성됩니다.
#include "Components/CapsuleComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"

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

	CapsuleComponent = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Capsule"));
	CapsuleComponent->InitCapsuleSize(34.0f, 88.0f);
	RootComponent = CapsuleComponent;

	Mesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("SkeletalMesh"));
	Mesh->SetupAttachment(CapsuleComponent);
	Mesh->SetRelativeLocationAndRotation(FVector(0, 0, -88), FRotator(0, -90, 0));

	SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponenet"));
	SpringArm->SetupAttachment(CapsuleComponent);

	SpringArm->TargetArmLength = 700.f;

	Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
	Camera->SetupAttachment(SpringArm);
}
728x90