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();
            }
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM