Beispiel #1
0
        public override BuildResult Run(BuildContext context)
        {
            var manifest           = context.BuildManifest;
            var buildConfiguration = BuildContextInternals.GetBuildConfiguration(context);
            var profile            = context.GetComponentOrDefault <DotsRuntimeBuildProfile>();
            var rootAssembly       = context.GetComponentOrDefault <DotsRuntimeRootAssembly>();
            var targetName         = rootAssembly.MakeBeeTargetName(context.BuildConfigurationName);
            var scenes             = context.GetComponentOrDefault <SceneList>();
            var firstScene         = scenes.GetScenePathsForBuild().FirstOrDefault();

            s_AssemblyCache.BaseAssemblies = rootAssembly.RootAssembly.asset;
            s_AssemblyCache.PlatformName   = profile.Target.UnityPlatformName;

            // Record any log errors/exceptions to be able to stop the build if any
            ExportConfigurationLogHandler logHandler = new ExportConfigurationLogHandler();

            logHandler.Hook();

            using (var tmpWorld = new World(ConfigurationScene.Guid.ToString()))
            {
                var dataDirInfo =
                    WorldExport.GetOrCreateDataDirectoryFrom(rootAssembly.StagingDirectory.Combine(targetName));
                var logDirectory = WorldExport.GetOrCreateLogDirectoryFrom(targetName);
                var subScenePath = WorldExport
                                   .GetOrCreateSubSceneDirectoryFrom(rootAssembly.StagingDirectory.Combine(targetName)).ToString();
                var outputDir = BuildStepGenerateBeeFiles.GetFinalOutputDirectory(context, targetName);

                var hasFilter = context.TryGetComponent <ConversionSystemFilterSettings>(out var conversionFilter);

                // Run configuration systems
                ConfigurationSystemGroup configSystemGroup = tmpWorld.GetOrCreateSystem <ConfigurationSystemGroup>();
                var systems = TypeCache.GetTypesDerivedFrom(typeof(ConfigurationSystemBase));
                foreach (var type in systems)
                {
                    if (hasFilter && !conversionFilter.ShouldRunConversionSystem(type))
                    {
                        continue;
                    }

                    ConfigurationSystemBase baseSys = (ConfigurationSystemBase)tmpWorld.GetOrCreateSystem(type);
                    baseSys.BuildContext     = context;
                    baseSys.AssemblyCache    = s_AssemblyCache;
                    baseSys.LogDirectoryPath = logDirectory.FullName;
                    configSystemGroup.AddSystemToUpdateList(baseSys);
                }

                configSystemGroup.SortSystems();
                configSystemGroup.Update();

                // Export configuration scene
                var writeEntitySceneSettings = new WriteEntitySceneSettings()
                {
                    Codec              = Codec.LZ4,
                    IsDotsRuntime      = true,
                    OutputPath         = subScenePath,
                    BuildAssemblyCache = s_AssemblyCache
                };
                var(decompressedSize, compressedSize) = EditorEntityScenes.WriteEntitySceneSection(
                    tmpWorld.EntityManager, ConfigurationScene.Guid, "0", null, writeEntitySceneSettings,
                    out var objectRefCount, out var objRefs, default);
        public void TestReadAndWriteWithObjectRef()
        {
            string binPath    = "Temp/test.bin";
            string binRefPath = "Temp/test.bin.ref";

            var dstWorld         = new World("");
            var dstEntitymanager = dstWorld.EntityManager;
            var material         = AssetDatabase.LoadAssetAtPath <Material>("Packages/com.unity.entities/Unity.Scenes.Hybrid.Tests/Test.mat");

            var entity = m_Manager.CreateEntity();

            m_Manager.AddComponentData(entity, new MaterialRefComponent {
                Value = material
            });
            m_Manager.AddComponentData(entity, new EcsTestData()
            {
                value = 5
            });

            EditorEntityScenes.Write(m_Manager, binPath, binRefPath);
            EditorEntityScenes.Read(dstEntitymanager, binPath, binRefPath);

            var dstEntity = dstEntitymanager.UniversalQuery.GetSingletonEntity();

            Assert.AreEqual(material, m_Manager.GetComponentData <MaterialRefComponent>(entity).Value);
            Assert.AreEqual(material, dstEntitymanager.GetComponentData <MaterialRefComponent>(dstEntity).Value);

            Assert.AreEqual(5, m_Manager.GetComponentData <EcsTestData>(entity).value);
            Assert.AreEqual(5, dstEntitymanager.GetComponentData <EcsTestData>(dstEntity).value);
        }
Beispiel #3
0
        public IEnumerator EndToEnd_ManagedComponents()
        {
            var guid = GUID.Generate();
            var temp = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene);

            temp.name = "EndToEndTest_ManagedComponentsScene";
            EditorSceneManager.SetActiveScene(temp);
            var cube            = GameObject.CreatePrimitive(PrimitiveType.Cube);
            var entitySceneData = EditorEntityScenes.WriteEntityScene(temp, guid);

            Assert.AreEqual(1, entitySceneData.Length);

            var sceneEntity = m_Manager.CreateEntity();

            m_Manager.AddComponentData(sceneEntity, entitySceneData[0]);

            var query = m_Manager.CreateEntityQuery(typeof(ManagedObjectWithObjectReference));

            for (int i = 0; i != 10; i++)
            {
                m_Manager.AddComponentData(sceneEntity, new RequestSceneLoaded());

                Assert.AreEqual(1, m_Manager.Debug.EntityCount);

                for (int w = 0; w != 1000; w++)
                {
                    World.GetOrCreateSystem <SceneSectionStreamingSystem>().Update();
                    if (1 != m_Manager.Debug.EntityCount)
                    {
                        break;
                    }

                    yield return(null);
                }

                // 1. Scene entity
                // 2. Public ref array
                // 3. Mesh Renderer
                Assert.AreEqual(3, m_Manager.Debug.EntityCount);

                var entities = query.ToEntityArray(Collections.Allocator.TempJob);
                Assert.AreEqual(1, entities.Length);
                var component = m_Manager.GetComponentData <ManagedObjectWithObjectReference>(entities[0]);
                Assert.NotNull(component.Texture);
                Assert.AreEqual(512, component.Texture.width);
                Assert.AreEqual(512, component.Texture.height);
                entities.Dispose();

                m_Manager.RemoveComponent <RequestSceneLoaded>(sceneEntity);
                World.GetOrCreateSystem <SceneSectionStreamingSystem>().Update();

                Assert.AreEqual(1, m_Manager.Debug.EntityCount);
            }
            query.Dispose();
        }
        /// <summary>
        /// Returns an enum detailing if the given <see cref="GameObject"/> will be converted and how.
        /// </summary>
        /// <param name="gameObject">The <see cref="GameObject"/> to be converted.</param>
        /// <returns>A <see cref="GameObjectConversionResultStatus"/> code detailing how the <see cref="GameObject"/> will be converted.</returns>
        public static GameObjectConversionResultStatus GetGameObjectConversionResultStatus(GameObject gameObject)
        {
            if (null == gameObject || !gameObject)
            {
                return(GameObjectConversionResultStatus.NotConverted);
            }

            if (EditorEntityScenes.IsEntitySubScene(gameObject.scene))
            {
                // Any gameObject in a sub-scene will ignore special conversion components and always result in a converted entity.
                return(GameObjectConversionResultStatus.ConvertedBySubScene);
            }

            s_StopConvertToEntity.Clear();
            gameObject.GetComponentsInParent(true, s_StopConvertToEntity);
            if (s_StopConvertToEntity.Count > 0)
            {
                // This gameObject or an ancestor has the StopConvertToEntity component.
                // This means all of its children (including itself) are not converted.
                return(GameObjectConversionResultStatus.NotConvertedByStopConvertToEntityComponent);
            }

            s_ConvertToEntity.Clear();
            gameObject.GetComponentsInParent(true, s_ConvertToEntity);
            foreach (var convertToEntity in s_ConvertToEntity)
            {
                if (convertToEntity.gameObject != gameObject && convertToEntity.ConversionMode == ConvertToEntity.Mode.ConvertAndInjectGameObject)
                {
                    // An ancestor is being converted and injected.
                    // This means all of its children are not converted.
                    return(GameObjectConversionResultStatus.NotConvertedByConvertAndInjectMode);
                }
            }

            foreach (var convertToEntity in s_ConvertToEntity)
            {
                if (convertToEntity.gameObject != gameObject && convertToEntity.ConversionMode == ConvertToEntity.Mode.ConvertAndDestroy)
                {
                    // An ancestor is being converted and destroyed.
                    // This means all of its children are converted.
                    return(GameObjectConversionResultStatus.ConvertedByAncestor);
                }
            }

            var convertToEntityOnCurrentGO = gameObject.GetComponent <ConvertToEntity>();

            return((convertToEntityOnCurrentGO && convertToEntityOnCurrentGO.enabled)
                ? GameObjectConversionResultStatus.ConvertedByConvertToEntity
                : GameObjectConversionResultStatus.NotConverted);
        }
Beispiel #5
0
        public IEnumerator EndToEnd()
        {
            var guid = GUID.Generate();
            var temp = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene);

            EditorSceneManager.SetActiveScene(temp);
            var cube            = GameObject.CreatePrimitive(PrimitiveType.Cube);
            var entitySceneData = EditorEntityScenes.WriteEntityScene(temp, guid);

            Assert.AreEqual(1, entitySceneData.Length);

            var sceneEntity = m_Manager.CreateEntity();

            m_Manager.AddComponentData(sceneEntity, entitySceneData[0]);

            for (int i = 0; i != 10; i++)
            {
                m_Manager.AddComponentData(sceneEntity, new RequestSceneLoaded());

                Assert.AreEqual(1, m_Manager.Debug.EntityCount);

                for (int w = 0; w != 1000; w++)
                {
                    World.GetOrCreateSystem <ResolveSceneReferenceSystem>().Update();
                    World.GetOrCreateSystem <SceneSectionStreamingSystem>().Update();
                    if (1 != m_Manager.Debug.EntityCount)
                    {
                        break;
                    }

                    yield return(null);
                }

                // 1. Scene entity
                // 2. Public ref array
                // 3. Mesh Renderer
                Assert.AreEqual(3, m_Manager.Debug.EntityCount);

                m_Manager.RemoveComponent <RequestSceneLoaded>(sceneEntity);
                World.GetOrCreateSystem <SceneSectionStreamingSystem>().Update();

                Assert.AreEqual(1, m_Manager.Debug.EntityCount);
            }
        }
Beispiel #6
0
        public IEnumerator LodSplitOverSections()
        {
            var guid = GUID.Generate();
            var temp = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene);

            EditorSceneManager.SetActiveScene(temp);

            // Large set of dummy entities to stress the public ref array
            const int dummyCount = 100;

            for (int i = 0; i < dummyCount; ++i)
            {
                new GameObject($"Dummy {i}");
            }

            var lod0Renderer = GameObject.CreatePrimitive(PrimitiveType.Cube);
            var lod1Renderer = GameObject.CreatePrimitive(PrimitiveType.Cube);
            var lod2Renderer = GameObject.CreatePrimitive(PrimitiveType.Cube);

            lod0Renderer.transform.position = new Vector3(0, 0, 0);
            lod1Renderer.transform.position = new Vector3(1, 0, 0);
            lod2Renderer.transform.position = new Vector3(2, 0, 0);

            var highLod = new GameObject("HighLOD");

            highLod.SetActive(false);
            var highLodGroup = highLod.AddComponent <LODGroup>();

            var lowLod = new GameObject("LowLOD");

            lowLod.SetActive(false);
            var lowLodGroup = lowLod.AddComponent <LODGroup>();

            var hlod = new GameObject("HLOD");

            hlod.SetActive(false);
            var hlodComponent = hlod.AddComponent <HLOD>();
            var hlodGroup     = hlod.GetComponent <LODGroup>();

            lod0Renderer.transform.parent = highLod.transform;
            lod1Renderer.transform.parent = highLod.transform;
            lod2Renderer.transform.parent = lowLod.transform;

            highLod.transform.parent = hlod.transform;
            lowLod.transform.parent  = hlod.transform;

            highLodGroup.SetLODs(new[]
            {
                new LOD(0.75f, new[] { lod0Renderer.GetComponent <Renderer>() }),
                new LOD(0.00f, new[] { lod1Renderer.GetComponent <Renderer>() }),
            });

            lowLodGroup.SetLODs(new[]
            {
                new LOD(0.00f, new[] { lod2Renderer.GetComponent <Renderer>() }),
            });

            hlodComponent.LODParentTransforms = new[] { highLod.transform, lowLod.transform };
            hlodGroup.SetLODs(new[]
            {
                new LOD(0.25f, new Renderer[] {}),
                new LOD(0.00f, new Renderer[] {}),
            });

            highLod.SetActive(true);
            lowLod.SetActive(true);
            hlod.SetActive(true);

            var entitySceneData = EditorEntityScenes.WriteEntityScene(temp, guid, 0);

            Assert.AreEqual(2, entitySceneData.Length);

            var sceneEntitySection0 = m_Manager.CreateEntity();

            m_Manager.AddComponentData(sceneEntitySection0, entitySceneData[0]);

            var sceneEntitySection1 = m_Manager.CreateEntity();

            m_Manager.AddComponentData(sceneEntitySection1, entitySceneData[1]);

            // Loading one scene at a time
            for (int i = 0; i != 10; i++)
            {
                Assert.AreEqual(2, m_Manager.Debug.EntityCount);

                m_Manager.AddComponentData(sceneEntitySection0, new RequestSceneLoaded());

                for (int w = 0; w != 1000; w++)
                {
                    World.GetOrCreateSystem <SubSceneStreamingSystem>().Update();
                    if (2 != m_Manager.Debug.EntityCount)
                    {
                        break;
                    }

                    Assert.AreNotEqual(999, w, "Streaming is stuck");

                    yield return(null);
                }

                // 1. Scene entity section 0
                // 2. Scene entity section 1
                // 3. Public ref array
                // 4. HLOD
                // 5. LowLod group
                // 6. LOD2 renderer
                Assert.AreEqual(dummyCount + 6, m_Manager.Debug.EntityCount);

                // Destroying and recreating the system causes the streaming worlds to reset, otherwise oversize
                // buffers could carry over and would not trigger the right asserts.
                World.DestroySystem(World.GetExistingSystem <SubSceneStreamingSystem>());

                m_Manager.AddComponentData(sceneEntitySection1, new RequestSceneLoaded());

                for (int w = 0; w != 1000; w++)
                {
                    World.GetOrCreateSystem <SubSceneStreamingSystem>().Update();
                    if (dummyCount + 6 != m_Manager.Debug.EntityCount)
                    {
                        break;
                    }

                    Assert.AreNotEqual(999, w, "Streaming is stuck");

                    yield return(null);
                }

                // 1. Scene entity section 0
                // 2. Scene entity section 1
                // 3. Public ref array
                // 4. HLOD
                // 5. LowLod group
                // 6. LOD2 renderer
                // 7. External ref array
                // 8. HighLod group
                // 9. LOD1 renderer
                // A. LOD0 renderer
                Assert.AreEqual(dummyCount + 10, m_Manager.Debug.EntityCount);

                m_Manager.RemoveComponent <RequestSceneLoaded>(sceneEntitySection1);
                World.GetOrCreateSystem <SubSceneStreamingSystem>().Update();

                Assert.AreEqual(dummyCount + 6, m_Manager.Debug.EntityCount);

                m_Manager.RemoveComponent <RequestSceneLoaded>(sceneEntitySection0);
                World.GetOrCreateSystem <SubSceneStreamingSystem>().Update();

                Assert.AreEqual(2, m_Manager.Debug.EntityCount);
            }
        }
Beispiel #7
0
        public override BuildResult Run(BuildContext context)
        {
            var manifest               = context.BuildManifest;
            var rootAssembly           = context.GetComponentOrDefault <DotsRuntimeRootAssembly>();
            var buildScenes            = context.GetComponentOrDefault <SceneList>();
            var targetName             = rootAssembly.MakeBeeTargetName(context.BuildConfigurationName);
            var scenePaths             = buildScenes.GetScenePathsForBuild();
            var buildConfigurationGuid = context.BuildConfigurationAssetGUID;
            var dataDirectory          = WorldExport.GetOrCreateDataDirectoryFrom(rootAssembly.StagingDirectory.Combine(targetName));
            var logsDirectory          = WorldExport.GetOrCreateLogDirectoryFrom(targetName);

            var sceneGuids = scenePaths.SelectMany(scenePath =>
            {
                var guids = EditorEntityScenes.GetSubScenes(AssetDatabaseCompatibility.PathToGUID(scenePath)).ToList();
                guids.Add(AssetDatabaseCompatibility.PathToGUID(scenePath));
                return(guids);
            }).Distinct().ToList();

            //Save all unsaved scenes of the project first
            foreach (var guid in sceneGuids)
            {
                string scenePath = AssetDatabase.GUIDToAssetPath(guid.ToString());
                var    scene     = SceneManager.GetSceneByPath(scenePath);
                EditorSceneManager.SaveScene(scene);
            }

            var requiresRefresh       = false;
            var sceneBuildConfigGuids = new NativeArray <GUID>(sceneGuids.Count, Allocator.TempJob);

            for (int i = 0; i != sceneBuildConfigGuids.Length; i++)
            {
                sceneBuildConfigGuids[i] = SceneWithBuildConfigurationGUIDs.EnsureExistsFor(sceneGuids[i], new Hash128(buildConfigurationGuid), false, out var thisRequiresRefresh);
                requiresRefresh         |= thisRequiresRefresh;
            }
            if (requiresRefresh)
            {
                AssetDatabase.Refresh();
            }

            var artifactHashes = new NativeArray <UnityEngine.Hash128>(sceneGuids.Count, Allocator.TempJob);

            AssetDatabaseCompatibility.ProduceArtifactsRefreshIfNecessary(sceneBuildConfigGuids, typeof(SubSceneImporter), artifactHashes);

            bool succeeded = true;

            for (int i = 0; i != sceneBuildConfigGuids.Length; i++)
            {
                var sceneGuid    = sceneGuids[i];
                var artifactHash = artifactHashes[i];

                AssetDatabaseCompatibility.GetArtifactPaths(artifactHash, out var artifactPaths);

                List <FileInfo> exportedFiles     = new List <FileInfo>();
                bool            foundEntityHeader = false;
                foreach (var artifactPath in artifactPaths)
                {
                    var ext = Path.GetExtension(artifactPath).ToLower().Replace(".", "");
                    if (ext == EntityScenesPaths.GetExtension(EntityScenesPaths.PathType.EntitiesHeader))
                    {
                        foundEntityHeader = true;
                        var destinationFile = dataDirectory.FullName + Path.DirectorySeparatorChar + EntityScenesPaths.RelativePathFolderFor(sceneGuid, EntityScenesPaths.PathType.EntitiesHeader, -1);
                        new NPath(artifactPath).MakeAbsolute().Copy(new NPath(destinationFile).MakeAbsolute().EnsureParentDirectoryExists());
                        exportedFiles.Add(new FileInfo(destinationFile));
                    }
                    else if (ext == EntityScenesPaths.GetExtension(EntityScenesPaths.PathType.EntitiesBinary))
                    {
                        var destinationFile = dataDirectory.FullName + Path.DirectorySeparatorChar + EntityScenesPaths.RelativePathFolderFor(sceneGuid, EntityScenesPaths.PathType.EntitiesBinary, EntityScenesPaths.GetSectionIndexFromPath(artifactPath));
                        new NPath(artifactPath).MakeAbsolute().Copy(new NPath(destinationFile).MakeAbsolute().EnsureParentDirectoryExists());
                        exportedFiles.Add(new FileInfo(destinationFile));
                    }
                    else if (ext == EntityScenesPaths.GetExtension(EntityScenesPaths.PathType.EntitiesConversionLog))
                    {
                        var destinationFile = logsDirectory.FullName + Path.DirectorySeparatorChar + $"{sceneGuid}.{EntityScenesPaths.GetExtension(EntityScenesPaths.PathType.EntitiesConversionLog)}";
                        new NPath(artifactPath).MakeAbsolute().Copy(new NPath(destinationFile).MakeAbsolute().EnsureParentDirectoryExists());
                        var result = PrintConversionLogToUnityConsole(artifactPath);
                        if (result.HasError || result.HasException)
                        {
                            UnityEngine.Debug.LogError("Failed to export scene: " + Path.GetFileName(AssetDatabase.GUIDToAssetPath(sceneGuid.ToString())));
                            succeeded = false;
                        }
                    }
                    else if (new Hash128(ext).IsValid) //Asset files are exported as {artifactHash}.{assetguid}
                    {
                        var destinationFile = dataDirectory.FullName + Path.DirectorySeparatorChar + ext;
                        new NPath(artifactPath).MakeAbsolute().Copy(new NPath(destinationFile).MakeAbsolute().EnsureParentDirectoryExists());
                        exportedFiles.Add(new FileInfo(destinationFile));
                    }
                }

                if (!foundEntityHeader)
                {
                    Debug.LogError($"Failed to build EntityScene for '{AssetDatabaseCompatibility.GuidToPath(sceneGuid)}'.");
                    succeeded = false;
                }

                //UpdateManifest
                manifest.Add(new Guid(sceneGuid.ToString()), AssetDatabase.GUIDToAssetPath(sceneGuid.ToString()), exportedFiles);
            }

            var catalogPath = Path.Combine(dataDirectory.ToString(), SceneSystem.k_SceneInfoFileName);

            WriteCatalogFile(catalogPath, buildScenes);
            manifest.AddAdditionalFilesToDeploy(new FileInfo(catalogPath.ToString()));

            sceneBuildConfigGuids.Dispose();
            artifactHashes.Dispose();

            if (succeeded)
            {
                return(context.Success());
            }
            return(context.Failure($"Failed to export scenes"));
        }