internal static GameObjectSceneRefCount *CreateOrRetainScene(Scene unityScene) { if (JobsUtility.IsExecutingJob) { throw new InvalidOperationException("You cannot CreateOrRetain a GameObjectSceneHandle from a Job"); } if (!unityScene.IsValid()) { throw new InvalidOperationException("Unity Scene is invalid."); } SceneEquatable unitySceneEquatable = unityScene; // Lazy create the LoadedScenes container GameObjectSceneManager.Initialize(); // First Check if we have it loaded var loadedScenes = GameObjectSceneManager.LoadedScenes; loadedScenes.TryGetValue(unityScene, out var unitySceneHandleIntPtr); GameObjectSceneRefCount *unitySceneHandle = (GameObjectSceneRefCount *)unitySceneHandleIntPtr; if (unitySceneHandle == null) { // Create a new handle unitySceneHandle = (GameObjectSceneRefCount *)UnsafeUtility.Malloc(sizeof(GameObjectSceneRefCount), 16, Allocator.Persistent); *unitySceneHandle = new GameObjectSceneRefCount(unityScene); loadedScenes[unitySceneEquatable] = (IntPtr)unitySceneHandle; } return(unitySceneHandle); }
private static void ReleaseScene(GameObjectSceneRefCount *unitySceneHandle) { if (JobsUtility.IsExecutingJob) { throw new InvalidOperationException("You cannot Release a GameObjectSceneHandle from a Job. This is likely because you are removing the last Scene Entity with a GameObject Scene reference from a job."); } var unityScene = unitySceneHandle->Scene; var loadedScenes = GameObjectSceneManager.LoadedScenes; if (!loadedScenes.ContainsKey(unityScene)) { throw new InvalidOperationException($"Attempting to release a Scene that is not contained within LoadedScenes! {unityScene}"); } loadedScenes.Remove(unityScene); UnsafeUtility.Free(unitySceneHandle, Allocator.Persistent); // This may have been unloaded outside of the GameObjectSceneSystem, so we just do nothing if (unityScene.IsValid()) { SceneManager.UnloadSceneAsync(unityScene); } }