コード例 #1
0
bool FAnimationSequenceThumbnailScene::SetAnimation(UAnimSequenceBase* InAnimation)
{
    PreviewActor->GetSkeletalMeshComponent()->OverrideMaterials.Empty();

    bool bSetSucessfully = false;

    PreviewAnimation = InAnimation;

    if (InAnimation)
    {
        if (USkeleton* Skeleton = InAnimation->GetSkeleton())
        {
            USkeletalMesh* PreviewSkeletalMesh = Skeleton->GetAssetPreviewMesh(InAnimation);

            PreviewActor->GetSkeletalMeshComponent()->SetSkeletalMesh(PreviewSkeletalMesh);

            if (PreviewSkeletalMesh)
            {
                bSetSucessfully = true;

                if (InAnimation->IsValidToPlay())
                {
                    // Handle posing the mesh at the middle of the animation
                    const float AnimPosition = InAnimation->SequenceLength / 2.f;

                    UDebugSkelMeshComponent* MeshComponent = CastChecked<UDebugSkelMeshComponent>(PreviewActor->GetSkeletalMeshComponent());

                    MeshComponent->EnablePreview(true, InAnimation);
                    MeshComponent->Play(false);
                    MeshComponent->Stop();
                    MeshComponent->SetPosition(AnimPosition, false);

                    UAnimSingleNodeInstance* SingleNodeInstance = PreviewActor->GetSkeletalMeshComponent()->GetSingleNodeInstance();
                    if (SingleNodeInstance)
                    {
                        SingleNodeInstance->UpdateMontageWeightForTimeSkip(AnimPosition);
                    }

                    PreviewActor->GetSkeletalMeshComponent()->RefreshBoneTransforms(nullptr);
                }

                PreviewActor->SetActorLocation(FVector(0, 0, 0), false);
                PreviewActor->GetSkeletalMeshComponent()->UpdateBounds();

                // Center the mesh at the world origin then offset to put it on top of the plane
                const float BoundsZOffset = GetBoundsZOffset(PreviewActor->GetSkeletalMeshComponent()->Bounds);
                PreviewActor->SetActorLocation(-PreviewActor->GetSkeletalMeshComponent()->Bounds.Origin + FVector(0, 0, BoundsZOffset), false);
                PreviewActor->GetSkeletalMeshComponent()->RecreateRenderState_Concurrent();
            }
        }
    }

    if(!bSetSucessfully)
    {
        CleanupComponentChildren(PreviewActor->GetSkeletalMeshComponent());
        PreviewActor->GetSkeletalMeshComponent()->SetAnimation(NULL);
        PreviewActor->GetSkeletalMeshComponent()->SetSkeletalMesh(nullptr);
    }

    return bSetSucessfully;
}