SVisualLogger::~SVisualLogger()
{
	UWorld* World = NULL;
#if WITH_EDITOR
	FCategoryFiltersManager::Get().SavePresistentData();

	UEditorEngine *EEngine = Cast<UEditorEngine>(GEngine);
	if (GIsEditor && EEngine != NULL)
	{
		// lets use PlayWorld during PIE/Simulate and regular world from editor otherwise, to draw debug information
		World = EEngine->PlayWorld != NULL ? EEngine->PlayWorld : EEngine->GetEditorWorldContext().World();

	}
	else
#endif
	if (!GIsEditor)
	{
	World = GEngine->GetWorld();
	}

	if (World == NULL)
	{
		World = GWorld;
	}

	if (World)
	{
		for (TActorIterator<AVisualLoggerRenderingActor> It(World); It; ++It)
		{
			World->DestroyActor(*It);
		}
	}

	UDebugDrawService::Unregister(DrawOnCanvasDelegateHandle);
	VisualLoggerCanvasRenderer.Reset();

	FVisualLogger::Get().RemoveDevice(InternalDevice.Get());
	InternalDevice.Reset();
}
UWorld* FLogVisualizer::GetWorld(UObject* OptionalObject)
{
    UWorld* World = OptionalObject != nullptr ? GEngine->GetWorldFromContextObject(OptionalObject, false) : nullptr;
#if WITH_EDITOR
    if (!World && GIsEditor)
    {
        UEditorEngine *EEngine = Cast<UEditorEngine>(GEngine);
        // lets use PlayWorld during PIE/Simulate and regular world from editor otherwise, to draw debug information
        World = EEngine != nullptr && EEngine->PlayWorld != nullptr ? EEngine->PlayWorld : EEngine->GetEditorWorldContext().World();

    }
    else
#endif
        if (!World && !GIsEditor)
        {

            World = GEngine->GetWorld();
        }

    if (World == nullptr)
    {
        World = GWorld;
    }

    return World;
}