Пример #1
0
    public void SimpleHybridComponent()
    {
        TestUtilities.RegisterSystems(World, TestUtilities.SystemCategories.Streaming | TestUtilities.SystemCategories.HybridComponents);

        var loadParams = new SceneSystem.LoadParameters
        {
            Flags = SceneLoadFlags.BlockOnImport | SceneLoadFlags.BlockOnStreamIn
        };

        var sceneGUID = new GUID(AssetDatabase.AssetPathToGUID("Assets/StressTests/HybridLights/HybridLights/SubScene.unity"));

        World.GetExistingSystem <SceneSystem>().LoadSceneAsync(sceneGUID, loadParams);
        World.Update();


        var entity    = EmptySystem.GetSingletonEntity <Light>();
        var companion = m_Manager.GetComponentObject <Light>(entity).gameObject;

        void MoveAndCheck(float3 testpos)
        {
            m_Manager.SetComponentData(entity, new LocalToWorld {
                Value = float4x4.Translate(testpos)
            });
            World.Update();
            Assert.AreEqual(testpos, (float3)companion.transform.position);
        }

        MoveAndCheck(new float3(1, 2, 3));
        MoveAndCheck(new float3(2, 3, 4));
    }
        public void IncrementalConversionLinkedGroup()
        {
            var conversionFlags = GameObjectConversionUtility.ConversionFlags.GameViewLiveLink | GameObjectConversionUtility.ConversionFlags.AssignName;
            // Parent (LinkedEntityGroup) (2 additional entities)
            // - Child (2 additional entities)
            // All reference parent game object

            var parent = CreateGameObject().AddComponent <EntityRefTestDataAuthoring>();
            var child  = CreateGameObject().AddComponent <EntityRefTestDataAuthoring>();

            child.transform.parent = parent.transform;

            child.name = "child";
            child.AdditionalEntityCount    = 2;
            child.DeclareLinkedEntityGroup = false;
            child.Value = parent.gameObject;

            parent.name = "parent";
            parent.AdditionalEntityCount    = 2;
            parent.DeclareLinkedEntityGroup = true;
            parent.Value = parent.gameObject;

            using (var conversionWorld = GameObjectConversionUtility.ConvertIncrementalInitialize(SceneManager.GetActiveScene(), new GameObjectConversionSettings(World, conversionFlags)))
            {
                Entities.ForEach((ref EntityRefTestData data) =>
                                 StringAssert.StartsWith("parent", m_Manager.GetName(data.Value)));

                var entity = EmptySystem.GetSingletonEntity <LinkedEntityGroup>();

                // Parent (LinkedEntityGroup) (2 additional entities)
                // - Child (1 additional entities)
                // All reference child game object
                child.Value = child.gameObject;
                child.AdditionalEntityCount = 1;
                parent.Value = child.gameObject;
                GameObjectConversionUtility.ConvertIncremental(conversionWorld, new[] { child.gameObject }, conversionFlags);

                EntitiesAssert.ContainsOnly(m_Manager,
                                            EntityMatch.Exact <EntityRefTestData>(entity, k_CommonComponents,
                                                                                  EntityMatch.Component((LinkedEntityGroup[] group) => group.Length == 5)),
                                            EntityMatch.Exact <EntityRefTestData>(k_ChildComponents),
                                            EntityMatch.Exact <EntityRefTestData>(),
                                            EntityMatch.Exact <EntityRefTestData>(),
                                            EntityMatch.Exact <EntityRefTestData>());

                // We expect there to still only be one linked entity group and it should be the same entity as before
                // since it is attached to the primary entity which is not getting destroyed.
                Assert.AreEqual(entity, EmptySystem.GetSingletonEntity <LinkedEntityGroup>());

                Entities.ForEach((ref EntityRefTestData data) =>
                                 StringAssert.StartsWith("child", m_Manager.GetName(data.Value)));

                foreach (var e in m_Manager.GetBuffer <LinkedEntityGroup>(entity).AsNativeArray())
                {
                    Assert.IsTrue(m_Manager.Exists(e.Value));
                }
            }
        }
        public void GetComponentGroup_WhenObjectWithMatchingComponentExists_ComponentArrayIsPopulated()
        {
            var rb = m_GameObject.AddComponent <Rigidbody>();

            GameObjectEntity.AddToEntityManager(m_Manager, m_GameObject);

            var grp = EmptySystem.GetComponentGroup(typeof(Rigidbody));

            var array = grp.GetComponentArray <Rigidbody>();

            Assert.That(array.ToArray(), Is.EqualTo(new[] { rb }));
        }
Пример #4
0
        public void RigidbodyComponentArray()
        {
            var go = new GameObject("test", typeof(Rigidbody));

            /*var entity =*/ GameObjectEntity.AddToEntityManager(m_Manager, go);

            var grp = EmptySystem.GetComponentGroup(typeof(Rigidbody));

            var arr = grp.GetComponentArray <Rigidbody>();

            Assert.AreEqual(1, arr.Length);
            Assert.AreEqual(go.GetComponent <Rigidbody>(), arr[0]);

            Object.DestroyImmediate(go);
        }
        public void ComponentDataAndTransformArray()
        {
            var go = new GameObject("test", typeof(EcsTestComponent)).GetComponent <GameObjectEntity>();

            m_Manager.SetComponentData(go.Entity, new EcsTestData(5));

            var grp = EmptySystem.GetComponentGroup(typeof(Transform), typeof(EcsTestData));
            var arr = grp.GetComponentArray <Transform>();

            Assert.AreEqual(1, arr.Length);
            Assert.AreEqual(go.transform, arr[0]);
            Assert.AreEqual(5, grp.GetComponentDataArray <EcsTestData>()[0].value);

            Object.DestroyImmediate(go.gameObject);
        }
        public void GetComponentGroup_WhenObjectWithMatchingComponentAndECSDataExists_ComponentArraysArePopulated()
        {
            var goe = m_GameObject.AddComponent <EcsTestProxy>().GetComponent <GameObjectEntity>();
            var expectedTestData = new EcsTestData(5);

            m_Manager.SetComponentData(goe.Entity, expectedTestData);

            var grp = EmptySystem.GetComponentGroup(typeof(Transform), typeof(EcsTestData));

            var transformArray = grp.GetComponentArray <Transform>();

            Assert.That(transformArray.ToArray(), Is.EqualTo(new[] { goe.transform }));
            var ecsDataArray = grp.GetComponentDataArray <EcsTestData>();

            Assert.That(ecsDataArray.Length, Is.EqualTo(1));
            Assert.That(ecsDataArray[0], Is.EqualTo(expectedTestData));
        }
Пример #7
0
    private EventQueue EnqueueBufferFromChunkJobDynamicBuffer()
    {
        var components = new[]
        {
            ComponentType.ReadWrite <EcsTestData2>(),
            ComponentType.ReadWrite <EcsIntElement>()
        };

        var query  = Manager.CreateEntityQuery(components);
        var arch   = Manager.CreateArchetype(components);
        var entity = Manager.CreateEntity(arch);

        var testComponent = GetDefaultComponent2();

        Manager.SetComponentData(entity, testComponent);

        var testBuffer = GetDefaultBufferData();
        var buffer     = Manager.GetBuffer <EcsIntElement>(entity);

        buffer.AddRange(testBuffer);

        var(baseQueue, componentQueue, bufferQueue) = new QueueRig <EcsTestData, EcsIntElement>(Allocator.Temp);

        var handle1 = new EnqueuesFromDynamicBufferJob
        {
            ComponentType = Manager.GetArchetypeChunkComponentType <EcsTestData2>(true),
            BufferType    = Manager.GetArchetypeChunkBufferType <EcsIntElement>(true),
            Events        = bufferQueue
        }.ScheduleSingle(query);

        var handle2 = new EnqueuesFromDynamicBufferJob
        {
            ComponentType = Manager.GetArchetypeChunkComponentType <EcsTestData2>(true),
            BufferType    = Manager.GetArchetypeChunkBufferType <EcsIntElement>(true),
            Events        = bufferQueue
        }.ScheduleSingle(query);


        EmptySystem.AddDependencies(handle1, handle2);
        handle1.Complete();
        handle2.Complete();

        return(baseQueue);
    }
        public void MultipleHybridComponents_DontGetMixedUp_WhenInstantiated()
        {
            // Setup a simple entity with multiple hybrid components
            var gameObjectPrefab = CreateGameObject("prefab",
                                                    typeof(ConversionTestHybridComponentA),
                                                    typeof(ConversionTestHybridComponentB),
                                                    typeof(ConversionTestHybridComponentC));
            var settings     = MakeDefaultSettings().WithExtraSystem <MonoBehaviourComponentConversionSystem>();
            var entityPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(gameObjectPrefab, settings);

            // Create a bunch of instances
            var instances = m_Manager.Instantiate(entityPrefab, 10, Allocator.Temp);

            var query = EmptySystem.GetEntityQuery(new EntityQueryDesc
            {
                All = new[]
                {
                    ComponentType.ReadOnly <ConversionTestHybridComponentA>(),
                    ComponentType.ReadOnly <ConversionTestHybridComponentB>(),
                    ComponentType.ReadOnly <ConversionTestHybridComponentC>(),
                }
            });

            var a = query.ToComponentArray <ConversionTestHybridComponentA>();
            var b = query.ToComponentArray <ConversionTestHybridComponentB>();
            var c = query.ToComponentArray <ConversionTestHybridComponentC>();

            // The source doesn't have the Prefab tag, so it also gets picked up by the query
            Assert.AreEqual(instances.Length + 1, a.Length);
            Assert.AreEqual(instances.Length + 1, b.Length);
            Assert.AreEqual(instances.Length + 1, c.Length);

            CollectionAssert.AllItemsAreUnique(a);
            CollectionAssert.AllItemsAreUnique(b);
            CollectionAssert.AllItemsAreUnique(c);
        }
        public void CompanionGameObject_ActivatesIfNotPrefabOrDisabled()
        {
            // Create a prefab asset with an Hybrid Component
            var prefab     = CreateGameObject("prefab", typeof(ConversionTestHybridComponent));
            var prefabPath = m_TempAssetDir + "/TestPrefab.prefab";

            Assert.IsFalse(prefab.IsPrefab());
            prefab = PrefabUtility.SaveAsPrefabAsset(prefab, prefabPath, out var success);
            Assert.IsTrue(success && prefab.IsPrefab());

            // Create a GameObject that references the prefab, in order to trigger the conversion of the prefab
            var gameObject = CreateGameObject("prefab_ref", typeof(ConversionTestHybridComponentPrefabReference));

            gameObject.GetComponent <ConversionTestHybridComponentPrefabReference>().Prefab = prefab;

            // Run the actual conversion, we only care about the prefab so we destroy the other entity
            var settings = MakeDefaultSettings().WithExtraSystem <MonoBehaviourComponentConversionSystem>();
            var dummy    = GameObjectConversionUtility.ConvertGameObjectHierarchy(gameObject, settings);

            m_Manager.DestroyEntity(dummy);
            EntitiesAssert.ContainsOnly(m_Manager, EntityMatch.Exact <CompanionLink, ConversionTestHybridComponent, Prefab, LinkedEntityGroup>(k_CommonComponents));

            // Accessing the prefab entity and its companion GameObject can't be directly done with GetSingleton because it requires EntityQueryOptions.IncludePrefab
            var companionQuery = EmptySystem.GetEntityQuery(new EntityQueryDesc
            {
                All     = new[] { ComponentType.ReadOnly <CompanionLink>() },
                Options = EntityQueryOptions.IncludePrefab
            });
            var prefabEntity    = companionQuery.GetSingletonEntity();
            var prefabCompanion = m_Manager.GetComponentData <CompanionLink>(prefabEntity).Companion;

            // Create an instance, the expectation is that the prefab remains inactive, but the instance activates
            var instanceEntity    = m_Manager.Instantiate(prefabEntity);
            var instanceCompanion = m_Manager.GetComponentData <CompanionLink>(instanceEntity).Companion;

            // Activation happens through a system, so before the first update everything is inactive
            Assert.IsFalse(prefabCompanion.activeSelf);
            Assert.IsFalse(instanceCompanion.activeSelf);

            // Register all the Hybrid Component related systems, including the one that deals with activation
            TestUtilities.RegisterSystems(World, TestUtilities.SystemCategories.HybridComponents);

            // After an update, the prefab should remain inactive, but the instance should be active
            World.Update();
            Assert.IsFalse(prefabCompanion.activeSelf);
            Assert.IsTrue(instanceCompanion.activeSelf);

            // Let's reverse the test, demote the prefab to a regular entity, and disable the instance
            m_Manager.RemoveComponent <Prefab>(prefabEntity);
            m_Manager.AddComponent <Disabled>(instanceEntity);

            // After an update, the prefab which isn't one anymore should be active, and the disabled entity should be inactive
            World.Update();
            Assert.IsTrue(prefabCompanion.activeSelf);
            Assert.IsFalse(instanceCompanion.activeSelf);

            // Let's reverse once more and get back to the initial state
            m_Manager.AddComponent <Prefab>(prefabEntity);
            m_Manager.RemoveComponent <Disabled>(instanceEntity);

            // After an update, the prefab should be inactive again, and the instance should be active again
            World.Update();
            Assert.IsFalse(prefabCompanion.activeSelf);
            Assert.IsTrue(instanceCompanion.activeSelf);
        }