1. C++ UUserWidget Class 생성

UCLASS()
class KGAME_API UKHUDWidget : public UUserWidget
{
	GENERATED_BODY()
public:
	void BindCharacterStat(class UKCharacterStatComponent* CharacterStat);
	void UpdatePlayerState();
private:
	TWeakObjectPtr<class UKCharacterStatComponent> CurrentCharacterStat;
	UPROPERTY()
		class UTextBlock* MaxAmmoText;

	UPROPERTY()
		class UTextBlock* CurAmmoText;
}
        
        




클래스 생성 후 빌드를 한다

그리고 위젯을 생성 후 디폴트 클래스를 설정해 준다

myHUD widget 생성

 

디폴트 설정


그리고 사용할 팔레트를 만든다

 

widget 생성


widget의 LeftAmmoText와 RightAmmoText 에 데이터를 바인딩 예정

 

2.   AddViewPort


HUD 는 컨트롤러에 장착시켜 보일려고 구현

////h
UCLASS()
class KGAME_API AKPlayerController : public APlayerController
{

    UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = UI)
		TSubclassOf<class UKHUDWidget> HUDWidgetClass;

	UPROPERTY()
		class UKHUDWidget* HUDWidget;
}

////cpp
AKPlayerController::AKPlayerController()
{
	static ConstructorHelpers::FClassFinder<UKHUDWidget> UI_HUD_C(TEXT("WidgetBlueprint'/Game/_WIDGET/myHUD.myHUD_C'"));
	if (UI_HUD_C.Succeeded())
	{
		HUDWidgetClass = UI_HUD_C.Class;
	}
}

void AKPlayerController::BeginPlay()
{

	HUDWidget = CreateWidget<UKHUDWidget>(this, HUDWidgetClass);
	HUDWidget->AddToViewport(1);

}





위젯이 컨트롤러에 추가된 화면

 

3. C++ 데이터 바인딩

void UKHUDWidget::NativeConstruct()
{
	Super::NativeConstruct();
	CurAmmoText = Cast<UTextBlock>(GetWidgetFromName(TEXT("LeftAmmoText")));
	MaxAmmoText = Cast<UTextBlock>(GetWidgetFromName(TEXT("RightAmmoText")));
}

 



4. C++ 데이터 갱신 (델리게이트)

 

void UKHUDWidget::BindCharacterStat(class UKCharacterStatComponent* CharacterStat)
{
	KCHECK(nullptr != CharacterStat);
	CurrentCharacterStat = CharacterStat;
	
	CharacterStat->OnAmmoChange.AddUObject(this, &UKHUDWidget::UpdatePlayerState);
}

void UKHUDWidget::UpdatePlayerState()
{
	if (CurrentCharacterStat.IsValid()==true)
	{
		CurAmmoText->SetText(FText::FromString(FString::FromInt(wep->GetCurAmmo())));
		MaxAmmoText->SetText(FText::FromString(FString::FromInt(wep->GetMaxAmmo())));
	}
};




 


 

 

 

 

 

'Unreal > Game 2 ' 카테고리의 다른 글

10. AI 만들기  (0) 2019.07.01
9.3 PlayerState - 작성 중  (0) 2019.06.30
9.1 StatComponent  (0) 2019.06.30
9. GameInstace 및 DataTable  (0) 2019.06.27
8.3 Screen Center Aim Shooting  (0) 2019.06.25

+ Recent posts