当前位置:   article > 正文

UE5 runtime模式下自定义视口大小和位置并跟随分辨率自适应缩放_ue runtime视口

ue runtime视口

本文旨在解决因UI问题导致屏幕中心位置不对的问题
处理前的现象:如果四周UI透明度都为1,那么方块的位置就不太对,没在中心

处理后的现象:

解决办法:自定义大小和视口偏移
创建一个基于子系统的类或者蓝图函数库(什么类不重要,你调的到就行)
.h

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include "Subsystems/GameInstanceSubsystem.h"
  5. #include "CustomGameViewSizeSub.generated.h"
  6. /**
  7. *
  8. */
  9. class FViewport;
  10. //定义当视口大小发生变化时的代理
  11. DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnViewportResized);
  12. UCLASS()
  13. class CUSTOMGAMEVIEWSIZE_API UCustomGameViewSizeSub : public UGameInstanceSubsystem
  14. {
  15. GENERATED_BODY()
  16. public:
  17. UPROPERTY(BlueprintAssignable, Category = "Delegate")
  18. FOnViewportResized OnViewportResized;
  19. public:
  20. void ViewportResize(FViewport* Viewport, uint32 Size);
  21. virtual void Initialize(FSubsystemCollectionBase& Collection) override;
  22. UFUNCTION(BlueprintCallable, Category = "Fun Tool")
  23. void AdjustViewportSize(FMargin margin);
  24. };

 .cpp

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #include "CustomGameViewSizeSub.h"
  3. #include "UnrealClient.h"
  4. #include "Engine/Engine.h"
  5. #include "Engine/GameViewportClient.h"
  6. void UCustomGameViewSizeSub::Initialize(FSubsystemCollectionBase& Collection)
  7. {
  8. GEngine->GameViewport->Viewport->ViewportResizedEvent.AddUObject(this, &UCustomGameViewSizeSub::ViewportResize);
  9. }
  10. void UCustomGameViewSizeSub::ViewportResize(FViewport* Viewport, uint32 Size)
  11. {
  12. OnViewportResized.Broadcast();
  13. }
  14. void UCustomGameViewSizeSub::AdjustViewportSize(FMargin margin)
  15. {
  16. UE_LOG(LogTemp, Log, TEXT(" AdjustViewportSize "));
  17. if (GEngine->GameViewport)
  18. {
  19. FVector2D ViewportSize;
  20. GEngine->GameViewport->GetViewportSize(ViewportSize);
  21. if (ViewportSize.X > 0 && ViewportSize.Y > 0)
  22. {
  23. //视口大小缩放
  24. GEngine->GameViewport->SplitscreenInfo[0].PlayerData[0].SizeX = margin.Right / ViewportSize.X;
  25. GEngine->GameViewport->SplitscreenInfo[0].PlayerData[0].SizeY = margin.Bottom / ViewportSize.Y;
  26. //基于视口原点偏移,原点是左上角(0,0)
  27. GEngine->GameViewport->SplitscreenInfo[0].PlayerData[0].OriginX = margin.Left / ViewportSize.X;
  28. GEngine->GameViewport->SplitscreenInfo[0].PlayerData[0].OriginY = margin.Top / ViewportSize.Y;
  29. }
  30. }
  31. }

UMG设计图:
 逻辑实现:

最终效果:
 

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

闽ICP备14008679号