UE4 SetVisibility()和SetHiddenInGame()的比较

区别与联系:SetVisibility(实现的更加广泛一些,而SetHiddenInGame()则是只在SceneComponent中有实现,意味着SetHiddenInGame()只能隐藏SceneComponent。SetVisibility()可以隐藏包括SceneComponent在内的很多东西(如UI组件)。一般来说能在场景中显示(看的见的)物体,都有SceneComponent,两种方法都可以达到目的,但是如果是一些SWeiget,SPanel,UWeiget等就只能通过SetVisibility()来做。
另外,在SceneComponent中两种方法的实现几乎是相同的。
void USceneComponent::SetVisibility(const bool bNewVisibility, const USceneComponent::EVisibilityPropagation PropagateToChildren)
{
    bool bRecurseChildren = (PropagateToChildren == EVisibilityPropagation::Propagate);
    if ( bNewVisibility != GetVisibleFlag() )
    {
        bRecurseChildren = bRecurseChildren || (PropagateToChildren == EVisibilityPropagation::DirtyOnly);
        SetVisibleFlag(bNewVisibility);
        OnVisibilityChanged();
    }

    const TArray<USceneComponent*>& AttachedChildren = GetAttachChildren();
    if (bRecurseChildren && AttachedChildren.Num() > 0)
    {
        // fully traverse down the attachment tree
        // we do it entirely inline here instead of recursing in case a primitivecomponent is a child of a non-primitivecomponent
        TInlineComponentArray<USceneComponent*, NumInlinedActorComponents> ComponentStack;

        // prime the pump
        ComponentStack.Append(AttachedChildren);

        while (ComponentStack.Num() > 0)
        {
            USceneComponent* const CurrentComp = ComponentStack.Pop(/*bAllowShrinking=*/ false);
            if (CurrentComp)
            {
                ComponentStack.Append(CurrentComp->GetAttachChildren());

                if (PropagateToChildren == EVisibilityPropagation::Propagate)
                {
                    CurrentComp->SetVisibility(bNewVisibility, EVisibilityPropagation::NoPropagation);
                }

                // Render state must be dirtied if any parent component's visibility has changed. Since we can't easily track whether 
                // any parent in the hierarchy was dirtied, we have to mark dirty always.
                CurrentComp->MarkRenderStateDirty();
            }
        }
    }
}
void USceneComponent::SetHiddenInGame(const bool bNewHiddenGame, const USceneComponent::EVisibilityPropagation PropagateToChildren)
{
    bool bRecurseChildren = (PropagateToChildren == EVisibilityPropagation::Propagate);
    if ( bNewHiddenGame != bHiddenInGame )
    {
        bRecurseChildren = bRecurseChildren || (PropagateToChildren == EVisibilityPropagation::DirtyOnly);
        bHiddenInGame = bNewHiddenGame;
        OnHiddenInGameChanged();
    }

    const TArray<USceneComponent*>& AttachedChildren = GetAttachChildren();
    if (bRecurseChildren && AttachedChildren.Num() > 0)
    {
        // fully traverse down the attachment tree
        // we do it entirely inline here instead of recursing in case a primitivecomponent is a child of a non-primitivecomponent
        TInlineComponentArray<USceneComponent*, NumInlinedActorComponents> ComponentStack;

        // prime the pump
        ComponentStack.Append(AttachedChildren);

        while (ComponentStack.Num() > 0)
        {
            USceneComponent* const CurrentComp = ComponentStack.Pop(/*bAllowShrinking=*/ false);
            if (CurrentComp)
            {
                ComponentStack.Append(CurrentComp->GetAttachChildren());

                if (PropagateToChildren == EVisibilityPropagation::Propagate)
                {
                    CurrentComp->SetHiddenInGame(bNewHiddenGame, EVisibilityPropagation::NoPropagation);
                }

                // Render state must be dirtied if any parent component's visibility has changed. Since we can't easily track whether 
                // any parent in the hierarchy was dirtied, we have to mark dirty always.
                CurrentComp->MarkRenderStateDirty();
            }
        }
    }
}

 

posted @ 2020-12-30 20:36  yocichen  阅读(1285)  评论(0编辑  收藏  举报