언리얼 5

230922

슬뷔 2023. 9. 22. 10:47

오늘 할 것

1. 아이템창 켜놓은 상태에서 아이템 먹었을 때, 개수 실시간 반영

UCLASS()
class UNREAL_3TH_API UInvenMgr : public UObject
{
	GENERATED_BODY()

private:
	void ResetWidget();
};
void UInvenMgr::ResetWidget()
{
	// 현재 게임모드를 체크
	AUnreal_3thGameModeBase* pGameMode = Cast<AUnreal_3thGameModeBase>(UGameplayStatics::GetGameMode(m_World));
	if (!IsValid(pGameMode))
		return;

	// 게임모드로부터 메인 HUD 가져오기
	UMainHUD_Base* MainHUD = pGameMode->GetMainHUD();

	// MainHUD 로부터 InventoryWidget(UI) 가져오기
	UInventory_Base* InventoryWidget = MainHUD->GetInventoryWidget();

	// InventoryWidget(UI) 이전 내용 클리어하기
	InventoryWidget->Clear();

	// InvenMgr 가 보유하고 있는 아이템 목록을 전부 UI로 보내기
	for (int32 i = 0; (int32)EITEM_TYPE::END; ++i)
	{
		for (auto Iterator = m_BackPack[i].CreateConstIterator(); Iterator; ++Iterator)
		{
			UInvenItemData* pData = NewObject<UInvenItemData>();
			pData->SetIconImgPath(Iterator.Value().ItemInfo->IconPath);
			pData->SetItemName(Iterator.Value().ItemInfo->Description);
			pData->SetItemCount(Iterator.Value().Stack);

			InventoryWidget->AddItem(pData);
		}
	}
}

결과

2. 게임모드여서 마우스 노출이 안됨 => 인벤토리창이 켜졌을 때는 ui모드로 변경 하는 구조 추가

class UNREAL_3TH_API UMainHUD_Base : public UUserWidget
{
	GENERATED_BODY()

public:
	bool IsInventoryOpen();
}
bool UMainHUD_Base::IsInventoryOpen()
{
	if (ESlateVisibility::Visible == m_Inventory->GetVisibility())
		return true;
	else
		return false;
}
void UInvenMgr::OpenInventoryUI()
{
	// 현재 게임모드를 체크
	AUnreal_3thGameModeBase* pGameMode = Cast<AUnreal_3thGameModeBase>(UGameplayStatics::GetGameMode(m_World));
	if (!IsValid(pGameMode))
		return;

	// 게임모드로부터 메인 HUD 가져오기
	UMainHUD_Base* MainHUD = pGameMode->GetMainHUD();

	// 인벤토리 내용으로 위젯을 갱신
	ResetWidget();

	// InventoryWidget Visible
	MainHUD->ShowInventoryUI(true);

	// 플레이어 컨트롤러의 입력모드를 UI 모드로 전환한다.
	APlayerController* pController = m_World->GetFirstPlayerController();

	FInputModeUIOnly uionly{};
	pController->SetInputMode(uionly);
	pController->bShowMouseCursor = true;
}

결과

3. I 키를 눌렀을 때 창을 키고, I키를 다시 눌렀을 때 창을 끄는 기능 (아이템창 On/Off 기능)

UCLASS()
class UNREAL_3TH_API UInvenMgr : public UObject
{
	GENERATED_BODY()

public:
	UFUNCTION(BlueprintCallable)
	void CloseInventoryUI();
};
void UInvenMgr::CloseInventoryUI()
{
	// 현재 게임모드를 체크
	AUnreal_3thGameModeBase* pGameMode = Cast<AUnreal_3thGameModeBase>(UGameplayStatics::GetGameMode(m_World));
	if (!IsValid(pGameMode))
		return;

	// 게임모드로부터 메인 HUD 가져오기
	UMainHUD_Base* MainHUD = pGameMode->GetMainHUD();

	// InventoryWidget Visible == hidden
	MainHUD->ShowInventoryUI(false);
}

게임모드에서는 widget 입력이 비활성화 되지만, UI 모드가 켜졌을 때는 widget 입력이 활성화 된다

 

블루프린트

오버라이드 -> on key down

단점 -> 인벤토리창에 포커싱이 되어야 I 키를 눌렀을 때 인벤토리 창이 꺼짐

 

영상

 

해결방법

MainHUD

 

4. UI 모드 -> 게임모드 로 변경

void UInvenMgr::CloseInventoryUI()
{
	// 플레이어 컨트롤러의 입력모드를 게임 모드로 전환한다.
	APlayerController* pController = m_World->GetFirstPlayerController();

	FInputModeGameOnly gameonly{};
	pController->SetInputMode(gameonly);
	pController->bShowMouseCursor = false;
}