I slightly modified findwithinterface to find in viewport widgets for a specific class. DO NOT use this every frame
template < class T>
void GetUserWidgetsInviewport(UObject* WorldContextObject, TArray<UUserWidget*>& FoundWidgets)
{
//Prevent possibility of an ever-growing array if user uses this in a loop
FoundWidgets.Empty();
if (!WorldContextObject)
{
return;
}
const UWorld* const World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);
if (!World)
{
return;
}
for (TObjectIterator<UUserWidget> Itr; Itr; ++Itr)
{
UUserWidget* LiveWidget = *Itr;
// Skip any widget that's not in the current world context or that is not a child of the class specified.
if (LiveWidget->GetWorld() != World || !LiveWidget->GetClass()->IsChildOf<T>())
{
continue;
}
if (LiveWidget->IsInViewport())
FoundWidgets.Add(LiveWidget);
}
}