Skip to content

Commit

Permalink
[v0.1.18] Dependency updated; Test new scene activation
Browse files Browse the repository at this point in the history
  • Loading branch information
g0dzZz-coder committed May 17, 2024
1 parent 67fcab7 commit dafab0e
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 66 deletions.
30 changes: 30 additions & 0 deletions Runtime/Activation/CompositeSceneActivation.cs
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);
}
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Activation/CompositeSceneActivation.cs.meta

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

2 changes: 0 additions & 2 deletions Runtime/Activation/EmptySceneActivation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@ public sealed class EmptySceneActivation : ISceneActivation
void ISceneActivation.BeforeLoading(AsyncOperation operation) { }

void ISceneActivation.OnProgress(AsyncOperation operation) { }

void ISceneActivation.AfterLoading() { }
}
}
48 changes: 48 additions & 0 deletions Runtime/Activation/ExternalSceneActivation.cs
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) { }
}
}
3 changes: 3 additions & 0 deletions Runtime/Activation/ExternalSceneActivation.cs.meta

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

2 changes: 0 additions & 2 deletions Runtime/Activation/ISceneActivation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@ public interface ISceneActivation
void BeforeLoading(AsyncOperation operation);

void OnProgress(AsyncOperation operation);

void AfterLoading();
}
}
2 changes: 0 additions & 2 deletions Runtime/Activation/PercentageSceneActivation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@ void ISceneActivation.OnProgress(AsyncOperation operation)
operation.allowSceneActivation = true;
}
}

void ISceneActivation.AfterLoading() { }
}
}
16 changes: 8 additions & 8 deletions Runtime/Operations/SceneChangeOperation.cs
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;
Expand All @@ -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);
}
}
}
65 changes: 26 additions & 39 deletions Runtime/Operations/SceneLoadOperation.cs
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;
Expand All @@ -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);
}
}
}
}
18 changes: 12 additions & 6 deletions Runtime/Operations/SceneReloadOperation.cs
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;
Expand All @@ -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);
}
}
}
11 changes: 6 additions & 5 deletions Runtime/Operations/SceneUnloadOperation.cs
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;
Expand All @@ -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);
}
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "com.depra.scenes",
"version": "0.1.17",
"version": "0.1.18",
"displayName": "Depra.Scenes",
"description": "",
"unity": "2022.3",
"license": "Apache-2.0",
"dependencies": {
"com.depra.loading.unity": "0.0.7",
"com.depra.loading.unity": "0.1.7",
"com.depra.inspector.unity": "0.0.5"
},
"keywords": [
Expand Down

0 comments on commit dafab0e

Please sign in to comment.