1. 程式人生 > 實用技巧 >UE4 SetVisibility()和SetHiddenInGame()的比較

UE4 SetVisibility()和SetHiddenInGame()的比較

區別與聯絡:SetVility()實現的更加廣泛一些,而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();
            }
        }
    }
}