From 2fe5c427b287baba14c227faa795a363bcda5461 Mon Sep 17 00:00:00 2001 From: Alon Farchy Date: Thu, 14 Nov 2024 14:53:59 -0600 Subject: [PATCH 01/12] Support scenes preloaded from addressables --- .../SceneManagement/NetworkSceneManager.cs | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs index 65222007bf..059b594909 100644 --- a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs @@ -525,6 +525,16 @@ internal bool RemoveServerClientSceneHandle(int serverHandle, int clientHandle) /// internal Dictionary BuildIndexToHash = new Dictionary(); + /// + /// Hash to external scene path lookup table + /// + internal Dictionary HashToExternalScenePath = new Dictionary(); + + /// + /// External scene name to hash lookup table + /// + internal Dictionary ExternalScenePathToHash = new Dictionary(); + /// /// The Condition: While a scene is asynchronously loaded in single loading scene mode, if any new NetworkObjects are spawned /// they need to be moved into the do not destroy temporary scene @@ -706,6 +716,22 @@ internal void GenerateScenesInBuild() } } + /// + /// Register scene from outside of build (e.g. from an Addressables group). + /// + /// The paths of the external scenes to register. + public void RegisterExternalScenes(params string[] scenePaths) + { + HashToExternalScenePath.Clear(); + ExternalScenePathToHash.Clear(); + foreach (var scenePath in scenePaths) + { + var hash = XXHash.Hash32(scenePath); + HashToExternalScenePath.Add(hash, scenePath); + ExternalScenePathToHash.Add(scenePath, hash); + } + } + /// /// Gets the scene name from a hash value generated from the full scene path /// @@ -727,7 +753,11 @@ internal string SceneNameFromHash(uint sceneHash) /// internal string ScenePathFromHash(uint sceneHash) { - if (HashToBuildIndex.ContainsKey(sceneHash)) + if (HashToExternalScenePath.TryGetValue(sceneHash, out var externalScenePath)) + { + return externalScenePath; + } + else if (HashToBuildIndex.ContainsKey(sceneHash)) { return SceneUtility.GetScenePathByBuildIndex(HashToBuildIndex[sceneHash]); } @@ -743,6 +773,11 @@ internal string ScenePathFromHash(uint sceneHash) /// internal uint SceneHashFromNameOrPath(string sceneNameOrPath) { + if (ExternalScenePathToHash.TryGetValue(sceneNameOrPath, out var externalSceneHash)) + { + return externalSceneHash; + } + var buildIndex = SceneUtility.GetBuildIndexByScenePath(sceneNameOrPath); if (buildIndex >= 0) { From d75edbc8557d7689900128c71edbd8c0e7575933 Mon Sep 17 00:00:00 2001 From: Alon Farchy Date: Fri, 15 Nov 2024 16:39:37 -0600 Subject: [PATCH 02/12] Support addressable GUID --- .../SceneManagement/NetworkSceneManager.cs | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs index 059b594909..5f4bae518c 100644 --- a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs @@ -528,12 +528,12 @@ internal bool RemoveServerClientSceneHandle(int serverHandle, int clientHandle) /// /// Hash to external scene path lookup table /// - internal Dictionary HashToExternalScenePath = new Dictionary(); + internal Dictionary HashToAddressableName = new Dictionary(); /// /// External scene name to hash lookup table /// - internal Dictionary ExternalScenePathToHash = new Dictionary(); + internal Dictionary AddressableNameOrScenePathToHash = new Dictionary(); /// /// The Condition: While a scene is asynchronously loaded in single loading scene mode, if any new NetworkObjects are spawned @@ -720,16 +720,12 @@ internal void GenerateScenesInBuild() /// Register scene from outside of build (e.g. from an Addressables group). /// /// The paths of the external scenes to register. - public void RegisterExternalScenes(params string[] scenePaths) + public void RegisterAddressableScene(string addressablePath, string editorScenePath) { - HashToExternalScenePath.Clear(); - ExternalScenePathToHash.Clear(); - foreach (var scenePath in scenePaths) - { - var hash = XXHash.Hash32(scenePath); - HashToExternalScenePath.Add(hash, scenePath); - ExternalScenePathToHash.Add(scenePath, hash); - } + var hash = XXHash.Hash32(addressablePath); + HashToAddressableName.Add(hash, addressablePath); + AddressableNameOrScenePathToHash.Add(editorScenePath, hash); + AddressableNameOrScenePathToHash.Add(addressablePath, hash); } /// @@ -753,7 +749,7 @@ internal string SceneNameFromHash(uint sceneHash) /// internal string ScenePathFromHash(uint sceneHash) { - if (HashToExternalScenePath.TryGetValue(sceneHash, out var externalScenePath)) + if (HashToAddressableName.TryGetValue(sceneHash, out var externalScenePath)) { return externalScenePath; } @@ -773,7 +769,7 @@ internal string ScenePathFromHash(uint sceneHash) /// internal uint SceneHashFromNameOrPath(string sceneNameOrPath) { - if (ExternalScenePathToHash.TryGetValue(sceneNameOrPath, out var externalSceneHash)) + if (AddressableNameOrScenePathToHash.TryGetValue(sceneNameOrPath, out var externalSceneHash)) { return externalSceneHash; } @@ -2064,6 +2060,7 @@ internal void SynchronizeNetworkObjects(ulong clientId) { continue; } + sceneEventData.SceneHash = SceneHashFromNameOrPath(scene.path); // If we are just a normal client, then always use the server scene handle From 564ed9b595d7cf83a2348d41af7120b3888beb69 Mon Sep 17 00:00:00 2001 From: Alon Farchy Date: Fri, 15 Nov 2024 17:26:20 -0600 Subject: [PATCH 03/12] Add some logs --- .../SceneManagement/NetworkSceneManager.cs | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs index 5f4bae518c..c2852a284b 100644 --- a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs @@ -678,8 +678,8 @@ internal bool ShouldDeferCreateObject() /// internal string GetSceneNameFromPath(string scenePath) { - var begin = scenePath.LastIndexOf("/", StringComparison.Ordinal) + 1; - var end = scenePath.LastIndexOf(".", StringComparison.Ordinal); + var begin = scenePath.Contains("/") ? scenePath.LastIndexOf("/", StringComparison.Ordinal) + 1 : 0; + var end = scenePath.Contains(".") ? scenePath.LastIndexOf(".", StringComparison.Ordinal) : scenePath.Length; return scenePath.Substring(begin, end - begin); } @@ -722,6 +722,7 @@ internal void GenerateScenesInBuild() /// The paths of the external scenes to register. public void RegisterAddressableScene(string addressablePath, string editorScenePath) { + Debug.Log($"Registering addressable scene: {addressablePath} with editor scene path: {editorScenePath}"); var hash = XXHash.Hash32(addressablePath); HashToAddressableName.Add(hash, addressablePath); AddressableNameOrScenePathToHash.Add(editorScenePath, hash); @@ -741,7 +742,10 @@ internal string SceneNameFromHash(uint sceneHash) { return "No Scene"; } - return GetSceneNameFromPath(ScenePathFromHash(sceneHash)); + + var result = GetSceneNameFromPath(ScenePathFromHash(sceneHash)); + Debug.Log($"SceneNameFromHash: {sceneHash} = {result}"); + return result; } /// @@ -769,27 +773,34 @@ internal string ScenePathFromHash(uint sceneHash) /// internal uint SceneHashFromNameOrPath(string sceneNameOrPath) { + uint result = 0; + if (AddressableNameOrScenePathToHash.TryGetValue(sceneNameOrPath, out var externalSceneHash)) { - return externalSceneHash; + result = externalSceneHash; } - - var buildIndex = SceneUtility.GetBuildIndexByScenePath(sceneNameOrPath); - if (buildIndex >= 0) + else { - if (BuildIndexToHash.ContainsKey(buildIndex)) + var buildIndex = SceneUtility.GetBuildIndexByScenePath(sceneNameOrPath); + if (buildIndex >= 0) { - return BuildIndexToHash[buildIndex]; + if (BuildIndexToHash.ContainsKey(buildIndex)) + { + result = BuildIndexToHash[buildIndex]; + } + else + { + throw new Exception($"Scene '{sceneNameOrPath}' has a build index of {buildIndex} that does not exist in the {nameof(BuildIndexToHash)} table!"); + } } else { - throw new Exception($"Scene '{sceneNameOrPath}' has a build index of {buildIndex} that does not exist in the {nameof(BuildIndexToHash)} table!"); + throw new Exception($"Scene '{sceneNameOrPath}' couldn't be loaded because it has not been added to the build settings scenes in build list."); } } - else - { - throw new Exception($"Scene '{sceneNameOrPath}' couldn't be loaded because it has not been added to the build settings scenes in build list."); - } + + Debug.Log($"SceneHashFromNameOrPath: {sceneNameOrPath} = {result}"); + return result; } /// From c1e37d2555aa7b179523497e82d77a35cb4f702c Mon Sep 17 00:00:00 2001 From: Alon Farchy Date: Fri, 15 Nov 2024 17:53:56 -0600 Subject: [PATCH 04/12] Fix exception calling add twice --- .../Runtime/SceneManagement/NetworkSceneManager.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs index c2852a284b..8d101252b4 100644 --- a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs @@ -719,14 +719,17 @@ internal void GenerateScenesInBuild() /// /// Register scene from outside of build (e.g. from an Addressables group). /// - /// The paths of the external scenes to register. + /// The addressable path to the scene + /// The asset path to the scene public void RegisterAddressableScene(string addressablePath, string editorScenePath) { Debug.Log($"Registering addressable scene: {addressablePath} with editor scene path: {editorScenePath}"); var hash = XXHash.Hash32(addressablePath); - HashToAddressableName.Add(hash, addressablePath); - AddressableNameOrScenePathToHash.Add(editorScenePath, hash); - AddressableNameOrScenePathToHash.Add(addressablePath, hash); + HashToAddressableName[hash] = addressablePath; + + // We add both here because scene.path can be different in editor and in build, depending on addressable naming settings. + AddressableNameOrScenePathToHash[editorScenePath] = hash; + AddressableNameOrScenePathToHash[addressablePath] = hash; } /// From f17b957bceeade38b9585a16471abd45ae57f290 Mon Sep 17 00:00:00 2001 From: Alon Farchy Date: Fri, 15 Nov 2024 18:42:57 -0600 Subject: [PATCH 05/12] More fixs --- .../SceneManagement/NetworkSceneManager.cs | 64 +++++++++---------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs index 8d101252b4..b28b293cf1 100644 --- a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs @@ -528,12 +528,12 @@ internal bool RemoveServerClientSceneHandle(int serverHandle, int clientHandle) /// /// Hash to external scene path lookup table /// - internal Dictionary HashToAddressableName = new Dictionary(); + internal Dictionary HashToAddressableKey = new Dictionary(); /// /// External scene name to hash lookup table /// - internal Dictionary AddressableNameOrScenePathToHash = new Dictionary(); + internal Dictionary AddressableKeyToHash = new Dictionary(); /// /// The Condition: While a scene is asynchronously loaded in single loading scene mode, if any new NetworkObjects are spawned @@ -717,19 +717,15 @@ internal void GenerateScenesInBuild() } /// - /// Register scene from outside of build (e.g. from an Addressables group). + /// Register scene that is preloaded from Addressable /// - /// The addressable path to the scene - /// The asset path to the scene - public void RegisterAddressableScene(string addressablePath, string editorScenePath) + /// The addressable runtime key of the scene + public void RegisterAddressableScene(string addressableKey) { - Debug.Log($"Registering addressable scene: {addressablePath} with editor scene path: {editorScenePath}"); - var hash = XXHash.Hash32(addressablePath); - HashToAddressableName[hash] = addressablePath; - - // We add both here because scene.path can be different in editor and in build, depending on addressable naming settings. - AddressableNameOrScenePathToHash[editorScenePath] = hash; - AddressableNameOrScenePathToHash[addressablePath] = hash; + Debug.Log($"Registering addressable scene: {addressableKey}"); + var hash = XXHash.Hash32(addressableKey); + HashToAddressableKey[hash] = addressableKey; + AddressableKeyToHash[addressableKey] = hash; } /// @@ -756,8 +752,12 @@ internal string SceneNameFromHash(uint sceneHash) /// internal string ScenePathFromHash(uint sceneHash) { - if (HashToAddressableName.TryGetValue(sceneHash, out var externalScenePath)) + if (HashToAddressableKey.TryGetValue(sceneHash, out var externalScenePath)) { +#if UNITY_EDITOR + // NOTE: Only works with addressables fast play mode script + externalScenePath = UnityEditor.AssetDatabase.GUIDToAssetPath(externalScenePath); +#endif return externalScenePath; } else if (HashToBuildIndex.ContainsKey(sceneHash)) @@ -776,34 +776,32 @@ internal string ScenePathFromHash(uint sceneHash) /// internal uint SceneHashFromNameOrPath(string sceneNameOrPath) { - uint result = 0; - - if (AddressableNameOrScenePathToHash.TryGetValue(sceneNameOrPath, out var externalSceneHash)) +#if UNITY_EDITOR + // NOTE: Only works with addressables fast play mode script + sceneNameOrPath = UnityEditor.AssetDatabase.AssetPathToGUID(sceneNameOrPath); +#endif + if (AddressableKeyToHash.TryGetValue(sceneNameOrPath, out var externalSceneHash)) { - result = externalSceneHash; + Debug.Log($"SceneHashFromNameOrPath: {sceneNameOrPath} = {externalSceneHash}"); + return externalSceneHash; } - else + + var buildIndex = SceneUtility.GetBuildIndexByScenePath(sceneNameOrPath); + if (buildIndex >= 0) { - var buildIndex = SceneUtility.GetBuildIndexByScenePath(sceneNameOrPath); - if (buildIndex >= 0) + if (BuildIndexToHash.ContainsKey(buildIndex)) { - if (BuildIndexToHash.ContainsKey(buildIndex)) - { - result = BuildIndexToHash[buildIndex]; - } - else - { - throw new Exception($"Scene '{sceneNameOrPath}' has a build index of {buildIndex} that does not exist in the {nameof(BuildIndexToHash)} table!"); - } + return BuildIndexToHash[buildIndex]; } else { - throw new Exception($"Scene '{sceneNameOrPath}' couldn't be loaded because it has not been added to the build settings scenes in build list."); + throw new Exception($"Scene '{sceneNameOrPath}' has a build index of {buildIndex} that does not exist in the {nameof(BuildIndexToHash)} table!"); } } - - Debug.Log($"SceneHashFromNameOrPath: {sceneNameOrPath} = {result}"); - return result; + else + { + throw new Exception($"Scene '{sceneNameOrPath}' couldn't be loaded because it has not been added to the build settings scenes in build list."); + } } /// From b2edeb7ff501c27775348da2bf84cccb1e879e30 Mon Sep 17 00:00:00 2001 From: Alon Farchy Date: Wed, 20 Nov 2024 11:54:34 -0600 Subject: [PATCH 06/12] Fix active scene getting reported incorrectly --- .../SceneManagement/NetworkSceneManager.cs | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs index b28b293cf1..19e1fd5e3b 100644 --- a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs @@ -776,13 +776,15 @@ internal string ScenePathFromHash(uint sceneHash) /// internal uint SceneHashFromNameOrPath(string sceneNameOrPath) { + var addressableKey = sceneNameOrPath; + #if UNITY_EDITOR // NOTE: Only works with addressables fast play mode script - sceneNameOrPath = UnityEditor.AssetDatabase.AssetPathToGUID(sceneNameOrPath); + addressableKey = UnityEditor.AssetDatabase.AssetPathToGUID(sceneNameOrPath); #endif - if (AddressableKeyToHash.TryGetValue(sceneNameOrPath, out var externalSceneHash)) + if (AddressableKeyToHash.TryGetValue(addressableKey, out var externalSceneHash)) { - Debug.Log($"SceneHashFromNameOrPath: {sceneNameOrPath} = {externalSceneHash}"); + Debug.Log($"SceneHashFromNameOrPath: {addressableKey} = {externalSceneHash}"); return externalSceneHash; } @@ -2042,10 +2044,7 @@ internal void SynchronizeNetworkObjects(ulong clientId) sceneEventData.LoadSceneMode = ClientSynchronizationMode; var activeScene = SceneManager.GetActiveScene(); sceneEventData.SceneEventType = SceneEventType.Synchronize; - if (BuildIndexToHash.ContainsKey(activeScene.buildIndex)) - { - sceneEventData.ActiveSceneHash = BuildIndexToHash[activeScene.buildIndex]; - } + sceneEventData.ActiveSceneHash = SceneHashFromNameOrPath(activeScene.path); // Organize how (and when) we serialize our NetworkObjects for (int i = 0; i < SceneManager.sceneCount; i++) @@ -2382,13 +2381,10 @@ private void HandleClientSceneEvent(uint sceneEventId) PopulateScenePlacedObjects(DontDestroyOnLoadScene, false); // If needed, set the currently active scene - if (HashToBuildIndex.ContainsKey(sceneEventData.ActiveSceneHash)) + var targetActiveScene = SceneManager.GetSceneByName(SceneNameFromHash(sceneEventData.ActiveSceneHash)); + if (targetActiveScene.isLoaded && targetActiveScene.handle != SceneManager.GetActiveScene().handle) { - var targetActiveScene = SceneManager.GetSceneByBuildIndex(HashToBuildIndex[sceneEventData.ActiveSceneHash]); - if (targetActiveScene.isLoaded && targetActiveScene.handle != SceneManager.GetActiveScene().handle) - { - SceneManager.SetActiveScene(targetActiveScene); - } + SceneManager.SetActiveScene(targetActiveScene); } // Spawn and Synchronize all NetworkObjects From 61ce89b94a6e88d27aa1423b321f784e97d5f368 Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Mon, 2 Dec 2024 11:49:56 -0500 Subject: [PATCH 07/12] update package author --- com.unity.netcode.gameobjects/package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/com.unity.netcode.gameobjects/package.json b/com.unity.netcode.gameobjects/package.json index a632de0172..5d64accf28 100644 --- a/com.unity.netcode.gameobjects/package.json +++ b/com.unity.netcode.gameobjects/package.json @@ -4,6 +4,9 @@ "description": "Netcode for GameObjects is a high-level netcode SDK that provides networking capabilities to GameObject/MonoBehaviour workflows within Unity and sits on top of underlying transport layer.", "version": "2.1.1", "unity": "6000.0", + "author": { + "name": "Unity" + }, "dependencies": { "com.unity.nuget.mono-cecil": "1.11.4", "com.unity.transport": "2.3.0" From ae5cdf6922d966abf32464cc735bacc4f15b4081 Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Fri, 13 Dec 2024 18:39:07 -0500 Subject: [PATCH 08/12] cleanup network manager changes - removed debug - reverted misc changes from upstream --- .../SceneManagement/NetworkSceneManager.cs | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs index 4e9ba8741f..619abef6d6 100644 --- a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs @@ -742,9 +742,7 @@ internal string SceneNameFromHash(uint sceneHash) return "No Scene"; } - var result = GetSceneNameFromPath(ScenePathFromHash(sceneHash)); - Debug.Log($"SceneNameFromHash: {sceneHash} = {result}"); - return result; + return GetSceneNameFromPath(ScenePathFromHash(sceneHash)); } /// @@ -755,8 +753,10 @@ internal string ScenePathFromHash(uint sceneHash) if (HashToAddressableKey.TryGetValue(sceneHash, out var externalScenePath)) { #if UNITY_EDITOR - // NOTE: Only works with addressables fast play mode script - externalScenePath = UnityEditor.AssetDatabase.GUIDToAssetPath(externalScenePath); + if (Guid.TryParse(externalScenePath, out var guid)) + { + externalScenePath = UnityEditor.AssetDatabase.GUIDToAssetPath(guid.ToString("N")); + } #endif return externalScenePath; } @@ -774,21 +774,20 @@ internal string ScenePathFromHash(uint sceneHash) /// /// Gets the associated hash value for the scene name or path /// - internal uint SceneHashFromNameOrPath(string sceneNameOrPath) + internal uint SceneHashFromNameOrPath(string resourceLocation) { - var addressableKey = sceneNameOrPath; - #if UNITY_EDITOR - // NOTE: Only works with addressables fast play mode script - addressableKey = UnityEditor.AssetDatabase.AssetPathToGUID(sceneNameOrPath); + if (Guid.TryParse(resourceLocation, out var guid)) + { + resourceLocation = UnityEditor.AssetDatabase.AssetPathToGUID(guid.ToString("N")); + } #endif - if (AddressableKeyToHash.TryGetValue(addressableKey, out var externalSceneHash)) + if (AddressableKeyToHash.TryGetValue(resourceLocation, out var externalSceneHash)) { - Debug.Log($"SceneHashFromNameOrPath: {addressableKey} = {externalSceneHash}"); return externalSceneHash; } - var buildIndex = SceneUtility.GetBuildIndexByScenePath(sceneNameOrPath); + var buildIndex = SceneUtility.GetBuildIndexByScenePath(resourceLocation); if (buildIndex >= 0) { if (BuildIndexToHash.ContainsKey(buildIndex)) @@ -797,12 +796,12 @@ internal uint SceneHashFromNameOrPath(string sceneNameOrPath) } else { - throw new Exception($"Scene '{sceneNameOrPath}' has a build index of {buildIndex} that does not exist in the {nameof(BuildIndexToHash)} table!"); + throw new Exception($"Scene '{resourceLocation}' has a build index of {buildIndex} that does not exist in the {nameof(BuildIndexToHash)} table!"); } } else { - throw new Exception($"Scene '{sceneNameOrPath}' couldn't be loaded because it has not been added to the build settings scenes in build list."); + throw new Exception($"Scene '{resourceLocation}' couldn't be loaded because it has not been added to the build settings scenes in build list."); } } @@ -2078,7 +2077,6 @@ internal void SynchronizeNetworkObjects(ulong clientId) { continue; } - sceneEventData.SceneHash = SceneHashFromNameOrPath(scene.path); // If we are just a normal client, then always use the server scene handle From 919d12fff0160157f078593464c798e850dd2146 Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Fri, 13 Dec 2024 18:51:40 -0500 Subject: [PATCH 09/12] revert and tweak --- .../SceneManagement/NetworkSceneManager.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs index 619abef6d6..a215719453 100644 --- a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs @@ -774,20 +774,22 @@ internal string ScenePathFromHash(uint sceneHash) /// /// Gets the associated hash value for the scene name or path /// - internal uint SceneHashFromNameOrPath(string resourceLocation) + internal uint SceneHashFromNameOrPath(string sceneNameOrPath) { #if UNITY_EDITOR - if (Guid.TryParse(resourceLocation, out var guid)) + var addressableKey = UnityEditor.AssetDatabase.AssetPathToGUID(sceneNameOrPath); + + if (AddressableKeyToHash.TryGetValue(addressableKey, out var externalSceneHash)) { - resourceLocation = UnityEditor.AssetDatabase.AssetPathToGUID(guid.ToString("N")); + return externalSceneHash; } #endif - if (AddressableKeyToHash.TryGetValue(resourceLocation, out var externalSceneHash)) + if (AddressableKeyToHash.TryGetValue(sceneNameOrPath, out externalSceneHash)) { return externalSceneHash; } - var buildIndex = SceneUtility.GetBuildIndexByScenePath(resourceLocation); + var buildIndex = SceneUtility.GetBuildIndexByScenePath(sceneNameOrPath); if (buildIndex >= 0) { if (BuildIndexToHash.ContainsKey(buildIndex)) @@ -796,12 +798,12 @@ internal uint SceneHashFromNameOrPath(string resourceLocation) } else { - throw new Exception($"Scene '{resourceLocation}' has a build index of {buildIndex} that does not exist in the {nameof(BuildIndexToHash)} table!"); + throw new Exception($"Scene '{sceneNameOrPath}' has a build index of {buildIndex} that does not exist in the {nameof(BuildIndexToHash)} table!"); } } else { - throw new Exception($"Scene '{resourceLocation}' couldn't be loaded because it has not been added to the build settings scenes in build list."); + throw new Exception($"Scene '{sceneNameOrPath}' couldn't be loaded because it has not been added to the build settings scenes in build list."); } } From 3a9e8d4825aa90e91a3f7c9a75854b70c43e3d28 Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Fri, 13 Dec 2024 18:53:00 -0500 Subject: [PATCH 10/12] revert --- .../Runtime/SceneManagement/NetworkSceneManager.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs index a215719453..57be8ed52d 100644 --- a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs @@ -741,7 +741,6 @@ internal string SceneNameFromHash(uint sceneHash) { return "No Scene"; } - return GetSceneNameFromPath(ScenePathFromHash(sceneHash)); } From fc39086d394c1f95903b5d1313c5947c5f6edec4 Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Fri, 13 Dec 2024 18:54:08 -0500 Subject: [PATCH 11/12] remove debug --- .../Runtime/SceneManagement/NetworkSceneManager.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs index 57be8ed52d..4e3fbec9c9 100644 --- a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs @@ -722,7 +722,6 @@ internal void GenerateScenesInBuild() /// The addressable runtime key of the scene public void RegisterAddressableScene(string addressableKey) { - Debug.Log($"Registering addressable scene: {addressableKey}"); var hash = XXHash.Hash32(addressableKey); HashToAddressableKey[hash] = addressableKey; AddressableKeyToHash[addressableKey] = hash; From ee19e52bed6813f1fbefecfbd2a0337249426fb6 Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Fri, 13 Dec 2024 19:31:50 -0500 Subject: [PATCH 12/12] fix name --- .../Runtime/SceneManagement/NetworkSceneManager.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs index 4e3fbec9c9..596c5311aa 100644 --- a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs @@ -777,12 +777,12 @@ internal uint SceneHashFromNameOrPath(string sceneNameOrPath) #if UNITY_EDITOR var addressableKey = UnityEditor.AssetDatabase.AssetPathToGUID(sceneNameOrPath); - if (AddressableKeyToHash.TryGetValue(addressableKey, out var externalSceneHash)) + if (AddressableKeyToHash.TryGetValue(addressableKey, out var addressableHash)) { - return externalSceneHash; + return addressableHash; } #endif - if (AddressableKeyToHash.TryGetValue(sceneNameOrPath, out externalSceneHash)) + if (AddressableKeyToHash.TryGetValue(sceneNameOrPath, out var externalSceneHash)) { return externalSceneHash; }