Skip to content

Commit

Permalink
[v0.0.5] Unloading operation
Browse files Browse the repository at this point in the history
  • Loading branch information
g0dzZz-coder committed Jan 26, 2024
1 parent efca799 commit bc82cb7
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Runtime/ISceneChange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public interface ISceneChange
{
bool IsActive(SceneDefinition scene);

void Switch(SceneDefinition scene);
Task Unload(SceneDefinition scene, CancellationToken token);

Task Reload(IEnumerable<ILoadingOperation> addOperations, CancellationToken token);

Task SwitchAsync(SceneDefinition scene, IEnumerable<ILoadingOperation> addOperations, CancellationToken token);
Task Load(SceneDefinition scene, IEnumerable<ILoadingOperation> addOperations, CancellationToken token);
}
}
37 changes: 14 additions & 23 deletions Runtime/SceneChange.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.Collections.Generic;
using System.Linq;
using System.Threading;
Expand All @@ -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[]
Expand All @@ -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);
}
}
3 changes: 3 additions & 0 deletions Runtime/SceneDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public sealed partial class SceneDefinition
[SerializeField] private string _title;
[SerializeField] private LoadSceneMode _loadMode;
[TextArea] [SerializeField] private string _description;
[SerializeField] private bool _activateOnLoad = true;

public SceneDefinition(string name, LoadSceneMode loadMode)
{
Expand All @@ -27,6 +28,8 @@ public SceneDefinition(string name, LoadSceneMode loadMode)
public LoadSceneMode LoadMode => _loadMode;
public Scene Handle => SceneManager.GetSceneByName(Name);
public string Title => string.IsNullOrEmpty(_title) ? Name : _title;

internal bool ActivateOnLoad => _activateOnLoad;
}

public sealed partial class SceneDefinition : IEquatable<SceneDefinition>
Expand Down
11 changes: 11 additions & 0 deletions Runtime/SceneLoadingOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -34,6 +44,7 @@ async Task ILoadingOperation.Load(Action<float> onProgress, CancellationToken to
}

onProgress?.Invoke(1);
ActivateIfNeeded(_sceneDefinition);
}
}
}
39 changes: 39 additions & 0 deletions Runtime/SceneUnloadingOperation.cs
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);
}
}
}
3 changes: 3 additions & 0 deletions Runtime/SceneUnloadingOperation.cs.meta

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.depra.scenes",
"version": "0.0.4",
"version": "0.0.5",
"displayName": "Depra.Scenes",
"description": "",
"unity": "2022.3",
Expand Down

0 comments on commit bc82cb7

Please sign in to comment.