Skip to content

Commit 0cd8c39

Browse files
authored
fix(NetworkScenePostProcess): Only process the last loaded scene (#4076)
* fix(NetworkScenePostProcess): Only process the last loaded scene - Faster when there are a lot of additive scenes - Avoids erroneous halts for unspawned objects in other scenes - Fixes #4066 * improved code + Debug.Log (commented out)
1 parent f83cd39 commit 0cd8c39

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

Assets/Mirror/Editor/NetworkScenePostProcess.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using UnityEditor;
44
using UnityEditor.Callbacks;
55
using UnityEngine;
6+
using UnityEngine.SceneManagement;
67

78
namespace Mirror
89
{
@@ -11,10 +12,16 @@ public class NetworkScenePostProcess : MonoBehaviour
1112
[PostProcessScene]
1213
public static void OnPostProcessScene()
1314
{
15+
Scene lastLoadedScene = SceneManager.GetSceneAt(SceneManager.sceneCount - 1);
16+
//Debug.Log($"[Mirror] OnPostProcessScene for scene '{lastLoadedScene.name}'");
17+
1418
// find all NetworkIdentities in all scenes
1519
// => can't limit it to GetActiveScene() because that wouldn't work
1620
// for additive scene loads (the additively loaded scene is never
1721
// the active scene)
22+
// => Should limit to the last scene loaded per SceneManager.sceneCount index
23+
// so we don't catch instantiated unspawned objects from other scenes
24+
// and only processing a single scene should be faster too.
1825
// => ignore DontDestroyOnLoad scene! this avoids weird situations
1926
// like in NetworkZones when we destroy the local player and
2027
// load another scene afterwards, yet the local player is still
@@ -26,6 +33,7 @@ public static void OnPostProcessScene()
2633
.Where(identity => identity.gameObject.hideFlags != HideFlags.NotEditable &&
2734
identity.gameObject.hideFlags != HideFlags.HideAndDontSave &&
2835
identity.gameObject.scene.name != "DontDestroyOnLoad" &&
36+
identity.gameObject.scene == lastLoadedScene &&
2937
!Utils.IsPrefab(identity.gameObject));
3038

3139
foreach (NetworkIdentity identity in identities)

0 commit comments

Comments
 (0)