diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs index 9e39b3cc7e..298de3a51c 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs @@ -4,6 +4,7 @@ using System.Linq; using UnityEngine; using Unity.Profiling; +using UnityEngine.SceneManagement; using Debug = UnityEngine.Debug; namespace Unity.Netcode @@ -839,10 +840,33 @@ private void OnEnable() } } + private void Awake() + { + UnityEngine.SceneManagement.SceneManager.sceneUnloaded += OnSceneUnloaded; + } + + // Ensures that the NetworkManager is cleaned up before OnDestroy is run on NetworkObjects and NetworkBehaviours when unloading a scene with a NetworkManager + private void OnSceneUnloaded(Scene scene) + { + if (scene == gameObject.scene) + { + OnDestroy(); + } + } + + // Ensures that the NetworkManager is cleaned up before OnDestroy is run on NetworkObjects and NetworkBehaviours when quitting the application. + private void OnApplicationQuit() + { + OnDestroy(); + } + + // Note that this gets also called manually by OnSceneUnloaded and OnApplicationQuit private void OnDestroy() { Shutdown(); + UnityEngine.SceneManagement.SceneManager.sceneUnloaded -= OnSceneUnloaded; + if (Singleton == this) { Singleton = null; diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs index c30fe6bcd6..05857a81ce 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs @@ -392,20 +392,8 @@ public static void NetworkHide(List networkObjects, ulong clientI } } - private bool m_ApplicationQuitting = false; - - private void OnApplicationQuit() - { - m_ApplicationQuitting = true; - } - private void OnDestroy() { - if (m_ApplicationQuitting) - { - return; - } - if (NetworkManager != null && NetworkManager.IsListening && NetworkManager.IsServer == false && IsSpawned) { throw new NotServerException($"Destroy a spawned {nameof(NetworkObject)} on a non-host client is not valid. Call {nameof(Destroy)} or {nameof(Despawn)} on the server/host instead."); diff --git a/com.unity.netcode.gameobjects/Runtime/Logging/NetworkLog.cs b/com.unity.netcode.gameobjects/Runtime/Logging/NetworkLog.cs index e969eb5da2..3d4cb9b4cd 100644 --- a/com.unity.netcode.gameobjects/Runtime/Logging/NetworkLog.cs +++ b/com.unity.netcode.gameobjects/Runtime/Logging/NetworkLog.cs @@ -65,7 +65,7 @@ private static void LogServer(string message, LogType logType) { var bufferSizeCapture = new CommandContextSizeCapture(nonNullContext); bufferSizeCapture.StartMeasureSegment(); - nonNullContext.NetworkWriter.WriteByte((byte) logType); + nonNullContext.NetworkWriter.WriteByte((byte)logType); nonNullContext.NetworkWriter.WriteStringPacked(message); var size = bufferSizeCapture.StopMeasureSegment(); diff --git a/com.unity.netcode.gameobjects/Runtime/Metrics/StreamExtensions.cs b/com.unity.netcode.gameobjects/Runtime/Metrics/StreamExtensions.cs index 95474529e4..615efb5994 100644 --- a/com.unity.netcode.gameobjects/Runtime/Metrics/StreamExtensions.cs +++ b/com.unity.netcode.gameobjects/Runtime/Metrics/StreamExtensions.cs @@ -1,4 +1,4 @@ -using System.IO; +using System.IO; namespace Unity.Netcode { diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/Metrics/OwnershipChangeMetricsTests.cs b/com.unity.netcode.gameobjects/Tests/Runtime/Metrics/OwnershipChangeMetricsTests.cs index b14abdfd6c..9cfb2dfbde 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/Metrics/OwnershipChangeMetricsTests.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/Metrics/OwnershipChangeMetricsTests.cs @@ -1,4 +1,4 @@ -#if MULTIPLAYER_TOOLS +#if MULTIPLAYER_TOOLS using System; using System.Collections; using System.Linq; diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/Metrics/Utility/MetricTestBase.cs b/com.unity.netcode.gameobjects/Tests/Runtime/Metrics/Utility/MetricTestBase.cs index a4f2c4ce45..c9531be61d 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/Metrics/Utility/MetricTestBase.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/Metrics/Utility/MetricTestBase.cs @@ -1,4 +1,4 @@ -#if MULTIPLAYER_TOOLS +#if MULTIPLAYER_TOOLS using System; using System.Collections; using UnityEngine; diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/Metrics/Utility/NetworkVariableComponent.cs b/com.unity.netcode.gameobjects/Tests/Runtime/Metrics/Utility/NetworkVariableComponent.cs index 461fdc6736..f4eb14db88 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/Metrics/Utility/NetworkVariableComponent.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/Metrics/Utility/NetworkVariableComponent.cs @@ -1,4 +1,4 @@ -#if MULTIPLAYER_TOOLS +#if MULTIPLAYER_TOOLS using System; namespace Unity.Netcode.RuntimeTests.Metrics.Utlity diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/Metrics/Utility/RpcTestComponent.cs b/com.unity.netcode.gameobjects/Tests/Runtime/Metrics/Utility/RpcTestComponent.cs index 7ba041039d..d9792e1570 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/Metrics/Utility/RpcTestComponent.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/Metrics/Utility/RpcTestComponent.cs @@ -1,4 +1,4 @@ -using System; +using System; namespace Unity.Netcode.RuntimeTests.Metrics.Utlity { diff --git a/testproject/Assets/Tests/Runtime/MessageOrdering.cs b/testproject/Assets/Tests/Runtime/MessageOrdering.cs index 36c20fc893..4b395434ba 100644 --- a/testproject/Assets/Tests/Runtime/MessageOrdering.cs +++ b/testproject/Assets/Tests/Runtime/MessageOrdering.cs @@ -1,4 +1,4 @@ -using System.Collections; +using System.Collections; using Unity.Netcode; using Unity.Netcode.RuntimeTests; using NUnit.Framework;