Skip to content

Commit ba02e05

Browse files
fix: loading screen progress not always synced [MTT-6302] (#862)
* fixing syncing issue of loading progress
1 parent 910d6c2 commit ba02e05

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

Packages/com.unity.multiplayer.samples.coop/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Multiplayer Samples Co-op Changelog
22

3+
## [Unreleased]
4+
5+
### Fixed
6+
* Host loading progress bar is now properly updated on all clients during scene loads (#862)
7+
38
## [1.6.1] - 2023-06-14
49

510
### Fixed

Packages/com.unity.multiplayer.samples.coop/Utilities/SceneManagement/LoadingProgressManager.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public AsyncOperation LocalLoadOperation
2727
{
2828
set
2929
{
30+
m_IsLoading = true;
3031
LocalProgress = 0;
3132
m_LocalLoadOperation = value;
3233
}
@@ -36,6 +37,8 @@ public AsyncOperation LocalLoadOperation
3637

3738
float m_LocalProgress;
3839

40+
bool m_IsLoading;
41+
3942
/// <summary>
4043
/// This event is invoked each time the dictionary of progress trackers is updated (if one is removed or added, for example.)
4144
/// </summary>
@@ -51,7 +54,7 @@ public float LocalProgress
5154
ProgressTrackers[NetworkManager.LocalClientId].Progress.Value : m_LocalProgress;
5255
private set
5356
{
54-
if (IsSpawned && ProgressTrackers.ContainsKey(NetworkManager.LocalClientId))
57+
if (IsSpawned && ProgressTrackers.ContainsKey(NetworkManager.LocalClientId) && ProgressTrackers[NetworkManager.LocalClientId].IsSpawned)
5558
{
5659
ProgressTrackers[NetworkManager.LocalClientId].Progress.Value = value;
5760
}
@@ -84,9 +87,17 @@ public override void OnNetworkDespawn()
8487

8588
void Update()
8689
{
87-
if (m_LocalLoadOperation != null)
90+
if (m_LocalLoadOperation != null && m_IsLoading)
8891
{
89-
LocalProgress = m_LocalLoadOperation.isDone ? 1 : m_LocalLoadOperation.progress;
92+
if (m_LocalLoadOperation.isDone)
93+
{
94+
m_IsLoading = false;
95+
LocalProgress = 1;
96+
}
97+
else
98+
{
99+
LocalProgress = m_LocalLoadOperation.progress;
100+
}
90101
}
91102
}
92103

@@ -137,10 +148,5 @@ void RemoveTracker(ulong clientId)
137148
}
138149
}
139150
}
140-
141-
public void ResetLocalProgress()
142-
{
143-
LocalProgress = 0;
144-
}
145151
}
146152
}

Packages/com.unity.multiplayer.samples.coop/Utilities/SceneManagement/NetworkedLoadingProgressTracker.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Unity.Netcode;
2+
using UnityEngine;
23

34
namespace Unity.Multiplayer.Samples.Utilities
45
{
@@ -11,5 +12,10 @@ public class NetworkedLoadingProgressTracker : NetworkBehaviour
1112
/// The current loading progress associated with the owner of this NetworkBehavior
1213
/// </summary>
1314
public NetworkVariable<float> Progress { get; } = new NetworkVariable<float>(0, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);
15+
16+
void Awake()
17+
{
18+
DontDestroyOnLoad(this);
19+
}
1420
}
1521
}

Packages/com.unity.multiplayer.samples.coop/Utilities/SceneManagement/SceneLoaderWrapper.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ void OnSceneEvent(SceneEvent sceneEvent)
154154
if (NetworkManager.IsClient)
155155
{
156156
m_ClientLoadingScreen.StopLoadingScreen();
157-
m_LoadingProgressManager.ResetLocalProgress();
158157
}
159158
break;
160159
case SceneEventType.Synchronize: // Server told client to start synchronizing scenes

0 commit comments

Comments
 (0)