27 - UMG , Inventory UI

Overcle·2023년 3월 9일
0

학원

목록 보기
20/29

요약
1.

용어 설명
1. LoadObject :
여러개를 로드 했더라도 경로가 같다면. 여러번 선언 하더라도 메모리는 1개만 사용한다.

Tip
1.

UI_Inventory


1. 사용자 위젯기반 "UI_Inventory" 생성.

2. 스케일, 크기, 캔버스 추가

3. Costom 사이즈 설정

4. 크기 박스 사이즈 설정

5. 목록 뷰 추가.

6. 목록 뷰 위치, 크기 설정

7. 목록 뷰에 들어갈 아이템 정보 위젯 생성

8. Custom 사이즈 설정

9. 스케일, 크기, 캔버스 추가 후 보더, Icon 이미지 추가

10. Icon(Image) 크기 설정.

11. 보더 크기 설정

12. 보더 색상 설정.

13. 텍스트 추가

14. 텍스트 1개 더 추가해서 위치는 알아서 설정

15. Inventory에 추가하기 위해 오브젝트 설정

15-1. 그래프 > 클래스 세팅 > 인터페이스 > 추가 > UserObjectListEntry 추가


16. Inventory에서 리스트 엔트리 추가

17. Inventory BackGround 색상 설정

18. MainHUD에 Inventory 추가

19. Inventory를 관리할 클래스 추가.

InventoryBase.h

#include "../GameInfo.h"
#include <Components/ListView.h>
#include "Blueprint/UserWidget.h"
#include "InventoryBase.generated.h"

UCLASS()
class UE11_API UInventoryBase : public UUserWidget
{
	GENERATED_BODY()
	
private:
	UListView* m_Listview;

public:
	virtual void NativeConstruct() override;
	virtual void NativeTick(const FGeometry& _geo, float _DeltaTime) override;
};

Inventory.cpp

void UInventoryBase::NativeConstruct()
{
	Super::NativeConstruct();

	m_Listview = Cast<UListView>(GetWidgetFromName(FName("ListView")));
	m_Listview->SetVisibility(ESlateVisibility::Visible);

}

void UInventoryBase::NativeTick(const FGeometry& _geo, float _DeltaTime)
{
	Super::NativeTick(_geo, _DeltaTime);
}

20. Inventory 부모 클래스 설정

Inventory Item 설정



InventoryItemBase.h


UCLASS()
class UE11_API UItemDataBase : public UObject
{
	GENERATED_BODY()
	

private:
	FString		m_IconPath;		// 아이콘 이미지 경로
	FString		m_Description;	// 아이템 설명
	int32		m_ItemCount;	// 아이템 수량


public:
	void SetIconPath(const FString& _IconPath) { m_IconPath = _IconPath; }
	const FString& GetIconPath() { return m_IconPath; }

	void SetItemDesc(const FString& _Desc) { m_Description = _Desc; }
	const FString& GetItemDesc() { return m_Description; }

	void SetItemCount(const int32 _ItemCount) { m_ItemCount = _ItemCount; }
	const int32 GetItemCount() { return m_ItemCount; }


public:
	UItemDataBase();
};

InventoryBase.cpp

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


#include "InventoryBase.h"
#include "ItemDataBase.h"

void UInventoryBase::NativeConstruct()
{
	Super::NativeConstruct();

	m_Listview = Cast<UListView>(GetWidgetFromName(FName("ListView")));
	m_Listview->SetVisibility(ESlateVisibility::Visible);


	UItemDataBase* pNewData = NewObject<UItemDataBase>();

	pNewData->SetIconPath(TEXT("/Script/Engine.Texture2D'/Game/Viking_RPG_UI_5_0/Buttons/Standart_buttons/Flat_Icon_13_a.Flat_Icon_13_a'"));
	pNewData->SetItemDesc(TEXT("그냥 칼임"));
	pNewData->SetItemCount(5);

	m_Listview->AddItem(pNewData);
}

void UInventoryBase::NativeTick(const FGeometry& _geo, float _DeltaTime)
{
	Super::NativeTick(_geo, _DeltaTime);
}

InventoryItemBase.h

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

#pragma once

#include <Components/Image.h>
#include <Components/TextBlock.h>

#include "../GameInfo.h"
#include "Blueprint/UserWidget.h"
#include "InventoryItemBase.generated.h"

/**
 * 
 */
UCLASS()
class UE11_API UInventoryItemBase : public UUserWidget
{
	GENERATED_BODY()

private:
	UImage* m_IconImg;
	UTextBlock* m_ItemNameText;
	UTextBlock* m_CountText;


public:
	virtual void NativeConstruct() override;
	virtual void NativeTick(const FGeometry& _geo, float _DeltaTime) override;

public:
	UFUNCTION(BlueprintCallable)
	void InitFromData(UObject* _Data);
};

InventoryItemBase.cpp

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


#include "InventoryItemBase.h"

void UInventoryItemBase::NativeConstruct()
{
	Super::NativeConstruct();

	m_IconImg = Cast<UImage>(GetWidgetFromName(FName("Icon")));
	m_ItemNameText = Cast<UTextBlock>(GetWidgetFromName(FName("ItemName")));
	m_CountText = Cast<UTextBlock>(GetWidgetFromName(FName("Count")));

}

void UInventoryItemBase::NativeTick(const FGeometry& _geo, float _DeltaTime)
{
	Super::NativeTick(_geo, _DeltaTime);

}

void UInventoryItemBase::InitFromData(UObject* _Data)
{
	UItemDataBase* pData =  Cast<UItemDataBase>(_Data);

	if (IsValid(pData))
	{
		const FString& IconPath = pData->GetIconPath();
		const FString& ItemName = pData->GetItemDesc();
		int32 ItemCount = pData->GetItemCount();

		UTexture2D* pTex2D =  LoadObject<UTexture2D>(nullptr, *IconPath);

		if (IsValid(pTex2D))
		{
			m_IconImg->SetBrushFromTexture(pTex2D);
		}


		m_ItemNameText->SetText(FText::FromString(ItemName));

		m_CountText->SetText(FText::FromString(FString::Printf(TEXT("%d"), ItemCount)));
	}

}

InventoryItemBase를 목록뷰를 가지고있는 위젯 클래스로 지정.

UI_InventoryItem 'On List Item Object Set' 더블클릭 후 함수 연결

결과


ItemDataTable


앞서 만든 데이터를 코드가 아닌 테이블로 관리 방법

1. Table의 틀 생성. GameInfo.h 수정

Item의 옵션을 전부 선언해서 필요한 부분만 할당하는 방식으로 진행

GameInfo.h

//Item 정보
UENUM(BlueprintType)
enum class EITEM_TYPE : uint8
{
	EQUIP_WEAPON,
	EQUIP_ARMOR,
	EQUIP_AACCESARY,

	CONSUMABLE,
	QUEST
};

UENUM(BlueprintType)
enum class EITEM_ID : uint8
{
	CI_POTION,
	CI_POTION_MID,
	CI_POTION_MEGA,

	EW_GREATEWORD,
	EW_LOWGBOW,
	EW_POISONDAGGER,

	EA_LEATHERARMOR,
	EA_KNIGHTARMOR,

	AC_RING,
	AC_KECKLESS,
};


USTRUCT(Atomic, BlueprintType)
struct FItemDataInfo : public FTableRowBase
{
	GENERATED_BODY()

public:
	UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = true))
		EITEM_ID	ID;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = true))
		EITEM_TYPE	ItemType;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = true))
		FString		ItemName;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = true))
		FString		Description;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = true))
		FString		IconPath;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = true))
		float		HPHeal;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = true))
		float	MPHeal;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = true))
		float	Att;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = true))
		float	Def;
};

2. Table 생성



다음 내용은 다음글에.

profile
게임 프로그래머 지망생의 발자취

0개의 댓글