Skip to content

fix: NetworkSceneManager notifications for scene not in build settings [MTT-2941] #1828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3 changes: 2 additions & 1 deletion com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
## [Unreleased]

### Added

- 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)
- Added `UnityTransport` implementation and `com.unity.transport` package dependency (#1823)
- Added `NetworkVariableWritePermission` to `NetworkVariableBase` and implemented `Owner` client writable netvars. (#1762)

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

### Fixed
- Fixed clarity for NetworkSceneManager client side notification when it receives a scene hash value that does not exist in its local hash table. (#1828)
- Fixed client throws a key not found exception when it times out using UNet or UTP. (#1821)
- 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)
- Fixed in-scene NetworkObjects get destroyed if a client fails to connect and shuts down the NetworkManager. (#1809)
Expand Down
39 changes: 38 additions & 1 deletion com.unity.netcode.gameobjects/Editor/NetworkManagerHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEditor;

namespace Unity.Netcode.Editor
Expand All @@ -25,7 +27,6 @@ private static void InitializeOnload()
{
Singleton = new NetworkManagerHelper();
NetworkManager.NetworkManagerHelper = Singleton;

EditorApplication.playModeStateChanged -= EditorApplication_playModeStateChanged;
EditorApplication.hierarchyChanged -= EditorApplication_hierarchyChanged;

Expand All @@ -40,11 +41,47 @@ private static void EditorApplication_playModeStateChanged(PlayModeStateChange p
case PlayModeStateChange.ExitingEditMode:
{
s_LastKnownNetworkManagerParents.Clear();
ScenesInBuildActiveSceneCheck();
break;
}
}
}

/// <summary>
/// Detects if a user is trying to enter into play mode when both conditions are true:
/// - the currently active and open scene is not added to the scenes in build list
/// - an instance of a NetworkManager with scene management enabled can be found
/// If both conditions are met then the user is presented with a dialog box that
/// provides the user with the option to add the scene to the scenes in build list
/// before entering into play mode or the user can continue under those conditions.
///
/// ** When scene management is enabled the user should treat all scenes that need to
/// be synchronized using network scene management as if they were preparing for a build.
/// Any scene that needs to be loaded at run time has to be included in the scenes in
/// build list. **
/// </summary>
private static void ScenesInBuildActiveSceneCheck()
{
var scenesList = EditorBuildSettings.scenes.ToList();
var activeScene = SceneManager.GetActiveScene();
var isSceneInBuildSettings = scenesList.Where((c) => c.path == activeScene.path).Count() == 1;
var networkManager = Object.FindObjectOfType<NetworkManager>();
if (!isSceneInBuildSettings && networkManager != null)
{
if (networkManager.NetworkConfig != null && networkManager.NetworkConfig.EnableSceneManagement)
{
if (EditorUtility.DisplayDialog("Add Scene to Scenes in Build", $"The current scene was not found in the scenes" +
$" in build and a {nameof(NetworkManager)} instance was found with scene management enabled! Clients will not be able " +
$"to synchronize to this scene unless it is added to the scenes in build list.\n\nWould you like to add it now?",
"Yes", "No - Continue"))
{
scenesList.Add(new EditorBuildSettingsScene(activeScene.path, true));
EditorBuildSettings.scenes = scenesList.ToArray();
}
}
}
}

/// <summary>
/// Invoked only when the hierarchy changes
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,8 @@ internal string ScenePathFromHash(uint sceneHash)
}
else
{
throw new Exception($"Scene Hash {sceneHash} does not exist in the {nameof(HashToBuildIndex)} table!");
throw new Exception($"Scene Hash {sceneHash} does not exist in the {nameof(HashToBuildIndex)} table! Verify that all scenes requiring" +
$" server to client synchronization are in the scenes in build list.");
}
}

Expand Down