Skip to content

Destroy LifetimeScope in Editor #814

@Nitero

Description

@Nitero

I was writing an editor test where my sut calls Dispose on a LifetimeScope, which throws because it uses Destroy instead of DestroyImmediate.

This small override seems to fix it:

public class EditorSafeLifetimeScope : LifetimeScope
{
    public new void Dispose()
    {
        DisposeCore();
        if (this != null)
        {
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                DestroyImmediate(gameObject);
                return;
            }
#endif
            Destroy(gameObject);
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions