Skip to content

Commit d509fca

Browse files
Merge branch 'develop' into fix/console-tests-failing
2 parents 8244627 + 58f194d commit d509fca

File tree

4 files changed

+79
-3
lines changed

4 files changed

+79
-3
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
2222
- Fixed error when serializing ConnectionApprovalMessage with scene management disabled when one or more objects is hidden via the CheckObjectVisibility delegate (#1509)
2323
- Fixed The NetworkConfig's checksum hash includes the NetworkTick so that clients with a different tickrate than the server are identified and not allowed to connect. (#1513)
2424
- Fixed OwnedObjects not being properly modified when using ChangeOwnership. (#1572)
25+
- Fixed When the LogLevel is set to developer NetworkBehaviour generates warning messages when it should not (#1631)
2526

2627
### Changed
2728

com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,14 @@ public NetworkObject NetworkObject
304304
m_NetworkObject = GetComponentInParent<NetworkObject>();
305305
}
306306

307-
if (m_NetworkObject == null || NetworkManager.Singleton == null ||
308-
(NetworkManager.Singleton != null && !NetworkManager.Singleton.ShutdownInProgress))
307+
// ShutdownInProgress check:
308+
// This prevents an edge case scenario where the NetworkManager is shutting down but user code
309+
// in Update and/or in FixedUpdate could still be checking NetworkBehaviour.NetworkObject directly (i.e. does it exist?)
310+
// or NetworkBehaviour.IsSpawned (i.e. to early exit if not spawned) which, in turn, could generate several Warning messages
311+
// per spawned NetworkObject. Checking for ShutdownInProgress prevents these unnecessary LogWarning messages.
312+
if (m_NetworkObject == null && (NetworkManager.Singleton == null || !NetworkManager.Singleton.ShutdownInProgress))
309313
{
310-
if (NetworkLog.CurrentLogLevel < LogLevel.Normal)
314+
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
311315
{
312316
NetworkLog.LogWarning($"Could not get {nameof(NetworkObject)} for the {nameof(NetworkBehaviour)}. Are you missing a {nameof(NetworkObject)} component?");
313317
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System.Collections;
2+
using NUnit.Framework;
3+
using UnityEngine;
4+
using UnityEngine.TestTools;
5+
6+
namespace Unity.Netcode.RuntimeTests
7+
{
8+
/// <summary>
9+
/// This class is for testing general fixes or functionality of NetworkBehaviours
10+
/// </summary>
11+
public class NetworkBehaviourGenericTests : BaseMultiInstanceTest
12+
{
13+
protected override int NbClients => 0;
14+
public override IEnumerator Setup()
15+
{
16+
// Make sure we don't automatically start
17+
m_BypassStartAndWaitForClients = true;
18+
19+
// Create the Host and add the SimpleNetworkBehaviour component
20+
yield return base.Setup();
21+
}
22+
23+
public class SimpleNetworkBehaviour : NetworkBehaviour
24+
{
25+
}
26+
27+
/// <summary>
28+
/// This test validates a fix to NetworkBehaviour.NetworkObject when
29+
/// the NetworkManager.LogLevel is set to Developer
30+
/// Note: This test does not require any clients, but should not impact this
31+
/// particular test if new tests are added to this class that do require clients
32+
/// </summary>
33+
[Test]
34+
public void ValidateNoSpam()
35+
{
36+
var objectToTest = new GameObject();
37+
var simpleNetworkBehaviour = objectToTest.AddComponent<SimpleNetworkBehaviour>();
38+
39+
// Now just start the Host
40+
Assert.True(MultiInstanceHelpers.Start(true, m_ServerNetworkManager, new NetworkManager[] { }), "Failed to start the host!");
41+
42+
// set the log level to developer
43+
m_ServerNetworkManager.LogLevel = LogLevel.Developer;
44+
45+
// Verify the warning gets logged under normal conditions
46+
var isNull = simpleNetworkBehaviour.NetworkObject == null;
47+
LogAssert.Expect(LogType.Warning, $"[Netcode] Could not get {nameof(NetworkObject)} for the {nameof(NetworkBehaviour)}. Are you missing a {nameof(NetworkObject)} component?");
48+
49+
var networkObjectToTest = objectToTest.AddComponent<NetworkObject>();
50+
networkObjectToTest.Spawn();
51+
52+
// Assure no log messages are logged when they should not be logged
53+
isNull = simpleNetworkBehaviour.NetworkObject != null;
54+
LogAssert.NoUnexpectedReceived();
55+
56+
networkObjectToTest.Despawn();
57+
Object.Destroy(networkObjectToTest);
58+
}
59+
}
60+
}

com.unity.netcode.gameobjects/Tests/Runtime/NetworkBehaviourGenericTests.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)