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.
[v0.1.18] Dependency updated; Test new scene activation
- Loading branch information
1 parent
67fcab7
commit dafab0e
Showing
12 changed files
with
138 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// © 2023-2024 Nikolay Melnikov <[email protected]> | ||
|
||
using UnityEngine; | ||
|
||
namespace Depra.Scenes.Activation | ||
{ | ||
public sealed class CompositeSceneActivation : ISceneActivation | ||
{ | ||
private readonly ISceneActivation[] _activations; | ||
|
||
public CompositeSceneActivation(params ISceneActivation[] activations) => _activations = activations; | ||
|
||
void ISceneActivation.BeforeLoading(AsyncOperation operation) | ||
{ | ||
foreach (var activation in _activations) | ||
{ | ||
activation.BeforeLoading(operation); | ||
} | ||
} | ||
|
||
void ISceneActivation.OnProgress(AsyncOperation operation) | ||
{ | ||
foreach (var activation in _activations) | ||
{ | ||
activation.OnProgress(operation); | ||
} | ||
} | ||
} | ||
} |
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
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,48 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// © 2023-2024 Nikolay Melnikov <[email protected]> | ||
|
||
using Depra.Expectation; | ||
using UnityEngine; | ||
using UnityEngine.SceneManagement; | ||
|
||
namespace Depra.Scenes.Activation | ||
{ | ||
public sealed class ExternalSceneActivation : ISceneActivation | ||
{ | ||
private readonly IExpectant _externalExpectant; | ||
|
||
private Scene _loadedScene; | ||
private Expectant _loadingExpectant; | ||
|
||
public ExternalSceneActivation(IExpectant expectant) => _externalExpectant = expectant; | ||
|
||
private void Activate() | ||
{ | ||
SceneManager.SetActiveScene(_loadedScene); | ||
_loadingExpectant?.Dispose(); | ||
_externalExpectant?.Dispose(); | ||
} | ||
|
||
private void OnSceneLoaded(Scene scene, LoadSceneMode mode) | ||
{ | ||
_loadedScene = scene; | ||
SceneManager.sceneLoaded -= OnSceneLoaded; | ||
if (_loadedScene.IsValid()) | ||
{ | ||
_loadingExpectant?.SetReady(); | ||
} | ||
} | ||
|
||
void ISceneActivation.BeforeLoading(AsyncOperation operation) | ||
{ | ||
SceneManager.sceneLoaded += OnSceneLoaded; | ||
new GroupExpectant.And() | ||
.With(_externalExpectant) | ||
.With(_loadingExpectant = new Expectant()) | ||
.Build() | ||
.Subscribe(Activate); | ||
} | ||
|
||
void ISceneActivation.OnProgress(AsyncOperation operation) { } | ||
} | ||
} |
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
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.Threading; | ||
using System.Threading.Tasks; | ||
using Depra.Expectation; | ||
|
@@ -13,36 +14,35 @@ namespace Depra.Scenes.Operations | |
{ | ||
public sealed class SceneChangeOperation : ILoadingOperation | ||
{ | ||
private readonly IExpectant _finishExpectant; | ||
private readonly ISceneActivation _activation; | ||
private readonly SceneDefinition _desiredScene; | ||
private readonly SceneDefinition _previousScene; | ||
private readonly IExpectant _loadedSceneActivationExpectant; | ||
private readonly OperationDescription _description; | ||
|
||
public SceneChangeOperation(SceneDefinition from, SceneDefinition to, | ||
OperationDescription description, ISceneActivation activation, | ||
IExpectant loadedSceneActivationExpectant = null) | ||
OperationDescription description, ISceneActivation activation, IExpectant finishExpectant = null) | ||
{ | ||
_desiredScene = to; | ||
_previousScene = from; | ||
_activation = activation; | ||
_description = description; | ||
_loadedSceneActivationExpectant = loadedSceneActivationExpectant; | ||
_finishExpectant = finishExpectant; | ||
} | ||
|
||
OperationDescription ILoadingOperation.Description => _description; | ||
|
||
public async Task Load(ProgressCallback onProgress, CancellationToken token) | ||
public async Task Load(IProgress<float> progress, CancellationToken token) | ||
{ | ||
if (_desiredScene.IsActive()) | ||
{ | ||
throw new UnexpectedSceneSwitch(_desiredScene.DisplayName); | ||
} | ||
|
||
await new SceneLoadOperation(_desiredScene, _description, _activation, _loadedSceneActivationExpectant) | ||
.Load(onProgress, token); | ||
await new SceneLoadOperation(_desiredScene, _description, _activation, _finishExpectant) | ||
.Load(progress, token); | ||
await new SceneUnloadOperation(_previousScene, _description) | ||
.Load(onProgress, token); | ||
.Load(progress, 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
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.Threading; | ||
using System.Threading.Tasks; | ||
using Depra.Expectation; | ||
|
@@ -13,81 +14,67 @@ namespace Depra.Scenes.Operations | |
{ | ||
public sealed class SceneLoadOperation : ILoadingOperation | ||
{ | ||
private readonly IExpectant _finishExpectant; | ||
private readonly ISceneActivation _activation; | ||
private readonly SceneDefinition _desiredScene; | ||
private readonly IExpectant _activationExpectant; | ||
private readonly OperationDescription _description; | ||
|
||
private Expectant _loadingExpectant; | ||
|
||
public SceneLoadOperation(SceneDefinition desiredScene, OperationDescription description, | ||
ISceneActivation activation, IExpectant activationExpectant = null) | ||
ISceneActivation activation, IExpectant finishExpectant = null) | ||
{ | ||
_activation = activation; | ||
_description = description; | ||
_desiredScene = desiredScene; | ||
_activationExpectant = activationExpectant; | ||
_finishExpectant = finishExpectant; | ||
_activation = desiredScene.ActivateOnLoad ? activation : new EmptySceneActivation(); | ||
} | ||
|
||
OperationDescription ILoadingOperation.Description => _description; | ||
|
||
public async Task Load(ProgressCallback onProgress, CancellationToken token) | ||
public async Task Load(IProgress<float> progress, CancellationToken token) | ||
{ | ||
onProgress?.Invoke(0); | ||
if (_desiredScene.ActivateOnLoad) | ||
{ | ||
SetupActivation(); | ||
} | ||
|
||
progress.Report(0); | ||
var operation = SceneManager.LoadSceneAsync(_desiredScene.DisplayName, _desiredScene.LoadMode); | ||
if (operation == null) | ||
{ | ||
onProgress?.Invoke(1); | ||
progress.Report(1); | ||
return; | ||
} | ||
|
||
_activation.BeforeLoading(operation); | ||
while (operation.isDone == false) | ||
{ | ||
onProgress?.Invoke(operation.progress); | ||
progress.Report(operation.progress); | ||
_activation.OnProgress(operation); | ||
|
||
await Task.Yield(); | ||
} | ||
|
||
onProgress?.Invoke(1); | ||
progress.Report(1); | ||
await AfterLoading(); | ||
} | ||
|
||
private void SetupActivation() | ||
private async Task AfterLoading() | ||
{ | ||
SceneManager.sceneLoaded += OnSceneLoaded; | ||
new GroupExpectant.And() | ||
.With(_loadingExpectant = new Expectant()) | ||
.With(_activationExpectant) | ||
.Build() | ||
.Subscribe(Activate); | ||
} | ||
|
||
private void OnSceneLoaded(Scene scene, LoadSceneMode mode) | ||
{ | ||
SceneManager.sceneLoaded -= OnSceneLoaded; | ||
if (scene.IsValid()) | ||
if (_finishExpectant == null) | ||
{ | ||
_loadingExpectant?.SetReady(); | ||
return; | ||
} | ||
} | ||
|
||
private void Activate() | ||
{ | ||
var scene = SceneManager.GetSceneByName(_desiredScene.DisplayName); | ||
SceneManager.SetActiveScene(scene); | ||
Dispose(); | ||
await _finishExpectant.AsTask(); | ||
if (_desiredScene.ActivateOnLoad) | ||
{ | ||
await Task.Yield(); | ||
ActivateScene(); | ||
} | ||
} | ||
|
||
private void Dispose() | ||
private void ActivateScene() | ||
{ | ||
_loadingExpectant?.Dispose(); | ||
_activationExpectant?.Dispose(); | ||
var loadedScene = SceneManager.GetSceneByName(_desiredScene.DisplayName); | ||
if (loadedScene.IsValid()) | ||
{ | ||
SceneManager.SetActiveScene(loadedScene); | ||
} | ||
} | ||
} | ||
} |
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,8 +1,10 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// © 2023-2024 Nikolay Melnikov <[email protected]> | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Depra.Expectation; | ||
using Depra.Loading.Operations; | ||
using Depra.Scenes.Activation; | ||
using Depra.Scenes.Definitions; | ||
|
@@ -12,23 +14,27 @@ namespace Depra.Scenes.Change | |
{ | ||
public sealed class SceneReloadOperation : ILoadingOperation | ||
{ | ||
private readonly IExpectant _finishExpectant; | ||
private readonly ISceneActivation _activation; | ||
private readonly SceneDefinition _activeScene; | ||
private readonly OperationDescription _description; | ||
private readonly ISceneActivation _activation; | ||
|
||
public SceneReloadOperation(SceneDatabase scenes, ISceneActivation activation) | ||
public SceneReloadOperation(SceneDatabase scenes, ISceneActivation activation, IExpectant finishExpectant = null) | ||
{ | ||
_activeScene = scenes.Active; | ||
_activation = activation; | ||
_activeScene = scenes.Active; | ||
_finishExpectant = finishExpectant; | ||
_description = OperationDescription.Default(_activeScene.DisplayName); | ||
} | ||
|
||
OperationDescription ILoadingOperation.Description => _description; | ||
|
||
public async Task Load(ProgressCallback onProgress, CancellationToken token) | ||
public async Task Load(IProgress<float> progress, CancellationToken token) | ||
{ | ||
await new SceneUnloadOperation(_activeScene, _description).Load(onProgress, token); | ||
await new SceneLoadOperation(_activeScene, _description, _activation).Load(onProgress, token); | ||
await new SceneUnloadOperation(_activeScene, _description) | ||
.Load(progress, token); | ||
await new SceneLoadOperation(_activeScene, _description, _activation, _finishExpectant) | ||
.Load(progress, 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
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.Threading; | ||
using System.Threading.Tasks; | ||
using Depra.Loading.Operations; | ||
|
@@ -22,28 +23,28 @@ public SceneUnloadOperation(SceneDefinition scene, OperationDescription descript | |
|
||
OperationDescription ILoadingOperation.Description => _description; | ||
|
||
public async Task Load(ProgressCallback onProgress, CancellationToken token) | ||
public async Task Load(IProgress<float> progress, CancellationToken token) | ||
{ | ||
if (_scene.CanBeUnloaded == false) | ||
{ | ||
return; | ||
} | ||
|
||
onProgress?.Invoke(0); | ||
progress.Report(0); | ||
var operation = SceneManager.UnloadSceneAsync(_scene.DisplayName); | ||
if (operation == null) | ||
{ | ||
onProgress?.Invoke(1); | ||
progress.Report(1); | ||
return; | ||
} | ||
|
||
while (operation.isDone == false) | ||
{ | ||
onProgress?.Invoke(operation.progress); | ||
progress.Report(operation.progress); | ||
await Task.Yield(); | ||
} | ||
|
||
onProgress?.Invoke(1); | ||
progress.Report(1); | ||
} | ||
} | ||
} |
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