void CleanUpEntityRecursively(IDCLEntity entity, bool removeImmediatelyFromEntitiesList) { // Iterate through all entity children using (var iterator = entity.children.GetEnumerator()) { while (iterator.MoveNext()) { CleanUpEntityRecursively(iterator.Current.Value, removeImmediatelyFromEntitiesList); } } OnEntityRemoved?.Invoke(entity); if (Environment.i.world.sceneBoundsChecker.enabled) { entity.OnShapeUpdated -= Environment.i.world.sceneBoundsChecker.AddEntityToBeChecked; Environment.i.world.sceneBoundsChecker.RemoveEntityToBeChecked(entity); } if (removeImmediatelyFromEntitiesList) { // Every entity ends up being removed through here entity.Cleanup(); entities.Remove(entity.entityId); } else { Environment.i.platform.parcelScenesCleaner.MarkForCleanup(entity); } }
public void ForceCleanup() { while (disposableComponentsMarkedForCleanup.Count > 0) { MarkedSharedComponentInfo markedSharedComponentInfo = disposableComponentsMarkedForCleanup.Dequeue(); markedSharedComponentInfo.scene.SharedComponentDispose(markedSharedComponentInfo.componentId); } HashSet <ParcelScene> scenesToRemove = new HashSet <ParcelScene>(); // If we have root entities queued for removal, we call Parcel Scene's RemoveEntity() // so that the child entities end up recursively in the entitiesMarkedForCleanup queue while (rootEntitiesMarkedForCleanup.Count > 0) { MarkedEntityInfo markedEntityInfo = rootEntitiesMarkedForCleanup.Dequeue(); markedEntityInfo.scene.RemoveEntity(markedEntityInfo.entity.entityId, false); if (!scenesToRemove.Contains(markedEntityInfo.scene)) { scenesToRemove.Add(markedEntityInfo.scene); } } while (entitiesMarkedForCleanup.Count > 0) { IDCLEntity entity = entitiesMarkedForCleanup.Dequeue(); entity.SetParent(null); entity.Cleanup(); } foreach (var scene in scenesToRemove) { if (scene != null && !Environment.i.world.state.loadedScenes.ContainsKey(scene.sceneData.id)) { Object.Destroy(scene.gameObject); } } }
IEnumerator CleanupEntitiesCoroutine() { while (true) { float lastTime = Time.unscaledTime; while (disposableComponentsMarkedForCleanup.Count > 0) { MarkedSharedComponentInfo markedSharedComponentInfo = disposableComponentsMarkedForCleanup.Dequeue(); markedSharedComponentInfo.scene.SharedComponentDispose(markedSharedComponentInfo.componentId); if (DCLTime.realtimeSinceStartup - lastTime >= MAX_TIME_BUDGET) { yield return(null); lastTime = Time.unscaledTime; } } HashSet <ParcelScene> scenesToRemove = new HashSet <ParcelScene>(); // If we have root entities queued for removal, we call Parcel Scene's RemoveEntity() // so that the child entities end up recursively in the entitiesMarkedForCleanup queue while (rootEntitiesMarkedForCleanup.Count > 0) { MarkedEntityInfo markedEntityInfo = rootEntitiesMarkedForCleanup.Dequeue(); markedEntityInfo.scene.RemoveEntity(markedEntityInfo.entity.entityId, false); if (!scenesToRemove.Contains(markedEntityInfo.scene)) { scenesToRemove.Add(markedEntityInfo.scene); } if (DCLTime.realtimeSinceStartup - lastTime >= MAX_TIME_BUDGET) { yield return(null); lastTime = Time.unscaledTime; } } while (entitiesMarkedForCleanup.Count > 0) { IDCLEntity entity = entitiesMarkedForCleanup.Dequeue(); entity.SetParent(null); entity.Cleanup(); if (DCLTime.realtimeSinceStartup - lastTime >= MAX_TIME_BUDGET) { yield return(null); lastTime = Time.unscaledTime; } } foreach (var scene in scenesToRemove) { if (scene != null && !Environment.i.world.state.loadedScenes.ContainsKey(scene.sceneData.id)) { Object.Destroy(scene.gameObject); if (DCLTime.realtimeSinceStartup - lastTime >= MAX_TIME_BUDGET) { yield return(null); lastTime = Time.unscaledTime; } } } yield return(null); } }