using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Unity.Entities;
using VContainer;
using VContainer.Unity;
public class NewTestScript
{
private static IEnumerable<World> Worlds
{
get
{
foreach (var world in World.All)
{
yield return world;
}
}
}
public static IEnumerable<TestCaseData> TestCases => new[]
{
new TestCaseData((Action<IContainerBuilder>)(builder =>
{
builder.RegisterNewWorld("test", Lifetime.Scoped);
builder.RegisterSystemIntoWorld<TestSystem>("test");
builder.Register<Test1>(Lifetime.Transient);
})).SetName("New World"),
new TestCaseData((Action<IContainerBuilder>)(builder =>
{
builder.RegisterSystemIntoDefaultWorld<TestSystem>();
builder.Register<Test1>(Lifetime.Transient);
})).SetName("Default World"),
};
[Test]
[TestCaseSource(nameof(TestCases))]
public void EntitySystemLifetime(Action<IContainerBuilder> configuration)
{
using (var scope = LifetimeScope.Create(configuration))
{
Assert.NotNull(scope.Container.Resolve(typeof(Test1)));
Assert.IsFalse(Worlds.All(world => world.GetExistingSystem<TestSystem>() == SystemHandle.Null), "generated");
}
// After exiting LifetimeScope, it is assumed that the items registered in the container will be deleted.
Assert.IsTrue(Worlds.All(world => world.GetExistingSystem<TestSystem>() == SystemHandle.Null), "LifetimeScope exited.");
}
}
[DisableAutoCreation]
public partial class TestSystem : SystemBase
{
protected override void OnUpdate()
{
}
}
public class Test1
{
[Inject]
public Test1(TestSystem testSystem) { }
}
The lifespan of a System appears to be tied to the World, not the container.
I ran the code below in Play Mode test:
The lifespan of a System appears to be tied to the World, not the container.
Versions