Skip to content

fix: duplicate scene added to scenes in build list #3180

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

Draft
wants to merge 4 commits into
base: develop-2.0.0
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 42 additions & 6 deletions com.unity.netcode.gameobjects/Editor/NetworkManagerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,12 @@ private static void EditorApplication_playModeStateChanged(PlayModeStateChange p
/// </summary>
private static void ScenesInBuildActiveSceneCheck()
{
var scenesList = EditorBuildSettings.scenes.ToList();
var activeScene = SceneManager.GetActiveScene();
var isSceneInBuildSettings = scenesList.Count((c) => c.path == activeScene.path) == 1;
#if UNITY_2023_1_OR_NEWER
var networkManager = Object.FindFirstObjectByType<NetworkManager>();
#else
var networkManager = Object.FindObjectOfType<NetworkManager>();
#endif
if (!isSceneInBuildSettings && networkManager != null)
if (!IsActiveSceneInBuildList() && networkManager != null)
{
if (networkManager.NetworkConfig != null && networkManager.NetworkConfig.EnableSceneManagement)
{
Expand All @@ -105,11 +102,50 @@ private static void ScenesInBuildActiveSceneCheck()
$"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();
// Double check that the scene hasn't already been added to the scenes in build list (potential issue with Parrel Sync)
if (!IsActiveSceneInBuildList())
{
var scenesList = EditorBuildSettings.scenes.ToList();
scenesList.Add(new EditorBuildSettingsScene(SceneManager.GetActiveScene().path, true));
EditorBuildSettings.scenes = scenesList.ToArray();
}
}
}
}
}

private static bool IsActiveSceneInBuildList()
{
var scenesList = EditorBuildSettings.scenes.ToList();
var activeScene = SceneManager.GetActiveScene();
var activeSceneAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(activeScene.path);
var activeSceneHash = activeSceneAsset.GetHashCode();

foreach (var scene in scenesList)
{
var sceneAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(scene.path);
var sceneAssetHash = sceneAsset.GetHashCode();
if (activeSceneHash == sceneAssetHash)
{
return true;
}
if (sceneAsset.name == activeSceneAsset.name)
{
if (scene.path != activeScene.path)
{
Debug.LogError($"Active scene {activeScene.name} with path ({activeScene.path}) is potentially a duplicate of " +
$"scene {sceneAsset.name} with a path of ({scene.path})! Excluding from automatically adding to the scenes in build list!");
}
else
{
Debug.LogError($"Active scene {activeScene.name} was found but the active scene asset hash value ({activeSceneHash}) was " +
$"not the same as the the one in the scenes in build list ({sceneAssetHash})! Excluding from automatically adding to the scenes in build list!");
}
// Exit as if it did find a perfect match
return true;
}
}
return false;
}

/// <summary>
Expand Down
Loading