@@ -17,7 +17,7 @@ public static class NetworkSceneManager
1717 internal static readonly HashSet < string > registeredSceneNames = new HashSet < string > ( ) ;
1818 internal static readonly Dictionary < string , uint > sceneNameToIndex = new Dictionary < string , uint > ( ) ;
1919 internal static readonly Dictionary < uint , string > sceneIndexToString = new Dictionary < uint , string > ( ) ;
20- internal static Dictionary < Guid , SwitchSceneProgress > switchSceneProgresses = new Dictionary < Guid , SwitchSceneProgress > ( ) ;
20+ internal static Dictionary < Guid , SceneSwitchProgress > sceneSwitchProgresses = new Dictionary < Guid , SceneSwitchProgress > ( ) ;
2121 private static Scene lastScene ;
2222 private static Scene nextScene ;
2323 private static bool isSwitching = false ;
@@ -41,7 +41,7 @@ internal static void SetCurrentSceneIndex ()
4141 /// Switches to a scene with a given name. Can only be called from Server
4242 /// </summary>
4343 /// <param name="sceneName">The name of the scene to switch to</param>
44- public static SwitchSceneProgress SwitchScene ( string sceneName )
44+ public static SceneSwitchProgress SwitchScene ( string sceneName )
4545 {
4646 if ( ! NetworkingManager . singleton . NetworkConfig . EnableSceneSwitching )
4747 {
@@ -63,8 +63,8 @@ public static SwitchSceneProgress SwitchScene(string sceneName)
6363 isSwitching = true ;
6464 lastScene = SceneManager . GetActiveScene ( ) ;
6565
66- SwitchSceneProgress switchSceneProgress = new SwitchSceneProgress ( ) ;
67- switchSceneProgresses . Add ( switchSceneProgress . guid , switchSceneProgress ) ;
66+ SceneSwitchProgress switchSceneProgress = new SceneSwitchProgress ( ) ;
67+ sceneSwitchProgresses . Add ( switchSceneProgress . guid , switchSceneProgress ) ;
6868 CurrentSceneSwitchProgressGuid = switchSceneProgress . guid ;
6969
7070 AsyncOperation sceneLoad = SceneManager . LoadSceneAsync ( sceneName , LoadSceneMode . Additive ) ;
@@ -201,114 +201,19 @@ internal static void OnClientSwitchSceneCompleted(uint clientId, Guid switchScen
201201 //If Guid is empty it means the client has loaded the start scene of the server and the server would never have a switchSceneProgresses created for the start scene.
202202 return ;
203203 }
204- if ( ! switchSceneProgresses . ContainsKey ( switchSceneGuid ) )
204+ if ( ! sceneSwitchProgresses . ContainsKey ( switchSceneGuid ) )
205205 {
206206 return ;
207207 }
208208
209- switchSceneProgresses [ switchSceneGuid ] . AddClientAsDone ( clientId ) ;
209+ sceneSwitchProgresses [ switchSceneGuid ] . AddClientAsDone ( clientId ) ;
210210 }
211211
212212
213213 internal static void removeClientFromSceneSwitchProgresses ( uint clientId )
214214 {
215- foreach ( SwitchSceneProgress switchSceneProgress in switchSceneProgresses . Values )
215+ foreach ( SceneSwitchProgress switchSceneProgress in sceneSwitchProgresses . Values )
216216 switchSceneProgress . RemoveClientAsDone ( clientId ) ;
217217 }
218218 }
219-
220- /// <summary>
221- /// Class for tracking scene switching progress by server and clients.
222- /// </summary>
223- public class SwitchSceneProgress
224- {
225- /// <summary>
226- /// List of clientIds of those clients that is done loading the scene.
227- /// </summary>
228- public List < uint > DoneClients { get ; } = new List < uint > ( ) ;
229- /// <summary>
230- /// The NetworkTime time at the moment the scene switch was initiated by the server.
231- /// </summary>
232- public float TimeAtInitiation { get ; } = NetworkingManager . singleton . NetworkTime ;
233- /// <summary>
234- /// Delegate type for when the switch scene progress is completed. Either by all clients done loading the scene or by time out.
235- /// </summary>
236- public delegate void OnCompletedDelegate ( bool timedOut ) ;
237- /// <summary>
238- /// The callback invoked when the switch scene progress is completed. Either by all clients done loading the scene or by time out.
239- /// </summary>
240- public event OnCompletedDelegate OnComplete ;
241- /// <summary>
242- /// Is this scene switch progresses completed, all clients are done loading the scene or a timeout has occured.
243- /// </summary>
244- public bool isCompleted { get ; private set ; }
245- /// <summary>
246- /// If all clients are done loading the scene, at the moment of completed.
247- /// </summary>
248- public bool isAllClientsDoneLoading { get ; private set ; }
249- /// <summary>
250- /// Delegate type for when a client is done loading the scene.
251- /// </summary>
252- public delegate void OnClientLoadedSceneDelegate ( uint clientId ) ;
253- /// <summary>
254- /// The callback invoked when a client is done loading the scene.
255- /// </summary>
256- public event OnClientLoadedSceneDelegate OnClientLoadedScene ;
257-
258- internal Guid guid { get ; } = Guid . NewGuid ( ) ;
259-
260- private Coroutine timeOutCoroutine ;
261- private AsyncOperation sceneLoadOperation ;
262-
263- internal SwitchSceneProgress ( )
264- {
265- timeOutCoroutine = NetworkingManager . singleton . StartCoroutine ( NetworkingManager . singleton . TimeOutSwitchSceneProgress ( this ) ) ;
266- }
267-
268- internal void AddClientAsDone ( uint clientId )
269- {
270- DoneClients . Add ( clientId ) ;
271- if ( OnClientLoadedScene != null )
272- OnClientLoadedScene . Invoke ( clientId ) ;
273- CheckCompletion ( ) ;
274- }
275-
276- internal void RemoveClientAsDone ( uint clientId )
277- {
278- DoneClients . Remove ( clientId ) ;
279- CheckCompletion ( ) ;
280- }
281-
282- internal void SetSceneLoadOperation ( AsyncOperation sceneLoadOperation )
283- {
284- this . sceneLoadOperation = sceneLoadOperation ;
285- this . sceneLoadOperation . completed += ( AsyncOperation operation ) => { CheckCompletion ( ) ; } ;
286- }
287-
288- internal void CheckCompletion ( )
289- {
290- if ( ! isCompleted && DoneClients . Count == NetworkingManager . singleton . ConnectedClientsList . Count && sceneLoadOperation . isDone )
291- {
292- isCompleted = true ;
293- isAllClientsDoneLoading = true ;
294- NetworkSceneManager . switchSceneProgresses . Remove ( guid ) ;
295- if ( OnComplete != null )
296- OnComplete . Invoke ( false ) ;
297-
298- NetworkingManager . singleton . StopCoroutine ( timeOutCoroutine ) ;
299- }
300- }
301-
302- internal void SetTimedOut ( )
303- {
304- if ( ! isCompleted )
305- {
306- isCompleted = true ;
307- NetworkSceneManager . switchSceneProgresses . Remove ( guid ) ;
308- if ( OnComplete != null )
309- OnComplete . Invoke ( true ) ;
310- }
311- }
312-
313- }
314219}
0 commit comments