Skip to content

fix: networkmanager destroy on app quit #1011

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

Merged
merged 12 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using UnityEngine;
using Unity.Profiling;
using UnityEngine.SceneManagement;
using Debug = UnityEngine.Debug;

namespace Unity.Netcode
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 0 additions & 12 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,20 +392,8 @@ public static void NetworkHide(List<NetworkObject> 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.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.IO;
using System.IO;

namespace Unity.Netcode
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if MULTIPLAYER_TOOLS
#if MULTIPLAYER_TOOLS
using System;
using System.Collections;
using System.Linq;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if MULTIPLAYER_TOOLS
#if MULTIPLAYER_TOOLS
using System;
using System.Collections;
using UnityEngine;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if MULTIPLAYER_TOOLS
#if MULTIPLAYER_TOOLS
using System;

namespace Unity.Netcode.RuntimeTests.Metrics.Utlity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

namespace Unity.Netcode.RuntimeTests.Metrics.Utlity
{
Expand Down
2 changes: 1 addition & 1 deletion testproject/Assets/Tests/Runtime/MessageOrdering.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections;
using System.Collections;
using Unity.Netcode;
using Unity.Netcode.RuntimeTests;
using NUnit.Framework;
Expand Down