Get UMG Widget under mouse cursor.

For people Goolging this, also consider taking a look at FSlateApplication::LocateWindowUnderMouse

void ReportWidgetsUnderMouse()
{
	// Get a reference to the singleton instance of the slate application.
	FSlateApplication& MySlateApplication = FSlateApplication::Get();

	// Find a "widget tree path" of all widgets under the mouse cursor.
	// This path will contain not only the top-level widget, but all widgets underneath.
	// For example, if the mouse cursor was over a Button with a Text widget inside of it, then the last 
	// widget in the widget path would be the Text widget, and the next to last widget would be the Button widget.
	FWidgetPath WidgetsUnderCursor = MySlateApplication.LocateWindowUnderMouse( MySlateApplication.GetCursorPos(), MySlateApplication.GetInteractiveTopLevelWindows() );

	FString Result = TEXT( "" );
	if ( WidgetsUnderCursor.IsValid() )
	{
		Result += TEXT( " Count:" ) + FString::FromInt( WidgetsUnderCursor.Widgets.Num() );

		for ( int32 Idx = 0; Idx < WidgetsUnderCursor.Widgets.Num(); ++Idx )
		{
			FArrangedWidget& Widget = WidgetsUnderCursor.Widgets[Idx];

			Result += TEXT( " " ) + Widget.Widget->ToString();
		}
	}

	// Print out a quick summary of all widgets that were found under the mouse.
	UE_LOG( LogTemp, Warning, TEXT("%s"), *Result );
}
3 Likes