当前位置:   article > 正文

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

ue ui管理

需求说明:

在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

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include "GameFramework/Actor.h"
  5. #include "UIManager.generated.h"
  6. UCLASS()
  7. class THIRDPERSONCPP_API AUIManager : public AActor
  8. {
  9. GENERATED_BODY()
  10. public:
  11. /// <summary>
  12. /// panelname,panel reference
  13. /// </summary>
  14. UPROPERTY(EditAnywhere, Category = "UIManager")
  15. TMap<FString, TSubclassOf<UUserWidget>> panelInfos;
  16. /// <summary>
  17. /// panelname, panel renference path
  18. /// </summary>
  19. UPROPERTY(EditAnywhere, Category = "UIManager")
  20. TMap<FString, FString> panelInfos2;
  21. TArray<UUserWidget*> panelStack;
  22. public:
  23. // Sets default values for this actor's properties
  24. AUIManager();
  25. UFUNCTION(BlueprintCallable, Category = "UIManager")
  26. void OpenUI(const FString panelName,const bool hideLastUI );
  27. UFUNCTION(BlueprintCallable, Category = "UIManager")
  28. void CloseUI();
  29. UFUNCTION(BlueprintCallable, Category = "UIManager")
  30. void CloseAllUI();
  31. protected:
  32. // Called when the game starts or when spawned
  33. virtual void BeginPlay() override;
  34. public:
  35. // Called every frame
  36. virtual void Tick(float DeltaTime) override;
  37. };

UIManager.cpp

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #include "UIManager.h"
  3. #include "Blueprint/UserWidget.h"
  4. #include "MyBlueprintFunctionLibrary.h"
  5. // Sets default values
  6. AUIManager::AUIManager()
  7. {
  8. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  9. PrimaryActorTick.bCanEverTick = true;
  10. }
  11. void AUIManager::OpenUI(const FString panelName,const bool hideLastUI = true)
  12. {
  13. if (panelInfos.Contains(panelName) == false) return;
  14. TSubclassOf<UUserWidget> t =panelInfos[panelName];
  15. if (t == false)
  16. {
  17. UE_LOG(LogTemp, Error, TEXT("Can't find UI refence, please check"));
  18. return;
  19. }
  20. //如果是第一次
  21. if (panelStack.Num() == 0)
  22. {
  23. //GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Red, FString::Printf(TEXT("A : %f"), AA));
  24. // //UE_LOG(LogTemp, Warning, TEXT("ThreadName::::::::::::%s"), *ThreadName);
  25. //直接生成,入栈
  26. //根据对象生成
  27. UUserWidget* temp = CreateWidget<UUserWidget>(GetWorld(), *panelInfos[panelName]);
  28. //根据路径生成
  29. //TSubclassOf<UUserWidget> clss = LoadClass<UUserWidget>(this, *panelInfos2[panelName]);
  30. //UUserWidget* umg = CreateWidget<UUserWidget>(GetWorld(), clss);
  31. temp->AddToViewport();
  32. panelStack.Push(temp);
  33. return;
  34. }
  35. UUserWidget* last = panelStack[panelStack.Num() - 1];
  36. if (hideLastUI)
  37. {
  38. last->SetVisibility(ESlateVisibility::Hidden);
  39. }
  40. last->SetIsEnabled(false);
  41. UUserWidget* temp = CreateWidget<UUserWidget>(GetWorld(), *panelInfos[panelName]);
  42. temp->AddToViewport();
  43. panelStack.Push(temp);
  44. }
  45. void AUIManager::CloseUI()
  46. {
  47. if (panelStack.Num() == 0)return;
  48. UUserWidget* umg = panelStack.Pop();
  49. umg->RemoveFromViewport();
  50. if (panelStack.Num() == 0)return;
  51. UUserWidget* newUMG = panelStack[panelStack.Num() - 1];
  52. newUMG->SetVisibility(ESlateVisibility::Visible);
  53. newUMG->SetIsEnabled(true);
  54. }
  55. void AUIManager::CloseAllUI()
  56. {
  57. while (panelStack.Num() > 0)
  58. {
  59. UUserWidget* umg = panelStack.Pop();
  60. umg->RemoveFromViewport();
  61. }
  62. }
  63. // Called when the game starts or when spawned
  64. void AUIManager::BeginPlay()
  65. {
  66. Super::BeginPlay();
  67. }
  68. // Called every frame
  69. void AUIManager::Tick(float DeltaTime)
  70. {
  71. Super::Tick(DeltaTime);
  72. }

使用教程:

1.新建UIManager c++类(参照上述代码)
2.基于UIManager类新建蓝图对象

    

3.将A_UIManager蓝图对象拖放到关卡中

4.配置UI信息

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

5.蓝图调用

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

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/646610
推荐阅读
相关标签
  

闽ICP备14008679号