Skip to content

Commit

Permalink
[v0.0.1] Scene definition & change service
Browse files Browse the repository at this point in the history
  • Loading branch information
g0dzZz-coder committed Jan 26, 2024
1 parent 040ffad commit 61be258
Show file tree
Hide file tree
Showing 19 changed files with 238 additions and 35 deletions.
15 changes: 0 additions & 15 deletions Editor/Depra.Template.Editor.asmdef

This file was deleted.

7 changes: 7 additions & 0 deletions LICENSE.md.meta

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

7 changes: 7 additions & 0 deletions README.md.meta

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

8 changes: 8 additions & 0 deletions Runtime.meta

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

20 changes: 20 additions & 0 deletions Runtime/Depra.Scenes.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "Depra.Scenes",
"rootNamespace": "Depra.Scenes",
"references": [
"GUID:36e952f101acb024da7ad1c7de888799",
"GUID:dae2464e94296ca48971481784720efc"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [
"Depra.Assets.dll",
"Depra.Loading.dll"
],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Runtime/Depra.Scenes.asmdef.meta

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

13 changes: 0 additions & 13 deletions Runtime/Depra.Template.asmdef

This file was deleted.

21 changes: 21 additions & 0 deletions Runtime/ISceneChange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: Apache-2.0
// © 2023-2024 Nikolay Melnikov <[email protected]>

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Depra.Loading.Operations;

namespace Depra.Scenes
{
public interface ISceneChange
{
bool IsActive(SceneDefinition scene);

void Switch(SceneDefinition scene);

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

Task SwitchAsync(SceneDefinition scene, IEnumerable<ILoadingOperation> addOperations, CancellationToken token);
}
}
3 changes: 3 additions & 0 deletions Runtime/ISceneChange.cs.meta

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

57 changes: 57 additions & 0 deletions Runtime/SceneChange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// SPDX-License-Identifier: Apache-2.0
// © 2023-2024 Nikolay Melnikov <[email protected]>

using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Depra.Loading.Curtain;
using Depra.Loading.Operations;
using UnityEngine.SceneManagement;

namespace Depra.Scenes
{
public sealed class SceneChange : ISceneChange
{
private readonly ILoadingCurtain _loadingCurtain;
private SceneDefinition _currentScene;

public SceneChange(SceneDefinition initialScene, ILoadingCurtain loadingCurtain)
{
_currentScene = initialScene;
_loadingCurtain = loadingCurtain;
}

private async Task SwitchAsyncInternal(SceneDefinition scene, IEnumerable<ILoadingOperation> addOperations,
CancellationToken token)
{
var operations = addOperations.Concat(new[]
{ new SceneLoadingOperation(scene, OperationDescription.Default(scene.Name)) });

await _loadingCurtain.Load(operations, token);
_loadingCurtain.Unload();
}

public bool IsActive(SceneDefinition scene) =>
scene == _currentScene || scene.Name == SceneManager.GetActiveScene().name;

void ISceneChange.Switch(SceneDefinition scene)
{
if (IsActive(scene))
{
throw new UnexpectedSceneSwitch(scene.Name);
}

_currentScene = scene;
SceneManager.LoadScene(_currentScene.Name, _currentScene.LoadMode);
}

Task ISceneChange.SwitchAsync(SceneDefinition scene, IEnumerable<ILoadingOperation> addOperations,
CancellationToken token) => IsActive(scene)
? throw new UnexpectedSceneSwitch(scene.Name)
: SwitchAsyncInternal(_currentScene = scene, addOperations, token);

Task ISceneChange.Reload(IEnumerable<ILoadingOperation> addOperations, CancellationToken token) =>
SwitchAsyncInternal(_currentScene, addOperations, token);
}
}
3 changes: 3 additions & 0 deletions Runtime/SceneChange.cs.meta

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

32 changes: 32 additions & 0 deletions Runtime/SceneDefinition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: Apache-2.0
// © 2023-2024 Nikolay Melnikov <[email protected]>

using System;
using Depra.Inspector.Attributes;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Depra.Scenes
{
[Serializable]
public sealed class SceneDefinition : IEquatable<SceneDefinition>
{
[Scene] [SerializeField] private string _name;
[SerializeField] private LoadSceneMode _loadMode;

public static bool operator ==(SceneDefinition a, SceneDefinition b) => a?.Equals(b) ?? b is null;

public static bool operator !=(SceneDefinition a, SceneDefinition b) => !(a == b);

public string Name => _name;
public LoadSceneMode LoadMode => _loadMode;

public bool Equals(SceneDefinition other) => other != null && Name == other.Name;

public override bool Equals(object obj) => obj is SceneDefinition other && Equals(other);

public override int GetHashCode() => Name.GetHashCode();

public override string ToString() => Name;
}
}
3 changes: 3 additions & 0 deletions Runtime/SceneDefinition.cs.meta

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

39 changes: 39 additions & 0 deletions Runtime/SceneLoadingOperation.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 SceneLoadingOperation : ILoadingOperation
{
private readonly SceneDefinition _sceneDefinition;

public SceneLoadingOperation(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.LoadSceneAsync(_sceneDefinition.Name, _sceneDefinition.LoadMode);
operation.allowSceneActivation = true;

while (operation.isDone == false)
{
onProgress?.Invoke(operation.progress);
await Task.Yield();
}

onProgress?.Invoke(1);
}
}
}
3 changes: 3 additions & 0 deletions Runtime/SceneLoadingOperation.cs.meta

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

12 changes: 12 additions & 0 deletions Runtime/UnexpectedSceneSwitch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: Apache-2.0
// © 2023-2024 Nikolay Melnikov <[email protected]>

using System;

namespace Depra.Scenes
{
internal sealed class UnexpectedSceneSwitch : Exception
{
public UnexpectedSceneSwitch(string sceneName) : base($"Unexpected switch to same scene '{sceneName}'!") { }
}
}
3 changes: 3 additions & 0 deletions Runtime/UnexpectedSceneSwitch.cs.meta

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

17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
{
"name": "com.depra.template",
"name": "com.depra.scenes",
"version": "0.0.1",
"displayName": "Unity Package Template",
"description": "Template for creating Unity game engine packages.",
"displayName": "Depra.Scenes",
"description": "",
"unity": "2022.3",
"license": "Apache-2.0",
"dependencies": {},
"keywords": [],
"keywords": [
"depra",
"unity",
"scenes"
],
"author": {
"name": "Depra, Inc.",
"email": "[email protected]",
"url": "https://github.com/Depra-Inc"
},
"repository": {
"type": "git",
"url": "https://github.com/Depra-Inc/Template.Package.Unity.git"
"url": "https://github.com/Depra-Inc/Scenes.git"
}
}
}
3 changes: 3 additions & 0 deletions package.json.meta

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

0 comments on commit 61be258

Please sign in to comment.