Пример #1
0
        public void TestDynamicPoolApplyAndPersist([Values(InstantiationType.Manager, InstantiationType.Command, InstantiationType.ParallelCommand)] InstantiationType instantiationType,
                                                   [Values(1, 2, 3, 444, 5555)] int total, [Values(false, true)] bool groupedJobs)
        {
            // Prepare
            PersistencySettings settings = CreateTestSettings();

            settings.ForceUseGroupedJobsInEditor   = groupedJobs;
            settings.ForceUseNonGroupedJobsInBuild = !groupedJobs;
            Assert.True(groupedJobs == settings.UseGroupedJobs());
            PersistentSceneSystem       persistentSceneSystem       = World.GetOrCreateSystem <PersistentSceneSystem>();
            BeginFramePersistencySystem beginFramePersistencySystem = World.GetExistingSystem <BeginFramePersistencySystem>();

            beginFramePersistencySystem.ReplaceSettings(settings);
            EndFramePersistencySystem endFramePersistencySystem = World.GetOrCreateSystem <EndFramePersistencySystem>();

            endFramePersistencySystem.ReplaceSettings(settings);
            EndInitializationEntityCommandBufferSystem ecbSystem = World.GetOrCreateSystem <EndInitializationEntityCommandBufferSystem>();
            PersistentDataStorage dataStorage = persistentSceneSystem.PersistentDataStorage;
            Hash128 identifier = Hash128.Compute("DynamicPoolTests");

            NativeArray <PersistableTypeHandle> typeHandles = new NativeArray <PersistableTypeHandle>(Archetype.Length, Allocator.Persistent);

            for (int i = 0; i < typeHandles.Length; i++)
            {
                typeHandles[i] = settings.GetPersistableTypeHandleFromFullTypeName(Archetype[i].GetManagedType().FullName); // This is not advised in a game
            }

            // Action
            Entity prefabEntity = m_Manager.CreateEntity(Archetype);

            m_Manager.AddComponent <Prefab>(prefabEntity);
            persistentSceneSystem.InitializePool(prefabEntity, total, identifier, typeHandles);
            int amountSpawned = 0;

            InstantiatePersistent(instantiationType, m_Manager, prefabEntity, ref amountSpawned, total);
            persistentSceneSystem.InstantChangePoolCapacity(identifier, total * 2); // double amount
            InstantiatePersistent(instantiationType, m_Manager, prefabEntity, ref amountSpawned, total);

            m_Manager.RemoveComponent <EmptyEcsTestData>(m_Manager.CreateEntityQuery(typeof(PersistenceState)));
            Entities.ForEach((Entity e, ref EcsTestData testData, DynamicBuffer <DynamicBufferData1> buffer) =>
            {
                testData.Value = 1;
                buffer.Add(new DynamicBufferData1()
                {
                    Value = 1
                });
            });

            dataStorage.ToIndex(0);
            endFramePersistencySystem.RequestPersist(dataStorage.GetWriteContainerForCurrentIndex(identifier));
            endFramePersistencySystem.Update();
            m_Manager.CompleteAllJobs();
            beginFramePersistencySystem.RequestApply(dataStorage.GetInitialStateReadContainer(identifier));
            beginFramePersistencySystem.Update();
            ecbSystem.Update();

            // Test
            int amountSurvivingEntities = m_Manager.CreateEntityQuery(typeof(EcsTestData)).CalculateEntityCount();

            Assert.True(amountSurvivingEntities == total * 2, $"{instantiationType} didn't create the expected amount of entities({amountSurvivingEntities}vs{total*2})! (Or they were destroyed by Apply)");
            Entities.ForEach((Entity e, ref EcsTestData testData, DynamicBuffer <DynamicBufferData1> buffer) =>
            {
                Assert.True(testData.Value == 0, "The apply didn't restore the component value!");
                Assert.True(buffer.IsEmpty, "The apply didn't restore the buffer!");
                Assert.True(m_Manager.HasComponent <EmptyEcsTestData>(e), "The apply didn't restore the component!");
            });

            // Action
            dataStorage.ToIndex(0);
            beginFramePersistencySystem.RequestApply(dataStorage.GetWriteContainerForCurrentIndex(identifier));
            beginFramePersistencySystem.Update();
            ecbSystem.Update();

            // Test
            amountSurvivingEntities = m_Manager.CreateEntityQuery(typeof(EcsTestData)).CalculateEntityCount();
            Assert.True(amountSurvivingEntities == total * 2, $"Some entities were unexpectedly destroyed!");
            Entities.ForEach((Entity e, ref EcsTestData testData, DynamicBuffer <DynamicBufferData1> buffer) =>
            {
                Assert.True(testData.Value == 1, "The second apply didn't restore the component value!");
                Assert.True(buffer[0].Value == 1, "The second apply didn't restore the buffer!");
                Assert.False(m_Manager.HasComponent <EmptyEcsTestData>(e), "The second apply didn't restore the component!");
            });

            // Cleanup
            m_Manager.DestroyEntity(m_Manager.UniversalQuery);
        }