Hi.
I’m trying to find a way to determine if UMG is displayed on the screen or not.
The functions “isVisible”, “IsRendered” work differently.
For example, Tick is called only when the widget is active on the screen. So I want to understand how I can determine that the widget is active.
Please tell me the best way.
Thanks!
Hi,
You could use Is in Viewport.
https://docs.unrealengine.com/5.0/en-US/API/Runtime/UMG/Blueprint/UUserWidget/IsInViewport/
This only works for the main widget. For child widgets, this is always called false.
You could use Construct?
https://docs.unrealengine.com/5.0/en-US/API/Runtime/UMG/Blueprint/UUserWidget/Construct/
It will be easier if you tell us what you’re doing.
Are you adding and removing the widget as a child?
Are you adding and leaving it off the screen?
I have several nested UMGs in Widget Switcher.
I need to make sure that a specific UMG is displayed on the screen at the moment.
In this case, I can’t check it with the active index in Widget Switcher, because this Widget Switcher can be hidden from different places, but the active index will remain active.
Okay, I wrote my own function that recursively checks the activity of the current widget in the parent. It works perfectly.
Be careful, because it can affect performance if you have too many nested widgets. But I don’t think there will be any problems if there are about 100 iterations.
If you know another more correct way, please tell me. But I researched the source code of the engine and unfortunately did not see anything like that, so that widgets recursively inform child widgets that they are no longer active.
bool UWidgetFunctionLibrary::CheckWidgetIsActive(UWidget* widget)
{
if (!IsValid(widget))
{
return false;
}
while (IsValid(widget))
{
UWidget* parent = widget->GetParent();
if (!IsValid(parent) || !parent->GetCachedWidget().IsValid())
{
return widget->GetCachedWidget()->GetVisibility() == EVisibility::Visible
|| widget->GetCachedWidget()->GetVisibility() == EVisibility::HitTestInvisible
|| widget->GetCachedWidget()->GetVisibility() == EVisibility::SelfHitTestInvisible;
}
FArrangedChildren arrangedChildren(EVisibility::Visible);
parent->GetCachedWidget()->ArrangeChildren(parent->GetCachedWidget().Get()->GetPaintSpaceGeometry(), arrangedChildren);
bool wasFound = false;
for (int32 i = 0; i < arrangedChildren.Num(); i++)
{
if (arrangedChildren[i].GetWidgetPtr() == widget->GetCachedWidget().Get())
{
wasFound = true;
break;
}
}
if (!wasFound)
{
return false;
}
widget = parent;
}
return true;
}