UI를 캐릭터에 부착하는 기능을 구현
부착 할 수 있도록 UWidgetCompnent 라는 클래스를 제공함

class KGAME_API AABCharacter : public ACharacter
{
//
    UPROPERTY(VisibleAnywhere, Category = UI)
        class UWidgetComponent* HPBarWidget;
}

 

하지만 컴파일을 하면 확인 할 수 없는 외부 참조 에러메시지가 나온다
이유는 현재 프로젝트 설정에 UI 관련 엔진 모듈을 지정 안했기 때문이다
엔진 모듈은 KGame.Build.cs 에 추가할 수 있다.

//KGame.Build.cs
using UnrealBuildTool;

public class KGame : ModuleRules
{
    public KGame(ReadOnlyTargetRules Target) : base(Target)
    {
        PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;

        PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "UMG" });

        PrivateDependencyModuleNames.AddRange(new string[] {  });

        // Uncomment if you are using Slate UI
        // PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });

        // Uncomment if you are using online features
        // PrivateDependencyModuleNames.Add("OnlineSubsystem");

        // To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
    }
}

UMG 모듈의 Public/Components폴더에는 현재 사용 중인 WidComponent.h 파일을 구현부 헤더 파일에 추가해 컴포넌트를 생성하는 코드를 생성한다.

//cpp
#include "ABCharacter.h"
#include "ABAnimInstance.h"
#include "ABWeapon.h"
#include "ABCharacterStatComponent.h"
#include "DrawDebugHelpers.h"
#include "WidgetComponent.h"

AABCharacter::AABCharacter()
{
    HPBarWidget = CreateDefaultSubobject<UWidgetComponent>(TEXT("HPBARWIDGET"));

    HPBarWidget->SetupAttachment(GetMesh());


    HPBarWidget->SetRelativeLocation(FVector(0.0f, 0.0f, 180.0f));
    HPBarWidget->SetWidgetSpace(EWidgetSpace::Screen);
    static ConstructorHelpers::FClassFinder<UUserWidget> UI_HUD(TEXT("/Game/UI/UI_Bar.UI_Bar"));
  ///Game/Book/UI/UI_Bar.UI_Bar_C
    if (UI_HUD.Succeeded())
    {
        HPBarWidget->SetWidgetClass(UI_HUD.Class);
        HPBarWidget->SetDrawSize(FVector2D(150.0f, 50.0f));
    }

UI는 끝에 _C 를 붙여 주어야 한다?

+ Recent posts