게임 엔진/Unreal

[Unreal] [Editor] [Example] Slate 예제 1

2020. 6. 22. 11:50

Custom Slate Class 만들기

Slate를 이용한 커스텀 클래스 ui 제작 예제이다.

참고 영상 https://www.youtube.com/watch?v=jeK6DPB5weA

 

MyPlugin.Build.cs

...
PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
...

 

 

SMainMenuWidget.h

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

#pragma once

#include "SlateBasics.h"
#include "SlateExtras.h"

class MENU_API SMainMenuWidget : public SCompoundWidget
{
public:

	SLATE_BEGIN_ARGS(SMainMenuWidget) {}
	
	// Construct 시 OwningHUD의 대한 Argument를 앞에 under bar가 붙은 형태로 생성해준다. ex) InArgs._OwningHUD
	SLATE_ARGUMENT(TWeakObjectPtr<class AMenuHUD>, OwningHUD)
	SLATE_ARGUMENT(float, TempVal)
	SLATE_END_ARGS()

	// slate class는 생성자와 소멸자를 사용하지 않고 Construct를 사용하여 생성시키도록 한다.
	void Construct(const FArguments& InArgs);
	virtual bool SupportsKeyboardFocus() const override { return true; }
	FReply OnQuitClicked() const;


private:
	TWeakObjectPtr<class AMenuHUD> OwningHUD;
	float TempVal;

};

 

 

SMainMenuWidget.cpp

#include "SMainMenuWidget.h"
#include "MenuHUD.h"
#include "GameFramework/PlayerController.h"

// localize for all languages
#define LOCTEXT_NAMESPACE "MainMenu"

void SMainMenuWidget::Construct(const FArguments & InArgs)
{
	// 생성될 때 받은 argument들이 InArgs를 통해 들어온다.
	// InArgs._TempVal
	// InArgs._OwningHUD

	OwningHUD = InArgs._OwningHUD;

	const FMargin ContentPadding = FMargin(500.f, 300.f);
	const FMargin ButtonPadding = FMargin(10.f);

	// localize "My Title Name" by key "GameTitle"
	const FText TitleText = LOCTEXT("GameTitle", "My Title Name");
	const FText QuitText = LOCTEXT("QuitGame", "Quit Game");

	// get unreal core style by FCoreStyle
	// FCoreStyle 를 통해 언리얼 코어 스타일을 사용할 수 있다.
	FSlateFontInfo ButtonTextStyle = FCoreStyle::Get().GetFontStyle("EmbossedText");
	ButtonTextStyle.Size = 40.f;

	FSlateFontInfo TitleTextStyle = ButtonTextStyle;
	TitleTextStyle.Size = 40.f;

	ChildSlot
	[
		SNew(SOverlay)
		+ SOverlay::Slot()
		.HAlign(HAlign_Fill)
		.VAlign(VAlign_Fill)
		[
			SNew(SImage)
			.ColorAndOpacity(FColor::Black)
		]
		+ SOverlay::Slot()
		.HAlign(HAlign_Fill)
		.VAlign(VAlign_Fill)
		.Padding(ContentPadding)
		[
			SNew(SVerticalBox)
			+ SVerticalBox::Slot()
			[
				SNew(STextBlock)
				.Font(TitleTextStyle)
				.Text(TitleText)
				.Justification(ETextJustify::Center)
			]
			+ SVerticalBox::Slot()
			.Padding(ButtonPadding)
			[
				SNew(SButton)
				.OnClicked(this, &SMainMenuWidget::OnQuitClicked)
				[
					SNew(STextBlock)
					.Font(ButtonTextStyle)
					.Text(QuitText)
					.Justification(ETextJustify::Center)
				]
			]
		]

	];
}

FReply SMainMenuWidget::OnQuitClicked() const
{
	if (OwningHUD.IsValid())
	{
		if (APlayerController* PC = OwningHUD->PlayerOwner)
		{
			PC->ConsoleCommand("quit");
		}
	}

	return FReply::Handled();
}

#undef LOCTEXT_NAMESPACE

 

 

MainHUD.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/HUD.h"
#include "MenuHUD.generated.h"

UCLASS()
class MENU_API AMenuHUD : public AHUD
{
	GENERATED_BODY()
	
protected:
	TSharedPtr<class SMainMenuWidget> MenuWidget;
	TSharedPtr<class SWidget> MenuWidgetContainer;
	
	virtual void BeginPlay() override;
};

 

 

MainHUD.cpp

#include "MenuHUD.h"

#include "SMainMenuWidget.h"
#include "Widgets/SWeakWidget.h"
#include "Engine/Engine.h"

void AMenuHUD::BeginPlay()
{
	Super::BeginPlay();

	if (GEngine && GEngine->GameViewport)
	{
		// slate 생성
		// .OwningHUD(this).TempVal(5)를 통해 Construct 함수의 InArgs에 파라메터를 넘겨준다.
		MenuWidget = SNew(SMainMenuWidget).OwningHUD(this).TempVal(5);
		GEngine->GameViewport->AddViewportWidgetContent(
			SAssignNew(MenuWidgetContainer, SWeakWidget).PossiblyNullContent(MenuWidget.ToSharedRef()));
	}
}

 

결과 화면

 

 

 

'게임 엔진 > Unreal' 카테고리의 다른 글

[Unreal] [Example] 객체 직렬화 방법  (0) 2020.06.25
[Unreal] [Editor] Slate를 이용한 에셋 경로 설정 다이얼로그 제작  (0) 2020.06.24
[Unreal] [Editor] [Example] Slate 예제 2  (0) 2020.06.22
[Unreal] [Editor] Unreal Plugin 제작 방법  (0) 2020.06.16
[Unreal] Animation Notify를 이용한 충돌처리 (AnimNotify, Collision)  (0) 2020.06.14
'게임 엔진/Unreal' 카테고리의 다른 글
  • [Unreal] [Example] 객체 직렬화 방법
  • [Unreal] [Editor] Slate를 이용한 에셋 경로 설정 다이얼로그 제작
  • [Unreal] [Editor] [Example] Slate 예제 2
  • [Unreal] [Editor] Unreal Plugin 제작 방법
AlgorFati
AlgorFati
algorfati@gmail.com
AlgorFati의 개발 기록algorfati@gmail.com
AlgorFati
AlgorFati의 개발 기록
AlgorFati
전체
오늘
어제
  • 분류 전체보기 (111)
    • 경제 (0)
    • 서비스 운영 (3)
    • 시행착오 (1)
    • 프로그래밍 (50)
      • CS 개념 (3)
      • 알고리즘 (32)
      • C++ (7)
      • C# (3)
      • OS (1)
      • Python (4)
    • 사업 (0)
    • Back End (1)
    • 생활 (1)
    • 정보 보안 (0)
    • 게임 개발 (1)
    • Tool (2)
    • 기술 (0)
    • AI (2)
    • 게임 엔진 (50)
      • Unreal (36)
      • Unity (14)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 프로그래머스
  • data asset
  • asset to editor
  • 불변성
  • unity
  • Event Handling
  • geom
  • 코딩테스트
  • c#
  • Nullable
  • Unreal
  • debug draw
  • Serialize
  • 알고리즘
  • aggregate geom
  • hp bar
  • ml agent
  • 대기업
  • navmesh
  • 취업
  • 카카오
  • Slate
  • customize
  • Animation
  • property editor
  • TickGroup
  • Editor
  • Collision
  • pathfinding
  • 비동기 프로그래밍

최근 댓글

최근 글

hELLO · Designed By 정상우.
AlgorFati
[Unreal] [Editor] [Example] Slate 예제 1
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.