How can I get the size of a widget in UMG?

I want to custom a scrollbox ,but I can not get the size of my scrollbox and the widget I put in it, I guess "I just want to get the size of a widget " that mey be a simple question, please! who can help me? and forgive my bad english.

Hello, libohan1312

To do this, you can use UUserWidget::SetDesiredSizeInViewport function:

UFUNCTION(BlueprintCallable, BlueprintCosmetic, Category="User Interface|Viewport")
void SetDesiredSizeInViewport (FVector2D Size)

Hope this helped!

Good luck!

Thinks , do you mean that get the size manually? can I get it dynamic in the running time? I hava extended the scrollbox, I want to get the size of the widget added in it.

Hey Libohan,

I may have found a way to get the size of a widget but its a little long winded to get there. Basically put your widget within a container, like a vertical or horizontal box. Make that container a variable so you can get it’s size.

I hope this helps :slight_smile:

3 Likes

yes! I think it works , I find only canvas solt can get its childrens size, thanks.

this works perfect

Best way in cpp is

GetCachedGeometry().GetLocalSize()

Or in BPs :

338594-untitled.png

12 Likes

I cannot open the BP link :frowning:

Another way, one that worked best for me, was to use the “Get Scroll Offset Of End” node from my scrollbox and use that as a clamp on the offset I applied via my input.

Input * scrollSpeed + GetScrollOffset (current offset) → Clamped between 0 (top of scrollbox) and GetScrollOffsetOfEnd (end of scrollbox) → pipe return into SetScrollOffset node.

Blueprint : Get Cached Geometry (returns a Geometry), Get Local Size (takes in a geometry) …

1 Like

Really nice one, thanks

Hello @anonymous_user_8e2766aa
Can you tell me how you can do that ?
Is it a node in a blueprint or it’s C++ ?

Thanks for your help
JD

It is works only after 1-2 frames after widget construction

2 Likes

In c++ you can use:
FVector2D WidgetSize = Widget->GetDesiredSize();

But if this widget is created in this frame you should wait frame before you be abble to use it. Other way you get WidgetSize = { 0 , 0 }.

To wait frame you can for example use FTimerHandle.

(1.) In .h file add to your UCLASS

  • bool HasWaitedFrame { false };
  • And add function void DoSomethingInNextFrame();

(2.) .cpp

void UYourClass ::DoSomethingInNextFrame();
{
   if ( ! HasWaitedFrame )
   {	
     FTimerHandle timerhandler;
     GetWorld() ->GetTimerManager() .SetTimer( timerhandler , [ this ]() { HasWaitedFrame = true; DoSomethingInNextFrame( ); } , 0.01f , false );	
   }
   else
   {
      FVector2D WidgetSize = Widget ->GetDesiredSize();
      // Do something more
   }

(3) All you must do is call this function when you want to DoSomethingInNextFrame