Skip to content

Commit

Permalink
Merge pull request #16 from mackysoft/fix/blank-scene-active
Browse files Browse the repository at this point in the history
Navigathema blank scene is now unloaded when scene loaded
  • Loading branch information
mackysoft committed Feb 14, 2024
2 parents 5543932 + a8c8168 commit 3baccb8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Threading;
using Cysharp.Threading.Tasks;
using UnityEngine.SceneManagement;

namespace MackySoft.Navigathena.SceneManagement
{
internal sealed class NavigathenaBlankSceneIdentifier : ISceneIdentifier
{

public static readonly NavigathenaBlankSceneIdentifier Instance = new NavigathenaBlankSceneIdentifier();

public ISceneHandle CreateHandle ()
{
return NavigathenaBlankSceneHandle.Instance;
}

sealed class NavigathenaBlankSceneHandle : ISceneHandle
{

public static readonly NavigathenaBlankSceneHandle Instance = new NavigathenaBlankSceneHandle();

const string kSceneName = "Navigathena Blank";

public UniTask<Scene> Load (IProgress<float> progress = null, CancellationToken cancellationToken = default)
{
if (SceneManager.GetSceneByName(kSceneName).IsValid())
{
return UniTask.FromResult(SceneManager.GetSceneByName(kSceneName));
}
return UniTask.FromResult(SceneManager.CreateScene(kSceneName));
}

public async UniTask Unload (IProgress<float> progress = null, CancellationToken cancellationToken = default)
{
Scene scene = SceneManager.GetSceneByName(kSceneName);
if (!scene.isLoaded && !scene.IsValid())
{
return;
}

await SceneManager.UnloadSceneAsync(kSceneName)
.ToUniTask(progress, cancellationToken: cancellationToken);
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ async UniTask TryFinalizeAndUnloadCurrentScene (SceneHistoryEntry currentSceneEn
// NOTE: If the current scene is the only scene, create an empty scene to prevent an exception from being thrown when unloading.
if (SceneManager.sceneCount < 2)
{
SceneManager.CreateScene("Navigathena Blank");
await NavigathenaBlankSceneIdentifier.Instance.CreateHandle().Load(cancellationToken: cancellationToken);
}

await m_CurrentSceneState.Value.Handle.Unload(m_SceneProgressFactory.CreateProgress(progressDataStore, progress), cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public static async UniTask<SceneState> LoadSceneAndGetEntryPoint (ISceneIdentif
Scene loadedScene = await sceneHandle.Load(sceneProgressFactory.CreateProgress(progressDataStore, progress), cancellationToken);
cancellationToken.ThrowIfCancellationRequested();

await NavigathenaBlankSceneIdentifier.Instance.CreateHandle().Unload(cancellationToken: cancellationToken);
cancellationToken.ThrowIfCancellationRequested();

var sceneEntryPoint = loadedScene.GetComponentInScene<ISceneEntryPoint>(true);
return new SceneState(scene, sceneHandle, sceneEntryPoint);
}
Expand Down

0 comments on commit 3baccb8

Please sign in to comment.