0


UE5中简易的UI管理框架(c++版,UE5.1)

需求说明:

在UE项目开发中,当UI界面较多的时候,就需要有一个管理功能出现,负责UI的生成、销毁、禁用等功能。

基于此需求,采用栈先进后出的数据接口,编写了一个简易的UI管理框架。

功能说明:

1.支持UI的自动创建

2.支持UI的按开启顺序关闭

3.支持一件关闭所有UI

4.支持开启当前UI后,禁用上一个UI的操作

5.管理类继承自AActor类,所以支持在面板配置UI信息

接口说明:

1.OpenUI:通过传入自定义的UI名来生成UI,如果hideLastUI为true,则隐藏上一个UI,否则,上一个UI继续显示

void OpenUI(const FString panelName,const bool hideLastUI );

2.CloseUI:关闭当前UI

void CloseUI();

3.CloseAllUI:关闭生成的所有UI

void CloseAllUI();

源码:

UIManager.h

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "UIManager.generated.h"

UCLASS()
class THIRDPERSONCPP_API AUIManager : public AActor
{
    GENERATED_BODY()
    
public:
    /// <summary>
    /// panelname,panel reference
    /// </summary>
    UPROPERTY(EditAnywhere, Category = "UIManager")
    TMap<FString, TSubclassOf<UUserWidget>> panelInfos;

    /// <summary>
    /// panelname, panel renference path
    /// </summary>
    UPROPERTY(EditAnywhere, Category = "UIManager")
    TMap<FString, FString> panelInfos2;

    TArray<UUserWidget*> panelStack;

public:    
    // Sets default values for this actor's properties
    AUIManager();
    UFUNCTION(BlueprintCallable, Category = "UIManager")
    void OpenUI(const FString panelName,const bool hideLastUI );
    UFUNCTION(BlueprintCallable, Category = "UIManager")
    void CloseUI();
    UFUNCTION(BlueprintCallable, Category = "UIManager")
    void CloseAllUI();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

public:    
    // Called every frame
    virtual void Tick(float DeltaTime) override;

};

UIManager.cpp

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

#include "UIManager.h"
#include "Blueprint/UserWidget.h"
#include "MyBlueprintFunctionLibrary.h"
// Sets default values
AUIManager::AUIManager()
{
     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;

}

void AUIManager::OpenUI(const FString panelName,const bool hideLastUI = true)
{
    
    if (panelInfos.Contains(panelName) == false) return;
    
    TSubclassOf<UUserWidget> t =panelInfos[panelName];
    if (t == false)
    {
        UE_LOG(LogTemp, Error, TEXT("Can't  find UI refence, please check"));
        return;
    }

    //如果是第一次
    if (panelStack.Num() == 0)
    {
        //GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, FString::Printf(TEXT("A : %f"), AA));
        // //UE_LOG(LogTemp, Warning, TEXT("ThreadName::::::::::::%s"), *ThreadName);
        //直接生成,入栈
        //根据对象生成
        UUserWidget* temp = CreateWidget<UUserWidget>(GetWorld(), *panelInfos[panelName]);
        //根据路径生成
        //TSubclassOf<UUserWidget> clss = LoadClass<UUserWidget>(this, *panelInfos2[panelName]);
        //UUserWidget* umg = CreateWidget<UUserWidget>(GetWorld(), clss);
        temp->AddToViewport();
        panelStack.Push(temp);
        return;
    }

    UUserWidget* last = panelStack[panelStack.Num() - 1];
    if (hideLastUI)
    {
        last->SetVisibility(ESlateVisibility::Hidden);
    }
    last->SetIsEnabled(false);

    UUserWidget* temp = CreateWidget<UUserWidget>(GetWorld(), *panelInfos[panelName]);
    temp->AddToViewport();
    panelStack.Push(temp);

}

void AUIManager::CloseUI()
{
    if (panelStack.Num() == 0)return;
    UUserWidget* umg = panelStack.Pop();
    umg->RemoveFromViewport();

    if (panelStack.Num() == 0)return;
    UUserWidget* newUMG = panelStack[panelStack.Num() - 1];
    newUMG->SetVisibility(ESlateVisibility::Visible);
    newUMG->SetIsEnabled(true);

}

void AUIManager::CloseAllUI()
{
    while (panelStack.Num() > 0)
    {
        UUserWidget* umg = panelStack.Pop();
        umg->RemoveFromViewport();
    }
}

// Called when the game starts or when spawned
void AUIManager::BeginPlay()
{
    Super::BeginPlay();
    
}

// Called every frame
void AUIManager::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

}

使用教程:

1.新建UIManager c++类(参照上述代码)
2.基于UIManager类新建蓝图对象
![](https://img-blog.csdnimg.cn/direct/a543482f33434556a1c0c6932f61a470.png)
3.将A_UIManager蓝图对象拖放到关卡中

4.配置UI信息

在Panel Infos中添加ui信息,key是你自定义的UI名,value是这个UI的对象

5.蓝图调用

在任意位置都可以调用,打开UI时,通过传入你配置的UI名,和设置是否隐藏上一个打开的UI即可

关闭单个UI时,无需传参,直接关闭即可

关闭所有UI时,无需传参,直接关闭即可

标签: ue5 ui

本文转载自: https://blog.csdn.net/u010804261/article/details/136929680
版权归原作者 半天法师 所有, 如有侵权,请联系我们删除。

“UE5中简易的UI管理框架(c++版,UE5.1)”的评论:

还没有评论