示例#1
0
        public void SetEnabledOnPrefabOnCompleteSet()
        {
            var entity   = ConvertGameObjectHierarchy(LoadPrefab("Prefab_Hierarchy"), MakeDefaultSettings());
            var instance = m_Manager.Instantiate(entity);

            EntitiesAssert.ContainsOnly(m_Manager,
                                        EntityMatch.Exact <Prefab>(new MockData(100), k_RootComponents, entity),
                                        EntityMatch.Exact <Prefab>(new MockData(101), k_ChildComponents),
                                        EntityMatch.Exact(new MockData(100), k_RootComponents, instance),
                                        EntityMatch.Exact(new MockData(101), k_ChildComponents));

            m_Manager.SetEnabled(instance, false);

            EntitiesAssert.Contains(m_Manager,
                                    EntityMatch.Exact <Disabled, MockData>(k_RootComponents, instance),
                                    EntityMatch.Exact <Disabled, MockData>(k_ChildComponents));

            m_Manager.SetEnabled(instance, true);

            EntitiesAssert.Contains(m_Manager,
                                    EntityMatch.Exact <MockData>(k_RootComponents, instance),
                                    EntityMatch.Exact <MockData>(k_ChildComponents));
        }
        public void GeneratedAuthoringComponent_WithReferencedPrefab_IsConverted()
        {
            var go = CreateGameObject();

            var authoringType = GetAuthoringComponentType <CodeGenTestComponent>();
            var c             = go.AddComponent(authoringType);

            authoringType.GetField(nameof(CodeGenTestComponent.Entity)).SetValue(c, LoadPrefab("Prefab"));

            Entity goEntity = default;

            Assert.DoesNotThrow(() => goEntity = GameObjectConversionUtility.ConvertGameObjectHierarchy(go, MakeDefaultSettings()));

            EntitiesAssert.Contains(m_Manager, EntityMatch.Exact <CodeGenTestComponent>(goEntity, k_RootComponents));
            var prefabEntity = m_Manager.GetComponentData <CodeGenTestComponent>(goEntity).Entity;

            EntitiesAssert.ContainsOnly(m_Manager,
                                        // gameobject created above
                                        EntityMatch.Exact <CodeGenTestComponent>(goEntity, k_RootComponents),
                                        // referenced prefab
                                        EntityMatch.Exact <Prefab>(prefabEntity, new MockData(), k_RootComponents)
                                        );
        }
        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;

            var settings = new GameObjectConversionSettings(World, conversionFlags)
            {
                Systems = TestWorldSetup.GetDefaultInitSystemsFromEntitiesPackage(WorldSystemFilterFlags.GameObjectConversion).ToList()
            };

            using (var conversionWorld = GameObjectConversionUtility.InitializeIncrementalConversion(SceneManager.GetActiveScene(), settings))
            {
                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;
#pragma warning disable 618
                GameObjectConversionUtility.ConvertIncremental(conversionWorld, new[] { child.gameObject }, conversionFlags);
#pragma warning restore 618

                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 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);
        }