Skip to content

Commit d15a8d7

Browse files
committed
Improve loading extensibility
1 parent 0f0d17e commit d15a8d7

File tree

7 files changed

+92
-27
lines changed

7 files changed

+92
-27
lines changed

Runtime/Loading/LoadingBase.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* ILoading.cs
3+
* Created by: João Borks [joao.borks@gmail.com]
4+
* Created on: 9/3/2022 (en-US)
5+
*/
6+
7+
using System;
8+
using UnityEngine;
9+
10+
namespace MyUnityTools.SceneLoading
11+
{
12+
/// <summary>
13+
/// Base class to standardize the loading screen behavior.
14+
/// Also implements <see cref="IProgress{T}"/>.
15+
/// </summary>
16+
public abstract class LoadingBase : MonoBehaviour, IProgress<float>
17+
{
18+
/// <summary>
19+
/// Event that reports the scene load progression in percentage.
20+
/// </summary>
21+
public event SceneLoadProgressDelegate OnProgress;
22+
/// <summary>
23+
/// Event that reports when the loading has been completed.
24+
/// </summary>
25+
public event Action OnLoadingComplete;
26+
27+
/// <summary>
28+
/// Controls if the loading screen is in an active state, in other words, if it's currently in focus.
29+
/// This is required for the <see cref="ISceneLoader.TransitionToScene(ILoadSceneInfo, ILoadSceneInfo)"/> to know when it should perform its internal operations.
30+
/// Before the loading screen can be actually seen, <see cref="Active"/> should be set to false.
31+
/// For example, if the loading screen is being faded in, or if an animation is transitioning to it.
32+
/// Then, once it's completely visible, <see cref="Active"/> should be set to true. The loading process starts.
33+
/// Once the target scene has finished loading, then the loading screen gets faded out or transitions out.
34+
/// After the transition has been completed and the loading screen is no longer visible, <see cref="Active"/>
35+
/// should be set to false again.
36+
/// </summary>
37+
public bool Active { get; protected set; }
38+
39+
[SerializeField, Tooltip("Common scene operations stop at 90%, but addressable scene operations go all the way up to 100%. Enabling this value reduces the ratio to 90% instead of 100%")]
40+
protected bool _reduceLoadRatio;
41+
42+
/// <summary>
43+
/// Default ratio to consider when displaying the loading progress bar.
44+
/// The <see cref="UnityEngine.SceneManagement.SceneManager"/> operations stop at 90%, so you might want to set that to 0.9f.
45+
/// The addressable scene operations go all the way up to 100%.
46+
/// </summary>
47+
protected float _ratio = 1f;
48+
49+
protected virtual void Awake() => _ratio = _reduceLoadRatio ? .9f : 1f;
50+
51+
/// <summary>
52+
/// Reports that the loading of the target scene has been completed, and the loading view can transition out of the screen.
53+
/// </summary>
54+
public virtual void CompleteLoading() => OnLoadingComplete?.Invoke();
55+
56+
/// <summary>
57+
/// Reports the current scene load progression in percentage.
58+
/// </summary>
59+
/// <param name="progress">The percentage of the scene load progression.</param>
60+
public virtual void Report(float progress) => OnProgress?.Invoke(progress / _ratio);
61+
}
62+
63+
/// <summary>
64+
/// Delegate to propagate the scene load progress in percentage.
65+
/// </summary>
66+
/// <param name="progress">The percentage value of the load progression from 0 to 1.</param>
67+
public delegate void SceneLoadProgressDelegate(float progress);
68+
}

Runtime/Loading/LoadingBase.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Loading/LoadingBehavior.cs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,16 @@
44
* Created on: 7/23/2022 (en-US)
55
*/
66

7-
using System;
87
using UnityEngine;
98

109
namespace MyUnityTools.SceneLoading
1110
{
12-
public delegate void SceneLoadProgressDelegate(float progress);
13-
14-
public class LoadingBehavior : MonoBehaviour, IProgress<float>
11+
public class LoadingBehavior : LoadingBase
1512
{
16-
public event SceneLoadProgressDelegate OnProgress;
17-
public event Action OnLoadingComplete;
18-
19-
public bool Active { get; private set; }
20-
21-
[SerializeField]
13+
[SerializeField, Tooltip("Should it wait for an animation or script to allow starting the transition? If not, then enable this toggle.")]
2214
bool _autoStart;
23-
[SerializeField]
15+
[SerializeField, Tooltip("Should it wait for an animation or script to allow finishing the transition? If not, then enable this toggle.")]
2416
bool _autoFinish;
25-
[SerializeField]
26-
bool _reduceLoadRatio;
27-
28-
float _ratio;
29-
30-
void Awake() => _ratio = _reduceLoadRatio ? .9f : 1f;
3117

3218
void Start()
3319
{
@@ -37,13 +23,11 @@ void Start()
3723

3824
public void SetLoadingActive(bool value) => Active = value;
3925

40-
public void CompleteLoading()
26+
public override void CompleteLoading()
4127
{
42-
OnLoadingComplete?.Invoke();
28+
base.CompleteLoading();
4329
if (_autoFinish)
4430
SetLoadingActive(false);
4531
}
46-
47-
public void Report(float progress) => OnProgress?.Invoke(progress / _ratio);
4832
}
4933
}

Runtime/SceneLoaders/AddressableSceneLoaderAsync.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ async Task<SceneInstance> TransitionToSceneFlowAsync(IAddressableLoadSceneRefere
5858
{
5959
var currentSceneHandle = _sceneManager.GetActiveSceneHandle();
6060
var loadingScene = await intermediateSceneReference.LoadSceneAsync(_sceneManager).Task;
61-
var loadingBehavior = Object.FindObjectOfType<LoadingBehavior>();
61+
var loadingBehavior = Object.FindObjectOfType<LoadingBase>();
6262

6363
SceneInstance result;
6464

@@ -82,7 +82,8 @@ async Task<SceneInstance> TransitionToSceneFlowAsync(IAddressableLoadSceneRefere
8282
else
8383
{
8484
result = await LoadSceneAsync(sceneReference, true);
85-
_ = UnloadSceneAsync(new AddressableLoadSceneInfoOperationHandle(currentSceneHandle));
85+
if (currentSceneHandle.IsValid())
86+
_ = UnloadSceneAsync(new AddressableLoadSceneInfoOperationHandle(currentSceneHandle));
8687
_ = UnloadSceneAsync(new AddressableLoadSceneInfoInstance(loadingScene));
8788
}
8889
return result;

Runtime/SceneLoaders/AddressableSceneLoaderUniTask.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ async UniTask<SceneInstance> TransitionToSceneFlowAsync(IAddressableLoadSceneRef
5454
{
5555
var currentSceneHandle = _sceneManager.GetActiveSceneHandle();
5656
var loadingScene = await intermediateSceneReference.LoadSceneAsync(_sceneManager).Task;
57-
var loadingBehavior = Object.FindObjectOfType<LoadingBehavior>();
57+
var loadingBehavior = Object.FindObjectOfType<LoadingBase>();
5858

5959
SceneInstance result;
6060

@@ -77,7 +77,8 @@ async UniTask<SceneInstance> TransitionToSceneFlowAsync(IAddressableLoadSceneRef
7777
else
7878
{
7979
result = await LoadSceneAsync(sceneReference, true);
80-
UnloadSceneAsync(new AddressableLoadSceneInfoOperationHandle(currentSceneHandle)).Forget();
80+
if (currentSceneHandle.IsValid())
81+
UnloadSceneAsync(new AddressableLoadSceneInfoOperationHandle(currentSceneHandle)).Forget();
8182
UnloadSceneAsync(new AddressableLoadSceneInfoInstance(loadingScene)).Forget();
8283
}
8384
return result;

Runtime/SceneLoaders/SceneLoaderAsync.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ async Task TransitionWithIntermediateAsync(ILoadSceneInfo targetSceneInfo, ILoad
4242
var currentSceneInfo = new LoadSceneInfoIndex(SceneManager.GetActiveScene().buildIndex);
4343
await LoadSceneAsync(intermediateSceneInfo, true);
4444

45-
var loadingBehavior = Object.FindObjectOfType<LoadingBehavior>();
45+
var loadingBehavior = Object.FindObjectOfType<LoadingBase>();
4646
if (loadingBehavior)
4747
{
4848
while (!loadingBehavior.Active)

Runtime/SceneLoaders/SceneLoaderUniTask.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async UniTask TransitionWithIntermediateAsync(ILoadSceneInfo targetSceneInfo, IL
3636
var currentSceneInfo = new LoadSceneInfoIndex(SceneManager.GetActiveScene().buildIndex);
3737
await LoadSceneAsync(intermediateSceneInfo, true);
3838

39-
var loadingBehavior = Object.FindObjectOfType<LoadingBehavior>();
39+
var loadingBehavior = Object.FindObjectOfType<LoadingBase>();
4040
if (loadingBehavior)
4141
{
4242
await UniTask.WaitWhile(() => !loadingBehavior.Active);

0 commit comments

Comments
 (0)