Пример #1
0
        void SubScene_WithBlobAsset_ImportsAndLoads_Impl(bool useBlobAsset)
        {
            var subScene = SubSceneTestsHelper.CreateSubSceneFromObjects(ref m_TempAssets, "SubScene", false, () =>
            {
                var go                     = new GameObject("GO1");
                var component              = go.AddComponent <SubSceneLoadTestBlobAssetAuthoring>();
                component.Int              = 1;
                component.PtrInt           = 2;
                component.String           = "Test";
                component.Strings          = new[] { "A", null, "B" };
                component.UseNullBlobAsset = !useBlobAsset;
                return(new List <GameObject> {
                    go
                });
            });

            subScene.AutoLoadScene = false;
            subScene.gameObject.SetActive(false);

            var world       = World.DefaultGameObjectInjectionWorld;
            var sceneSystem = world.GetOrCreateSystem <SceneSystem>();
            var sceneEntity = sceneSystem.LoadSceneAsync(subScene.SceneGUID, new SceneSystem.LoadParameters
            {
                Flags = SceneLoadFlags.BlockOnImport | SceneLoadFlags.BlockOnStreamIn
            });

            // TODO: Editor doesn't update if it doesn't have focus, so we must explicitly update the world to process the load.
            world.Update();

            Assert.IsTrue(sceneSystem.IsSceneLoaded(sceneEntity), "Failed to load scene");
            CheckPublicRefs(world.EntityManager);

            {
                var entities = DebugEntity.GetAllEntities(world.EntityManager);
                var entity   = entities.FirstOrDefault(de => de.HasComponent <SubSceneLoadTestBlobAssetAuthoring.Component>());
                Assert.IsNotNull(entity, "Failed to find converted GameObject");

                var component = world.EntityManager.GetComponentData <SubSceneLoadTestBlobAssetAuthoring.Component>(entity.Entity);
                if (useBlobAsset)
                {
                    Assert.AreEqual(1, component.BlobAsset.Value.Int);
                    Assert.AreEqual(2, component.BlobAsset.Value.Ptr.Value);
                    Assert.AreEqual("Test", component.BlobAsset.Value.String.ToString());
                    Assert.AreEqual(3, component.BlobAsset.Value.Strings.Length);
                    Assert.AreEqual("A", component.BlobAsset.Value.Strings[0].ToString());
                    Assert.AreEqual("", component.BlobAsset.Value.Strings[1].ToString());
                    Assert.AreEqual("B", component.BlobAsset.Value.Strings[2].ToString());
                }
                else
                {
                    // TODO: this sometimes fails
                    Assert.AreEqual(BlobAssetReference <SubSceneLoadTestBlobAsset> .Null, component.BlobAsset, "Blob asset should be null");
                    Assert.IsFalse(component.BlobAsset.IsCreated, "Blob asset should not be created, but it was");
                }
            }
        }
Пример #2
0
        void SubScene_WithAsset_ImportsAndLoads(UnityEngine.Object asset)
        {
            var subScene = SubSceneTestsHelper.CreateSubSceneFromObjects(ref m_TempAssets, "SubScene", false, () =>
            {
                var go        = new GameObject("GO1");
                var managed   = go.AddComponent <SubSceneLoadTestAssetAuthoring>();
                managed.Asset = asset;
                return(new List <GameObject> {
                    go
                });
            });

            subScene.AutoLoadScene = false;
            subScene.gameObject.SetActive(false);

            var world       = World.DefaultGameObjectInjectionWorld;
            var sceneSystem = world.GetOrCreateSystem <SceneSystem>();
            var sceneEntity = sceneSystem.LoadSceneAsync(subScene.SceneGUID, new SceneSystem.LoadParameters
            {
                Flags = SceneLoadFlags.BlockOnImport | SceneLoadFlags.BlockOnStreamIn
            });

            // TODO: Editor doesn't update if it doesn't have focus, so we must explicitly update the world to process the load.
            world.Update();

            Assert.IsTrue(sceneSystem.IsSceneLoaded(sceneEntity), "Failed to load scene");
            CheckPublicRefs(world.EntityManager);

            {
                var entities = DebugEntity.GetAllEntities(world.EntityManager);
                var entity   = entities.FirstOrDefault(de => de.HasComponent <SubSceneLoadTestAssetAuthoring.Component>());
                Assert.IsNotNull(entity, "Failed to find converted GameObject");

                var component = world.EntityManager.GetComponentData <SubSceneLoadTestAssetAuthoring.Component>(entity.Entity);
                Assert.AreEqual(asset, component.Asset);
            }
        }
        public void SubScene_WithComplexComponents_ImportsAndLoads()
        {
            var subScene = SubSceneTestsHelper.CreateSubSceneFromObjects(ref m_TempAssets, "SubScene", false, () =>
            {
                var go1 = new GameObject("GO1");
                {
                    var unmanaged    = go1.AddComponent <SubSceneLoadTestUnmanagedAuthoring>();
                    unmanaged.Int    = 1;
                    unmanaged.Entity = go1;

                    var shared    = go1.AddComponent <SubSceneLoadTestSharedAuthoring>();
                    shared.Int    = 42;
                    shared.String = "Test";
                    shared.Asset  = m_Texture2;

                    var buffer      = go1.AddComponent <SubSceneLoadTestBufferAuthoring>();
                    buffer.Ints     = new List <int>();
                    buffer.Entities = new List <GameObject>();
                }
                go1.AddComponent <TestComponentAuthoring>();

                var go2 = new GameObject("GO2");
                {
                    var unmanaged    = go2.AddComponent <SubSceneLoadTestUnmanagedAuthoring>();
                    unmanaged.Int    = 2;
                    unmanaged.Entity = go2;

                    var managed    = go2.AddComponent <SubSceneLoadTestManagedAuthoring>();
                    managed.Int    = 2;
                    managed.String = "Test2";
                    managed.Entity = go2;
                    managed.Asset  = m_Texture1;

                    var shared    = go2.AddComponent <SubSceneLoadTestSharedAuthoring>();
                    shared.Int    = 42;
                    shared.String = "Test";
                    shared.Asset  = m_Texture1;
                }

                var go3 = new GameObject("GO3");
                {
                    var managed    = go3.AddComponent <SubSceneLoadTestManagedAuthoring>();
                    managed.Int    = 3;
                    managed.String = "Test3";
                    managed.Entity = go3;
                    managed.Asset  = m_Texture1;

                    var shared    = go3.AddComponent <SubSceneLoadTestSharedAuthoring>();
                    shared.Int    = 42;
                    shared.String = "Test Different";
                    shared.Asset  = m_Texture1;

                    var buffer  = go3.AddComponent <SubSceneLoadTestBufferAuthoring>();
                    buffer.Ints = new List <int> {
                        5
                    };
                    buffer.Entities = new List <GameObject> {
                        go2
                    };
                }

                var go4 = new GameObject("GO4");
                {
                    var unmanaged    = go4.AddComponent <SubSceneLoadTestUnmanagedAuthoring>();
                    unmanaged.Int    = 4;
                    unmanaged.Entity = go4;

                    var managed    = go4.AddComponent <SubSceneLoadTestManagedAuthoring>();
                    managed.Int    = 4;
                    managed.String = "Test4";
                    managed.Entity = go4;
                    managed.Asset  = m_Texture1;

                    var buffer  = go4.AddComponent <SubSceneLoadTestBufferAuthoring>();
                    buffer.Ints = new List <int> {
                        3, 17, 2
                    };
                    buffer.Entities = new List <GameObject> {
                        go1.gameObject, go2.gameObject, go3.gameObject
                    };
                }

                return(new List <GameObject> {
                    go1, go2, go3, go4
                });
            });

            subScene.AutoLoadScene = false;
            subScene.gameObject.SetActive(false);

            var world       = World.DefaultGameObjectInjectionWorld;
            var sceneSystem = world.GetOrCreateSystem <SceneSystem>();
            var sceneEntity = sceneSystem.LoadSceneAsync(subScene.SceneGUID, new SceneSystem.LoadParameters
            {
                Flags = SceneLoadFlags.BlockOnImport | SceneLoadFlags.BlockOnStreamIn
            });

            // TODO: Editor doesn't update if it doesn't have focus, so we must explicitly update the world to process the load.
            world.Update();

            Assert.IsTrue(sceneSystem.IsSceneLoaded(sceneEntity), "Failed to load scene");

            {
                var entities       = DebugEntity.GetAllEntities(world.EntityManager);
                var unmanagedQuery = world.EntityManager.CreateEntityQuery(typeof(SubSceneLoadTestUnmanagedAuthoring.Component));
                var managedQuery   = world.EntityManager.CreateEntityQuery(typeof(SubSceneLoadTestManagedAuthoring.Component));
                var sharedQuery    = world.EntityManager.CreateEntityQuery(typeof(SubSceneLoadTestSharedAuthoring.Component));
                var bufferQuery    = world.EntityManager.CreateEntityQuery(typeof(SubSceneLoadTestBufferAuthoring.Component));

                Assert.AreEqual(3, unmanagedQuery.CalculateEntityCount(), $"Expected exactly 3 entities with {typeof(SubSceneLoadTestUnmanagedAuthoring.Component).FullName}");
                Assert.AreEqual(3, managedQuery.CalculateEntityCount(), $"Expected exactly 3 entities with {typeof(SubSceneLoadTestManagedAuthoring.Component).FullName}");
                Assert.AreEqual(3, sharedQuery.CalculateEntityCount(), $"Expected exactly 3 entities with {typeof(SubSceneLoadTestSharedAuthoring.Component).FullName}");
                Assert.AreEqual(3, bufferQuery.CalculateEntityCount(), $"Expected exactly 3 entities with {typeof(SubSceneLoadTestBufferAuthoring.Component).FullName}");


                var e1 = entities.FirstOrDefault(de => de.HasComponent <SubSceneLoadTestUnmanagedAuthoring.Component>() && de.HasComponent <SubSceneLoadTestSharedAuthoring.Component>() && de.HasComponent <SubSceneLoadTestBufferAuthoring.Component>());
                Assert.IsNotNull(e1, "Could not find entity corresponding to GO1");
                var e2 = entities.FirstOrDefault(de => de.HasComponent <SubSceneLoadTestUnmanagedAuthoring.Component>() &&
                                                 de.HasComponent <SubSceneLoadTestManagedAuthoring.Component>() &&
                                                 de.HasComponent <SubSceneLoadTestSharedAuthoring.Component>());
                Assert.IsNotNull(e2, "Could not find entity corresponding to GO2");
                var e3 = entities.FirstOrDefault(de =>
                                                 de.HasComponent <SubSceneLoadTestManagedAuthoring.Component>() &&
                                                 de.HasComponent <SubSceneLoadTestSharedAuthoring.Component>() && de.HasComponent <SubSceneLoadTestBufferAuthoring.Component>());
                Assert.IsNotNull(e3, "Could not find entity corresponding to GO3");
                var e4 = entities.FirstOrDefault(de => de.HasComponent <SubSceneLoadTestUnmanagedAuthoring.Component>() &&
                                                 de.HasComponent <SubSceneLoadTestManagedAuthoring.Component>() &&
                                                 de.HasComponent <SubSceneLoadTestBufferAuthoring.Component>());
                Assert.IsNotNull(e4, "Could not find entity corresponding to GO4");

                {
                    var unmanaged = world.EntityManager.GetComponentData <SubSceneLoadTestUnmanagedAuthoring.Component>(e1.Entity);
                    Assert.AreEqual(e1.Entity, unmanaged.Entity);
                    Assert.AreEqual(1, unmanaged.Int);
                    VerifyBlobAsset(unmanaged.BlobAsset, 1, "GO1", 1, "GO1-unmanaged");

                    var shared = world.EntityManager.GetSharedComponentData <SubSceneLoadTestSharedAuthoring.Component>(e1.Entity);
                    Assert.AreEqual(42, shared.Int);
                    Assert.AreEqual("Test", shared.String);
                    Assert.AreEqual(m_Texture2, shared.Asset);

                    var buffer = world.EntityManager.GetBuffer <SubSceneLoadTestBufferAuthoring.Component>(e1.Entity);
                    Assert.AreEqual(0, buffer.Length);
                }

                {
                    var unmanaged = world.EntityManager.GetComponentData <SubSceneLoadTestUnmanagedAuthoring.Component>(e2.Entity);
                    Assert.AreEqual(e2.Entity, unmanaged.Entity);
                    Assert.AreEqual(2, unmanaged.Int);
                    VerifyBlobAsset(unmanaged.BlobAsset, 2, "GO2", 1, "GO2-unmanaged");

                    var managed = world.EntityManager.GetComponentData <SubSceneLoadTestManagedAuthoring.Component>(e2.Entity);
                    Assert.AreEqual(e2.Entity, managed.Entity);
                    Assert.AreEqual(2, managed.Int);
                    Assert.AreEqual("Test2", managed.String);
                    Assert.AreEqual(m_Texture1, managed.Asset);

                    var shared = world.EntityManager.GetSharedComponentData <SubSceneLoadTestSharedAuthoring.Component>(e2.Entity);
                    Assert.AreEqual(42, shared.Int);
                    Assert.AreEqual("Test", shared.String);
                    Assert.AreEqual(m_Texture1, shared.Asset);
                }

                {
                    var managed = world.EntityManager.GetComponentData <SubSceneLoadTestManagedAuthoring.Component>(e3.Entity);
                    Assert.AreEqual(e3.Entity, managed.Entity);
                    Assert.AreEqual(3, managed.Int);
                    Assert.AreEqual("Test3", managed.String);
                    Assert.AreEqual(m_Texture1, managed.Asset);

                    var shared = world.EntityManager.GetSharedComponentData <SubSceneLoadTestSharedAuthoring.Component>(e3.Entity);
                    Assert.AreEqual(42, shared.Int);
                    Assert.AreEqual("Test Different", shared.String);
                    Assert.AreEqual(m_Texture1, shared.Asset);

                    var buffer = world.EntityManager.GetBuffer <SubSceneLoadTestBufferAuthoring.Component>(e3.Entity);
                    Assert.AreEqual(1, buffer.Length);
                    Assert.AreEqual(e2.Entity, buffer[0].Entity);
                    Assert.AreEqual(5, buffer[0].Int);
                    VerifyBlobAsset(buffer[0].BlobAsset, 5, "GO30", 0, "GO3-buffer0");
                }

                {
                    var unmanaged = world.EntityManager.GetComponentData <SubSceneLoadTestUnmanagedAuthoring.Component>(e4.Entity);
                    Assert.AreEqual(e4.Entity, unmanaged.Entity);
                    Assert.AreEqual(4, unmanaged.Int);
                    VerifyBlobAsset(unmanaged.BlobAsset, 4, "GO4", 1, "GO4-unmanaged");

                    var managed = world.EntityManager.GetComponentData <SubSceneLoadTestManagedAuthoring.Component>(e4.Entity);
                    Assert.AreEqual(e4.Entity, managed.Entity);
                    Assert.AreEqual(4, managed.Int);
                    Assert.AreEqual("Test4", managed.String);
                    Assert.AreEqual(m_Texture1, managed.Asset);

                    var buffer = world.EntityManager.GetBuffer <SubSceneLoadTestBufferAuthoring.Component>(e4.Entity);
                    Assert.AreEqual(3, buffer.Length);

                    Assert.AreEqual(e1.Entity, buffer[0].Entity);
                    Assert.AreEqual(3, buffer[0].Int);
                    VerifyBlobAsset(buffer[0].BlobAsset, 3, "GO40", 0, "GO4-buffer0");

                    Assert.AreEqual(e2.Entity, buffer[1].Entity);
                    Assert.AreEqual(17, buffer[1].Int);
                    VerifyBlobAsset(buffer[1].BlobAsset, 17, "GO41", 1, "GO4-buffer1");

                    Assert.AreEqual(e3.Entity, buffer[2].Entity);
                    Assert.AreEqual(2, buffer[2].Int);
                    VerifyBlobAsset(buffer[2].BlobAsset, 2, "GO42", 2, "GO4-buffer2");
                }
            }

            sceneSystem.UnloadScene(sceneEntity);
            Assert.IsFalse(sceneSystem.IsSceneLoaded(sceneEntity), "Failed to unload scene");
        }