From e4890a7462c7627a985d4c47c4fd3016449cd44d Mon Sep 17 00:00:00 2001 From: Luke Stampfli Date: Mon, 2 Aug 2021 17:38:55 +0100 Subject: [PATCH 1/5] fix: Ensure that networkmanager is shutdown before GameObjects are being destroyed --- .../Runtime/Core/NetworkManager.cs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs index 77d79c352a..2cb6ec2a2d 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs @@ -19,6 +19,7 @@ using MLAPI.Transports.Tasks; using MLAPI.Timing; using Unity.Profiling; +using UnityEngine.SceneManagement; using Debug = UnityEngine.Debug; namespace MLAPI @@ -854,6 +855,28 @@ private void OnEnable() } } + private void Awake() + { + Application.wantsToQuit += ApplicationWantsToQuit; + 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 bool ApplicationWantsToQuit() + { + OnDestroy(); + return true; + } + private void OnDestroy() { Shutdown(); From dee6feb4ec595dc72a5ee12290fa39ab0ec16352 Mon Sep 17 00:00:00 2001 From: Luke Stampfli Date: Wed, 4 Aug 2021 16:37:39 +0100 Subject: [PATCH 2/5] fix: use OnApplicationQuit instead of ApplicationWantsToQuit --- com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs index 1bfccebf8b..f3ba750e04 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs @@ -855,7 +855,6 @@ private void OnEnable() private void Awake() { - Application.wantsToQuit += ApplicationWantsToQuit; UnityEngine.SceneManagement.SceneManager.sceneUnloaded += OnSceneUnloaded; } @@ -869,10 +868,9 @@ private void OnSceneUnloaded(Scene scene) } // Ensures that the NetworkManager is cleaned up before OnDestroy is run on NetworkObjects and NetworkBehaviours when quitting the application. - private bool ApplicationWantsToQuit() + private void OnApplicationQuit() { OnDestroy(); - return true; } private void OnDestroy() From 0faa4e5b58a6395d34ddb9ae02c468e0cc106f35 Mon Sep 17 00:00:00 2001 From: "M. Fatih MAR" Date: Fri, 13 Aug 2021 12:27:49 +0100 Subject: [PATCH 3/5] `./standards.py --fix` --- com.unity.netcode.gameobjects/Runtime/Logging/NetworkLog.cs | 2 +- .../Runtime/Messaging/CustomMessageManager.cs | 2 +- .../Runtime/Metrics/StreamExtensions.cs | 2 +- .../Tests/Runtime/Metrics/OwnershipChangeMetricsTests.cs | 2 +- .../Tests/Runtime/Metrics/Utility/MetricTestBase.cs | 2 +- .../Tests/Runtime/Metrics/Utility/NetworkVariableComponent.cs | 2 +- .../Tests/Runtime/Metrics/Utility/RpcTestComponent.cs | 2 +- testproject/Assets/Tests/Runtime/MessageOrdering.cs | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) 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/Messaging/CustomMessageManager.cs b/com.unity.netcode.gameobjects/Runtime/Messaging/CustomMessageManager.cs index 79b737b9b5..d8f76a35ac 100644 --- a/com.unity.netcode.gameobjects/Runtime/Messaging/CustomMessageManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/Messaging/CustomMessageManager.cs @@ -206,7 +206,7 @@ public void SendNamedMessage(string name, ulong clientId, Stream stream, Network stream.Position = 0; stream.CopyTo(nonNullContext.NetworkWriter.GetStream()); - + var size = bufferSizeCapture.StopMeasureSegment(); m_NetworkManager.NetworkMetrics.TrackNamedMessageSent(clientId, name, size); 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; From f6d933778e88c8e1b823cae4e810a8c5b8874b41 Mon Sep 17 00:00:00 2001 From: "M. Fatih MAR" Date: Thu, 19 Aug 2021 16:32:34 +0100 Subject: [PATCH 4/5] undo unnecessary fix --- .../Runtime/Core/NetworkObject.cs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs index 3dc87e55bb..e22d2c4063 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs @@ -380,20 +380,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."); From 7e7d9e1ac7441504f7aa36ed9b93ba7243547e6f Mon Sep 17 00:00:00 2001 From: Luke Stampfli Date: Thu, 19 Aug 2021 17:32:05 +0100 Subject: [PATCH 5/5] fix a bug where OnDestroy was called twice when destroying the NetworkManager and then unloading the scene. --- com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs index 166ed7c7bc..260f42cdb7 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs @@ -860,10 +860,13 @@ 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;