Skip to content

Commit 1a171e1

Browse files
fix: NetworkSceneManager notifications for scene not in build settings [MTT-2941] (#1828)
* fix This provides users with more meaningful information if they are using something like ParrelSync or Multiplayer Virtual projects and encounter the Scene hash does not exist in the HashToBuildIndex table exception. * fix This is an editor side only fix to determine if the currently opened and active scene in the editor is not in the build list and if a NetworkManager instance exists that has scene management enabled. If so, then it will display a dialog box informing the user that the scene should be added to the scenes in build list as well as provide the user the option to have it done for them. * fix Fixing issue for recent update to where Unity Transport lives for Unity versions v2022.0a5 or higher. * fix * Style Added summary for ScenesInBuildActiveSceneCheck * Update CHANGELOG.md * Update CHANGELOG.md MTT-2941 Final changelog modification for better user notification clarity.
1 parent 594239c commit 1a171e1

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
99
## [Unreleased]
1010

1111
### Added
12-
12+
- Added editor only check prior to entering into play mode if the currently open and active scene is in the build list and if not displays a dialog box asking the user if they would like to automatically add it prior to entering into play mode. (#1828)
1313
- Added `UnityTransport` implementation and `com.unity.transport` package dependency (#1823)
1414
- Added `NetworkVariableWritePermission` to `NetworkVariableBase` and implemented `Owner` client writable netvars. (#1762)
1515

@@ -20,6 +20,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
2020
- Removed `com.unity.modules.animation`, `com.unity.modules.physics` and `com.unity.modules.physics2d` dependencies from the package (#1812)
2121

2222
### Fixed
23+
- Fixed clarity for NetworkSceneManager client side notification when it receives a scene hash value that does not exist in its local hash table. (#1828)
2324
- Fixed client throws a key not found exception when it times out using UNet or UTP. (#1821)
2425
- Fixed network variable updates are no longer limited to 32,768 bytes when NetworkConfig.EnsureNetworkVariableLengthSafety is enabled. The limits are now determined by what the transport can send in a message. (#1811)
2526
- Fixed in-scene NetworkObjects get destroyed if a client fails to connect and shuts down the NetworkManager. (#1809)

com.unity.netcode.gameobjects/Editor/NetworkManagerHelper.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using UnityEngine;
4+
using UnityEngine.SceneManagement;
35
using UnityEditor;
46

57
namespace Unity.Netcode.Editor
@@ -25,7 +27,6 @@ private static void InitializeOnload()
2527
{
2628
Singleton = new NetworkManagerHelper();
2729
NetworkManager.NetworkManagerHelper = Singleton;
28-
2930
EditorApplication.playModeStateChanged -= EditorApplication_playModeStateChanged;
3031
EditorApplication.hierarchyChanged -= EditorApplication_hierarchyChanged;
3132

@@ -40,11 +41,47 @@ private static void EditorApplication_playModeStateChanged(PlayModeStateChange p
4041
case PlayModeStateChange.ExitingEditMode:
4142
{
4243
s_LastKnownNetworkManagerParents.Clear();
44+
ScenesInBuildActiveSceneCheck();
4345
break;
4446
}
4547
}
4648
}
4749

50+
/// <summary>
51+
/// Detects if a user is trying to enter into play mode when both conditions are true:
52+
/// - the currently active and open scene is not added to the scenes in build list
53+
/// - an instance of a NetworkManager with scene management enabled can be found
54+
/// If both conditions are met then the user is presented with a dialog box that
55+
/// provides the user with the option to add the scene to the scenes in build list
56+
/// before entering into play mode or the user can continue under those conditions.
57+
///
58+
/// ** When scene management is enabled the user should treat all scenes that need to
59+
/// be synchronized using network scene management as if they were preparing for a build.
60+
/// Any scene that needs to be loaded at run time has to be included in the scenes in
61+
/// build list. **
62+
/// </summary>
63+
private static void ScenesInBuildActiveSceneCheck()
64+
{
65+
var scenesList = EditorBuildSettings.scenes.ToList();
66+
var activeScene = SceneManager.GetActiveScene();
67+
var isSceneInBuildSettings = scenesList.Where((c) => c.path == activeScene.path).Count() == 1;
68+
var networkManager = Object.FindObjectOfType<NetworkManager>();
69+
if (!isSceneInBuildSettings && networkManager != null)
70+
{
71+
if (networkManager.NetworkConfig != null && networkManager.NetworkConfig.EnableSceneManagement)
72+
{
73+
if (EditorUtility.DisplayDialog("Add Scene to Scenes in Build", $"The current scene was not found in the scenes" +
74+
$" in build and a {nameof(NetworkManager)} instance was found with scene management enabled! Clients will not be able " +
75+
$"to synchronize to this scene unless it is added to the scenes in build list.\n\nWould you like to add it now?",
76+
"Yes", "No - Continue"))
77+
{
78+
scenesList.Add(new EditorBuildSettingsScene(activeScene.path, true));
79+
EditorBuildSettings.scenes = scenesList.ToArray();
80+
}
81+
}
82+
}
83+
}
84+
4885
/// <summary>
4986
/// Invoked only when the hierarchy changes
5087
/// </summary>

com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,8 @@ internal string ScenePathFromHash(uint sceneHash)
530530
}
531531
else
532532
{
533-
throw new Exception($"Scene Hash {sceneHash} does not exist in the {nameof(HashToBuildIndex)} table!");
533+
throw new Exception($"Scene Hash {sceneHash} does not exist in the {nameof(HashToBuildIndex)} table! Verify that all scenes requiring" +
534+
$" server to client synchronization are in the scenes in build list.");
534535
}
535536
}
536537

0 commit comments

Comments
 (0)