generated from Depra-Inc/Template.Package.Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
efca799
commit bc82cb7
Showing
7 changed files
with
73 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// © 2023-2024 Nikolay Melnikov <[email protected]> | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
|
@@ -26,7 +27,10 @@ public SceneChange(SceneDefinition initialScene, ILoadingCurtain loadingCurtain) | |
|
||
public SceneDefinition ActiveScene { get; private set; } | ||
|
||
private async Task SwitchAsyncInternal(SceneDefinition scene, IEnumerable<ILoadingOperation> addOperations, | ||
public bool IsActive(SceneDefinition scene) => | ||
scene == ActiveScene || scene.Name == SceneManager.GetActiveScene().name; | ||
|
||
private async Task LoadInternal(SceneDefinition scene, IEnumerable<ILoadingOperation> addOperations, | ||
CancellationToken token) | ||
{ | ||
var operations = addOperations.Concat(new[] | ||
|
@@ -36,32 +40,19 @@ private async Task SwitchAsyncInternal(SceneDefinition scene, IEnumerable<ILoadi | |
_loadingCurtain.Unload(); | ||
} | ||
|
||
public bool IsActive(SceneDefinition scene) => | ||
scene == ActiveScene || scene.Name == SceneManager.GetActiveScene().name; | ||
Task ISceneChange.Load(SceneDefinition scene, IEnumerable<ILoadingOperation> addOperations, | ||
CancellationToken token) => IsActive(scene) | ||
? throw new UnexpectedSceneSwitch(scene.Name) | ||
: LoadInternal(ActiveScene = scene, addOperations, token); | ||
|
||
void ISceneChange.Switch(SceneDefinition scene) | ||
async Task ISceneChange.Unload(SceneDefinition scene, CancellationToken token) | ||
{ | ||
if (IsActive(scene)) | ||
{ | ||
throw new UnexpectedSceneSwitch(scene.Name); | ||
} | ||
|
||
ActiveScene = scene; | ||
SceneManager.LoadScene(ActiveScene.Name, ActiveScene.LoadMode); | ||
} | ||
|
||
Task ISceneChange.SwitchAsync(SceneDefinition scene, IEnumerable<ILoadingOperation> addOperations, | ||
CancellationToken token) | ||
{ | ||
if (IsActive(scene)) | ||
{ | ||
throw new UnexpectedSceneSwitch(scene.Name); | ||
} | ||
|
||
return SwitchAsyncInternal(ActiveScene = scene, addOperations, token); | ||
var operations = new[] { new SceneLoadingOperation(scene, OperationDescription.Default(scene.Name)) }; | ||
ILoadingCurtain cleanCurtain = new CleanLoadingCurtain(); | ||
await cleanCurtain.Load(operations, token); | ||
} | ||
|
||
Task ISceneChange.Reload(IEnumerable<ILoadingOperation> addOperations, CancellationToken token) => | ||
SwitchAsyncInternal(ActiveScene, addOperations, token); | ||
LoadInternal(ActiveScene, addOperations, token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
// © 2023-2024 Nikolay Melnikov <[email protected]> | ||
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Depra.Loading.Operations; | ||
|
@@ -21,6 +22,15 @@ public SceneLoadingOperation(SceneDefinition sceneDefinition, OperationDescripti | |
|
||
public OperationDescription Description { get; } | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private void ActivateIfNeeded(SceneDefinition scene) | ||
{ | ||
if (scene.ActivateOnLoad) | ||
{ | ||
SceneManager.SetActiveScene(scene.Handle); | ||
} | ||
} | ||
|
||
async Task ILoadingOperation.Load(Action<float> onProgress, CancellationToken token) | ||
{ | ||
onProgress?.Invoke(0); | ||
|
@@ -34,6 +44,7 @@ async Task ILoadingOperation.Load(Action<float> onProgress, CancellationToken to | |
} | ||
|
||
onProgress?.Invoke(1); | ||
ActivateIfNeeded(_sceneDefinition); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// © 2023-2024 Nikolay Melnikov <[email protected]> | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Depra.Loading.Operations; | ||
using UnityEngine.SceneManagement; | ||
|
||
namespace Depra.Scenes | ||
{ | ||
public sealed class SceneUnloadingOperation : ILoadingOperation | ||
{ | ||
private readonly SceneDefinition _sceneDefinition; | ||
|
||
public SceneUnloadingOperation(SceneDefinition sceneDefinition, OperationDescription description) | ||
{ | ||
Description = description; | ||
_sceneDefinition = sceneDefinition; | ||
} | ||
|
||
public OperationDescription Description { get; } | ||
|
||
async Task ILoadingOperation.Load(Action<float> onProgress, CancellationToken token) | ||
{ | ||
onProgress?.Invoke(0); | ||
var operation = SceneManager.UnloadSceneAsync(_sceneDefinition.Name); | ||
operation.allowSceneActivation = true; | ||
|
||
while (operation.isDone == false) | ||
{ | ||
onProgress?.Invoke(operation.progress); | ||
await Task.Yield(); | ||
} | ||
|
||
onProgress?.Invoke(1); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters