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

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

处理后的现象:

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

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

#pragma once

#include "CoreMinimal.h"
#include "Subsystems/GameInstanceSubsystem.h"
#include "CustomGameViewSizeSub.generated.h"

/**
 * 
 */
class FViewport;

//定义当视口大小发生变化时的代理
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnViewportResized);

UCLASS()
class CUSTOMGAMEVIEWSIZE_API UCustomGameViewSizeSub : public UGameInstanceSubsystem
{
	GENERATED_BODY()

public:

	UPROPERTY(BlueprintAssignable, Category = "Delegate")
		FOnViewportResized OnViewportResized;

public:

	void ViewportResize(FViewport* Viewport, uint32 Size);

	virtual void Initialize(FSubsystemCollectionBase& Collection) override;

	UFUNCTION(BlueprintCallable, Category = "Fun Tool")
		void AdjustViewportSize(FMargin margin);
};

 .cpp

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


#include "CustomGameViewSizeSub.h"
#include "UnrealClient.h"
#include "Engine/Engine.h"
#include "Engine/GameViewportClient.h"

void UCustomGameViewSizeSub::Initialize(FSubsystemCollectionBase& Collection)
{
    GEngine->GameViewport->Viewport->ViewportResizedEvent.AddUObject(this, &UCustomGameViewSizeSub::ViewportResize);
}

void UCustomGameViewSizeSub::ViewportResize(FViewport* Viewport, uint32 Size)
{
    OnViewportResized.Broadcast();
}

void UCustomGameViewSizeSub::AdjustViewportSize(FMargin margin)
{
    UE_LOG(LogTemp, Log, TEXT(" AdjustViewportSize "));
    if (GEngine->GameViewport)
    {
        FVector2D ViewportSize;
        GEngine->GameViewport->GetViewportSize(ViewportSize);

        if (ViewportSize.X > 0 && ViewportSize.Y > 0)
        {
            //视口大小缩放
            GEngine->GameViewport->SplitscreenInfo[0].PlayerData[0].SizeX = margin.Right / ViewportSize.X;
            GEngine->GameViewport->SplitscreenInfo[0].PlayerData[0].SizeY = margin.Bottom / ViewportSize.Y;
            //基于视口原点偏移,原点是左上角(0,0)
            GEngine->GameViewport->SplitscreenInfo[0].PlayerData[0].OriginX = margin.Left / ViewportSize.X;
            GEngine->GameViewport->SplitscreenInfo[0].PlayerData[0].OriginY = margin.Top / ViewportSize.Y;
        }
    }
}

UMG设计图:
 逻辑实现:

最终效果:
 

最近更新

  1. TCP协议是安全的吗?

    2023-12-21 07:52:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-21 07:52:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-21 07:52:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-21 07:52:02       20 阅读

热门阅读

  1. vue爷孙组件传参v-bind=“$attrs“ v-on=“$listeners“

    2023-12-21 07:52:02       32 阅读
  2. Linux|shell编程|实验总结|期末考查试题

    2023-12-21 07:52:02       29 阅读
  3. Wireshark在移动网络中的应用

    2023-12-21 07:52:02       26 阅读
  4. Spark发送到Kafka的数据出现重复问题解决方案

    2023-12-21 07:52:02       42 阅读
  5. xrandr

    2023-12-21 07:52:02       49 阅读
  6. 实用高阶函数map,reduce,filter

    2023-12-21 07:52:02       31 阅读
  7. 飞天使-k8s知识点3-卸载yum 安装的k8s

    2023-12-21 07:52:02       32 阅读
  8. SQL面试题挑战03:奖金瓜分问题(拼多多)

    2023-12-21 07:52:02       39 阅读
  9. GBDT-梯度提升决策树

    2023-12-21 07:52:02       22 阅读
  10. 前端:NPM的介绍和使用

    2023-12-21 07:52:02       40 阅读