USlateBlueprintLibrary::LocalToViewport
#include "Blueprint/SlateBlueprintLibrary.h"
Access: public
Specifiers: staticUFUNCTIONBlueprintPure
Description
Converts a local widget coordinate into both game-viewport pixel space and UMG viewport widget space in a single call.
Caveats & Gotchas
- • Returns two distinct coordinate spaces: PixelPosition is in physical pixels and is what you need for DeprojectScreenToWorld; ViewportPosition is in the viewport widget's coordinate system and is what AddToViewport-placed widgets use for positioning.
- • If the WorldContextObject cannot resolve a valid UGameViewportClient (e.g., called from a non-game context), both outputs will be zero vectors with no error logged.
- • The results change when the viewport is resized or the DPI scale changes. Do not cache these across frames if the viewport can be resized during gameplay.
Signature
static UMG_API void LocalToViewport(const UObject* WorldContextObject, const FGeometry& Geometry, FVector2D LocalCoordinate, FVector2D& PixelPosition, FVector2D& ViewportPosition) Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| WorldContextObject | const UObject* | World context used to resolve the game viewport client. | — |
| Geometry | const FGeometry& | Geometry of the source widget. | — |
| LocalCoordinate | FVector2D | Point in the widget's local space. | — |
| PixelPosition | FVector2D& | Output: position in game viewport pixel space, suitable for line traces and DeprojectScreenToWorld. | — |
| ViewportPosition | FVector2D& | Output: position in viewport widget space, suitable for placing other UMG widgets at the same location. | — |
Return Type
void Example
Raycast from a widget's center into the world C++
FVector2D PixelPos, ViewportPos;
USlateBlueprintLibrary::LocalToViewport(
GetWorld(),
MyWidget->GetCachedGeometry(),
FVector2D(MyWidget->GetCachedGeometry().GetLocalSize()) * 0.5f,
PixelPos,
ViewportPos
);
FVector WorldOrigin, WorldDir;
UGameplayStatics::DeprojectScreenToWorld(GetWorld()->GetFirstPlayerController(), PixelPos, WorldOrigin, WorldDir); See Also
Version History
Introduced in: 4.7
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?