-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
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); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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}'!") { } | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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" | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.