You need to factor in things like DPI and the viewport size when you change resolution, the same is true when you’re doing it in Blueprint. Here’s an excerpt from some of my source code, which places a SizeBox around an object in-game and factors in screen res etc.
// Call this once per-frame, on tick. To avoid re-calculation, or call it when settings / screen res changes.
FORCEINLINE void ComputePlacementParams()
// Cache some of the shared parameters
ViewportCenter = ViewportSize / 2.f;
// Root Panel Data
CanvasSize = RootPanel_Ptr->GetDesiredSize();
Divisor = CanvasSize / ViewportSize;
DPIScaling = GetDefault<UUserInterfaceSettings>()->GetDPIScaleBasedOnSize(FIntPoint(ViewportSize.X, ViewportSize.Y));
// Called on tick to set location. 'IssScreenLocation' is the central position on-screen of where I want the widget.
const FVector2D AnchorDivided = ((IssScreenLocation - ((ISSTrackerSlot_Ptr->GetSize() * DPIScaling) / 2.f)) * Divisor) / CanvasSize;
const FAnchors NewAnchors = FAnchors(AnchorDivided.X, AnchorDivided.Y, AnchorDivided.X, AnchorDivided.Y);
In this case, ISSTrackerSlot_Ptr is a UCanvasPanelSlot.