언리얼 엔진

Unreal Engine - 컴포넌트와 리소스 설정 및 오류 해결 과정

로안님 2025. 1. 20. 21:31

오늘은 언리얼 엔진에서 액터 클래스에 컴포넌트를 추가하고 리소스를 설정하는 방법과 그 과정에서 발생한 오류 해결 과정을 정리해 보았습니다. 액터를 레벨에 배치하여 실제로 동작하는 것까지 확인하며 학습을 마무리했습니다. 초보 개발자라면 놓치기 쉬운 헤더 파일 추가와 리소스 로드 방식을 자세히 다룹니다.


1. 기본적인 컴포넌트 생성 및 연결

목표

액터 클래스에서 다음과 같은 컴포넌트를 생성하고 계층적으로 연결:

  1. SceneRoot: 루트 컴포넌트로 설정
  2. StaticMeshComp: 정적 메시를 표시하는 컴포넌트
  3. AudioComp: 사운드를 재생하는 오디오 컴포넌트

코드

#include "Components/SceneComponent.h"
#include "Components/StaticMeshComponent.h"
#include "Components/AudioComponent.h"

AItem::AItem()
{
    // Root Component
    SceneRoot = CreateDefaultSubobject<USceneComponent>(TEXT("SceneRoot"));
    SetRootComponent(SceneRoot);

    // Static Mesh Component
    StaticMeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
    StaticMeshComp->SetupAttachment(SceneRoot);

    // Audio Component
    AudioComp = CreateDefaultSubobject<UAudioComponent>(TEXT("AudioComp"));
    AudioComp->SetupAttachment(StaticMeshComp);
}
  • 설명
    • SceneRoot: 액터의 위치와 트랜스폼(이동, 회전, 크기)을 관리.
    • StaticMeshComp: 액터에 정적 메시를 추가.
    • AudioComp: 액터에 사운드 기능을 추가.
    • SetupAttachment: 부모-자식 관계를 설정.

2. 리소스 설정 (Static Mesh와 SoundCue)

Static Mesh 설정

static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshAsset(TEXT("/Game/Resources/Props/SM_Chair.SM_Chair"));
if (MeshAsset.Succeeded())
{
    StaticMeshComp->SetStaticMesh(MeshAsset.Object);
}

SoundCue 설정

#include "Sound/SoundCue.h"

static ConstructorHelpers::FObjectFinder<USoundCue> AudioAsset(TEXT("/Game/Resources/Audio/Pickup_Coin_A_Cue.Pickup_Coin_A_Cue"));
if (AudioAsset.Succeeded())
{
    AudioComp->SetSound(AudioAsset.Object);
}

3. 문제 및 해결 과정

문제 1: "불완전한 형식 UAudioComponent 포인터 참조는 허용되지 않습니다"

원인

UAudioComponent 클래스 정의가 포함되지 않았기 때문.

해결

헤더 파일에 #include "Components/AudioComponent.h" 추가.


문제 2: "TObjectPtr<UAudioComponent>에서 USoundBase*로의 적절한 변환 함수가 없습니다"

원인

  • FObjectFinder가 UAudioComponent를 찾도록 설정했지만, 실제로 필요한 것은 USoundCue나 USoundBase 리소스.

해결

  • FObjectFinder의 타입을 USoundCue로 변경.
  • 추가적으로 #include "Sound/SoundCue.h" 헤더 파일을 포함.

문제 3: 리소스 경로 오류

원인

경로가 잘못되었거나 파일 이름이 틀렸음.

해결

  • 언리얼 에디터의 콘텐츠 브라우저에서 파일을 우클릭 → "참조 복사(Copy Reference)" 기능 사용.
  • 정확한 경로를 복사하여 코드에 붙여넣기.

4. 액터를 레벨에 배치하고 동작 확인

레벨에 배치

  1. 언리얼 에디터에서 새로 작성한 AItem 클래스를 기반으로 블루프린트 클래스를 생성.
  2. 생성한 블루프린트를 레벨에 배치.
  3. 배치된 액터의 Static MeshSoundCue가 올바르게 설정되었는지 Details 패널에서 확인.

플레이 확인

  • 플레이 버튼을 눌러 실행했을 때,
    • 배치된 정적 메시(의자)가 올바르게 표시되었고,
    • 배경에서 설정한 사운드가 정상적으로 재생되는 것을 확인.

5. 최종 코드

#include "Components/SceneComponent.h"
#include "Components/StaticMeshComponent.h"
#include "Components/AudioComponent.h"
#include "Sound/SoundCue.h"
#include "ConstructorHelpers.h"

AItem::AItem()
{
    // Root Component
    SceneRoot = CreateDefaultSubobject<USceneComponent>(TEXT("SceneRoot"));
    SetRootComponent(SceneRoot);

    // Static Mesh Component
    StaticMeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
    StaticMeshComp->SetupAttachment(SceneRoot);

    // Audio Component
    AudioComp = CreateDefaultSubobject<UAudioComponent>(TEXT("AudioComp"));
    AudioComp->SetupAttachment(StaticMeshComp);

    // Static Mesh Resource
    static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshAsset(TEXT("/Game/Resources/Props/SM_Chair.SM_Chair"));
    if (MeshAsset.Succeeded())
    {
        StaticMeshComp->SetStaticMesh(MeshAsset.Object);
    }

    // SoundCue Resource
    static ConstructorHelpers::FObjectFinder<USoundCue> AudioAsset(TEXT("/Game/Resources/Audio/Pickup_Coin_A_Cue.Pickup_Coin_A_Cue"));
    if (AudioAsset.Succeeded())
    {
        AudioComp->SetSound(AudioAsset.Object);
    }
}

6. 느낀 점

  • 언리얼 엔진에서는 컴포넌트와 리소스를 추가하는 과정에서 헤더 파일 추가리소스 경로 지정이 매우 중요하다.
  • FObjectFinder를 사용할 때는 리소스의 정확한 타입(USoundCue, UStaticMesh 등)을 사용하는 것이 핵심이다.
  • 코드 작업 후 실제로 액터를 레벨에 배치하고, 동작을 확인하는 과정에서 시각적 결과와 소리가 제대로 작동하는 것을 보며 큰 성취감을 느꼈다.

오늘은 액터에 컴포넌트를 추가하고 리소스를 설정하는 기본적인 과정과 문제를 해결하는 방법을 배웠습니다.