Skip to content

Commit 2879e01

Browse files
joaoborkssemantic-release-bot
authored andcommitted
feat: add reload scene methods and tests (#53)
1 parent 46d79f4 commit 2879e01

File tree

79 files changed

+240
-9
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+240
-9
lines changed

Runtime/CoreSceneManager.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ public Task<SceneResult> TransitionAsync(SceneParameters sceneParameters, ILoadS
147147
: TransitionWithIntermediateAsync(sceneParameters, intermediateSceneReference, linkedSource.Token).RunAndDisposeToken(linkedSource);
148148
}
149149

150+
public Task<SceneResult> ReloadActiveSceneAsync(ILoadSceneInfo intermediateSceneReference = null, CancellationToken token = default)
151+
{
152+
if (_activeScene == null || !_activeScene.SceneReference.IsValid() || !_activeScene.SceneReference.isLoaded)
153+
throw new InvalidOperationException($"[{GetType().Name}] Cannot reload the active scene because it is null or not loaded. Make sure to load a scene before trying to reload it.");
154+
155+
ILoadSceneInfo targetSceneInfo = _activeScene.LoadSceneInfo;
156+
return TransitionAsync(new SceneParameters(targetSceneInfo, true), intermediateSceneReference, token);
157+
}
158+
150159
public Task<SceneResult> LoadAsync(SceneParameters sceneParameters, IProgress<float> progress = null, CancellationToken token = default)
151160
{
152161
CancellationTokenSource linkedSource = CancellationTokenSource.CreateLinkedTokenSource(_lifetimeTokenSource.Token, token);

Runtime/Interfaces/ISceneManager.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ public interface ISceneManager : IDisposable
6969
/// <returns>A <see cref="System.Threading.Tasks.Task{TResult}"/> with all scenes loaded.</returns>
7070
Task<SceneResult> TransitionAsync(SceneParameters sceneParameters, ILoadSceneInfo intermediateSceneReference = default, CancellationToken token = default);
7171

72+
/// <summary>
73+
/// Reloads the active scene with an optional intermediate loading scene.
74+
/// </summary>
75+
/// <param name="intermediateSceneReference">
76+
/// A reference to the scene that's going to be loaded as the transition intermediate (as a loading scene).
77+
/// If null, the transition will not have an intermediate loading scene.
78+
/// </param>
79+
/// <param name="token">Optional token to manually cancel the operation. Note that Unity Scene Manager operations cannot be manually canceled and will continue to run.</param>
80+
/// <returns>A <see cref="System.Threading.Tasks.Task{TResult}"/> with all scenes reloaded.</returns>
81+
Task<SceneResult> ReloadActiveSceneAsync(ILoadSceneInfo intermediateSceneReference = default, CancellationToken token = default);
82+
7283
/// <summary>
7384
/// Loads the target scene or group of scenes provided via a <see cref="SceneParameters"/> struct.
7485
/// You may also provide the desired index to set as the active scene.

Runtime/MySceneManager.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ public static event Action<Scene> SceneLoaded
100100
/// <returns>A <see cref="System.Threading.Tasks.Task{TResult}"/> with all scenes loaded.</returns>
101101
public static Task<SceneResult> TransitionAsync(SceneParameters sceneParameters, ILoadSceneInfo intermediateSceneReference = default, CancellationToken token = default) => Instance.TransitionAsync(sceneParameters, intermediateSceneReference, token);
102102

103+
/// <summary>
104+
/// Reloads the active scene with an optional intermediate loading scene.
105+
/// </summary>
106+
/// <param name="intermediateSceneReference">
107+
/// A reference to the scene that's going to be loaded as the transition intermediate (as a loading scene).
108+
/// If null, the transition will not have an intermediate loading scene.
109+
/// </param>
110+
/// <param name="token">Optional token to manually cancel the operation. Note that Unity Scene Manager operations cannot be manually canceled and will continue to run.</param>
111+
/// <returns>A <see cref="System.Threading.Tasks.Task{TResult}"/> with all scenes reloaded.</returns>
112+
public static Task<SceneResult> ReloadActiveSceneAsync(ILoadSceneInfo intermediateSceneReference = null, CancellationToken token = default) => Instance.ReloadActiveSceneAsync(intermediateSceneReference, token);
113+
103114
/// <summary>
104115
/// Loads the target scene or group of scenes provided via a <see cref="SceneParameters"/> struct.
105116
/// You may also provide the desired index to set as the active scene.
@@ -520,6 +531,52 @@ public static event Action<Scene> SceneLoaded
520531
public static Task<SceneResult> TransitionAddressableAsync(string targetAddress, string loadingAddress = null, CancellationToken token = default) => Instance.TransitionAddressableAsync(targetAddress, loadingAddress, token);
521532
#endif
522533

534+
/// <summary>
535+
/// Reloads the active scene with an optional intermediate loading scene.
536+
/// </summary>
537+
/// <param name="loadingSceneName">
538+
/// A reference to the scene that's going to be loaded as the transition intermediate (as a loading scene).
539+
/// If null, the transition will not have an intermediate loading scene.
540+
/// </param>
541+
/// <param name="token">Optional token to manually cancel the operation. Note that Unity Scene Manager operations cannot be manually canceled and will continue to run.</param>
542+
/// <returns>A <see cref="System.Threading.Tasks.Task{TResult}"/> with all scenes reloaded.</returns>
543+
public static Task<SceneResult> ReloadActiveSceneAsync(string loadingSceneName = null, CancellationToken token = default) => Instance.ReloadActiveSceneAsync(loadingSceneName, token);
544+
545+
/// <summary>
546+
/// Reloads the active scene with an optional intermediate loading scene.
547+
/// </summary>
548+
/// <param name="loadingBuildIndex">
549+
/// A reference to the scene that's going to be loaded as the transition intermediate (as a loading scene).
550+
/// If null, the transition will not have an intermediate loading scene.
551+
/// </param>
552+
/// <param name="token">Optional token to manually cancel the operation. Note that Unity Scene Manager operations cannot be manually canceled and will continue to run.</param>
553+
/// <returns>A <see cref="System.Threading.Tasks.Task{TResult}"/> with all scenes reloaded.</returns>
554+
public static Task<SceneResult> ReloadActiveSceneAsync(int loadingBuildIndex = -1, CancellationToken token = default) => Instance.ReloadActiveSceneAsync(loadingBuildIndex, token);
555+
556+
#if ENABLE_ADDRESSABLES
557+
/// <summary>
558+
/// Reloads the active scene with an optional intermediate loading scene.
559+
/// </summary>
560+
/// <param name="loadingAssetReference">
561+
/// A reference to the scene that's going to be loaded as the transition intermediate (as a loading scene).
562+
/// If null, the transition will not have an intermediate loading scene.
563+
/// </param>
564+
/// <param name="token">Optional token to manually cancel the operation. Note that Unity Scene Manager operations cannot be manually canceled and will continue to run.</param>
565+
/// <returns>A <see cref="System.Threading.Tasks.Task{TResult}"/> with all scenes reloaded.</returns>
566+
public static Task<SceneResult> ReloadActiveSceneAddressableAsync(AssetReference loadingAssetReference = null, CancellationToken token = default) => Instance.ReloadActiveSceneAddressableAsync(loadingAssetReference, token);
567+
568+
/// <summary>
569+
/// Reloads the active scene with an optional intermediate loading scene.
570+
/// </summary>
571+
/// <param name="loadingAddress">
572+
/// A reference to the scene that's going to be loaded as the transition intermediate (as a loading scene).
573+
/// If null, the transition will not have an intermediate loading scene.
574+
/// </param>
575+
/// <param name="token">Optional token to manually cancel the operation. Note that Unity Scene Manager operations cannot be manually canceled and will continue to run.</param>
576+
/// <returns>A <see cref="System.Threading.Tasks.Task{TResult}"/> with all scenes reloaded.</returns>
577+
public static Task<SceneResult> ReloadActiveSceneAddressableAsync(string loadingAddress = null, CancellationToken token = default) => Instance.ReloadActiveSceneAddressableAsync(loadingAddress, token);
578+
#endif
579+
523580
/// <summary>
524581
/// Unloads the target scene or group of scenes.
525582
/// </summary>

Runtime/Utilities/SceneManagerExtensions.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,68 @@ public static Task<SceneResult> TransitionAddressableAsync(this ISceneManager sc
447447
}
448448
#endif
449449

450+
/// <summary>
451+
/// Reloads the active scene with an optional intermediate loading scene.
452+
/// </summary>
453+
/// <param name="loadingSceneName">
454+
/// A reference to the scene that's going to be loaded as the transition intermediate (as a loading scene).
455+
/// If null, the transition will not have an intermediate loading scene.
456+
/// </param>
457+
/// <param name="token">Optional token to manually cancel the operation. Note that Unity Scene Manager operations cannot be manually canceled and will continue to run.</param>
458+
/// <returns>A <see cref="System.Threading.Tasks.Task{TResult}"/> with all scenes reloaded.</returns>
459+
public static Task<SceneResult> ReloadActiveSceneAsync(this ISceneManager sceneManager, string loadingSceneName = null, CancellationToken token = default)
460+
{
461+
ILoadSceneInfo loadingSceneInfo = string.IsNullOrWhiteSpace(loadingSceneName) ? null : new LoadSceneInfoName(loadingSceneName);
462+
return sceneManager.ReloadActiveSceneAsync(loadingSceneInfo, token);
463+
}
464+
465+
/// <summary>
466+
/// Reloads the active scene with an optional intermediate loading scene.
467+
/// </summary>
468+
/// <param name="loadingBuildIndex">
469+
/// A reference to the scene that's going to be loaded as the transition intermediate (as a loading scene).
470+
/// If null, the transition will not have an intermediate loading scene.
471+
/// </param>
472+
/// <param name="token">Optional token to manually cancel the operation. Note that Unity Scene Manager operations cannot be manually canceled and will continue to run.</param>
473+
/// <returns>A <see cref="System.Threading.Tasks.Task{TResult}"/> with all scenes reloaded.</returns>
474+
public static Task<SceneResult> ReloadActiveSceneAsync(this ISceneManager sceneManager, int loadingBuildIndex = -1, CancellationToken token = default)
475+
{
476+
ILoadSceneInfo loadingSceneInfo = loadingBuildIndex >= 0 ? new LoadSceneInfoIndex(loadingBuildIndex) : null;
477+
return sceneManager.ReloadActiveSceneAsync(loadingSceneInfo, token);
478+
}
479+
480+
#if ENABLE_ADDRESSABLES
481+
/// <summary>
482+
/// Reloads the active scene with an optional intermediate loading scene.
483+
/// </summary>
484+
/// <param name="loadingAssetReference">
485+
/// A reference to the scene that's going to be loaded as the transition intermediate (as a loading scene).
486+
/// If null, the transition will not have an intermediate loading scene.
487+
/// </param>
488+
/// <param name="token">Optional token to manually cancel the operation. Note that Unity Scene Manager operations cannot be manually canceled and will continue to run.</param>
489+
/// <returns>A <see cref="System.Threading.Tasks.Task{TResult}"/> with all scenes reloaded.</returns>
490+
public static Task<SceneResult> ReloadActiveSceneAddressableAsync(this ISceneManager sceneManager, AssetReference loadingAssetReference = null, CancellationToken token = default)
491+
{
492+
ILoadSceneInfo loadingSceneInfo = loadingAssetReference != null ? new LoadSceneInfoAssetReference(loadingAssetReference) : null;
493+
return sceneManager.ReloadActiveSceneAsync(loadingSceneInfo, token);
494+
}
495+
496+
/// <summary>
497+
/// Reloads the active scene with an optional intermediate loading scene.
498+
/// </summary>
499+
/// <param name="loadingAddress">
500+
/// A reference to the scene that's going to be loaded as the transition intermediate (as a loading scene).
501+
/// If null, the transition will not have an intermediate loading scene.
502+
/// </param>
503+
/// <param name="token">Optional token to manually cancel the operation. Note that Unity Scene Manager operations cannot be manually canceled and will continue to run.</param>
504+
/// <returns>A <see cref="System.Threading.Tasks.Task{TResult}"/> with all scenes reloaded.</returns>
505+
public static Task<SceneResult> ReloadActiveSceneAddressableAsync(this ISceneManager sceneManager, string loadingAddress = null, CancellationToken token = default)
506+
{
507+
ILoadSceneInfo loadingSceneInfo = string.IsNullOrWhiteSpace(loadingAddress) ? null : new LoadSceneInfoAddress(loadingAddress);
508+
return sceneManager.ReloadActiveSceneAsync(loadingSceneInfo, token);
509+
}
510+
#endif
511+
450512
/// <summary>
451513
/// Unloads the target scene or group of scenes.
452514
/// </summary>

Samples.meta

Lines changed: 0 additions & 8 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)