Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ internal struct NetworkSessionInfo
public bool UsedCMBService;
public string Transport;
public NetworkConfig NetworkConfig;
public bool HybridSession;
public int NgoSpawnCount;
public int HybridSpawnCount;
}

/// <summary>
Expand Down Expand Up @@ -123,7 +126,16 @@ internal override void SessionStopped(NetworkManager networkManager)
{
return;
}
#if UNIFIED_NETCODE
recentSession.HybridSession = networkManager.NetworkConfig.Prefabs.HasGhostPrefabs;
#endif
recentSession.UsedCMBService = networkManager.CMBServiceConnection;
var spawnManager = networkManager.SpawnManager;
if (spawnManager != null)
{
recentSession.HybridSpawnCount = spawnManager.HybridSpawnCount;
recentSession.NgoSpawnCount = spawnManager.SpawnCount - spawnManager.HybridSpawnCount;
}
recentSession.SessionStopped = true;
RecentSessions[lastIndex] = recentSession;
}
Expand Down Expand Up @@ -220,6 +232,9 @@ private NetworkManagerAnalytics GetNetworkManagerAnalytics(NetworkSessionInfo ne
NetworkTransport = networkSession.Transport,
EnableSceneManagement = networkSession.NetworkConfig.EnableSceneManagement,
TickRate = (int)networkSession.NetworkConfig.TickRate,
HybridSession = networkSession.HybridSession,
NgoSpawnCount = networkSession.NgoSpawnCount,
HybridSpawnCount = networkSession.HybridSpawnCount,
};
return networkManagerAnalytics;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ internal struct NetworkManagerAnalytics : IAnalytic.IData, IEquatable<NetworkMan
public string NetworkTransport;
public bool EnableSceneManagement;
public int TickRate;
public bool HybridSession;
public int NgoSpawnCount;
public int HybridSpawnCount;
public override string ToString()
{
var message = new StringBuilder();
Expand All @@ -28,6 +31,9 @@ public override string ToString()
message.AppendLine($"{nameof(NetworkTransport)}: {NetworkTransport}");
message.AppendLine($"{nameof(EnableSceneManagement)}: {EnableSceneManagement}");
message.AppendLine($"{nameof(TickRate)}: {TickRate}");
message.AppendLine($"{nameof(HybridSession)}: {HybridSession}");
message.AppendLine($"{nameof(NgoSpawnCount)}: {NgoSpawnCount}");
message.AppendLine($"{nameof(HybridSpawnCount)}: {HybridSpawnCount}");
return message.ToString();
}

Expand All @@ -40,6 +46,7 @@ public bool Equals(NetworkManagerAnalytics other)
return IsDistributedAuthority == other.IsDistributedAuthority && WasServer == other.WasServer && WasClient == other.WasClient
&& UsedCMBService == other.UsedCMBService && IsUsingMultiplayerSDK == other.IsUsingMultiplayerSDK
&& EnableSceneManagement == other.EnableSceneManagement && TickRate == other.TickRate
&& NgoSpawnCount == other.NgoSpawnCount && HybridSpawnCount == other.HybridSpawnCount && HybridSession == other.HybridSession
&& NetworkTransport.Equals(other.NetworkTransport);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
/// </summary>
public readonly HashSet<NetworkObject> SpawnedObjectsList = new HashSet<NetworkObject>();

#if UNITY_EDITOR
// Spawns this peer made or received during the session, reported by the editor analytics.
internal int SpawnCount;
internal int HybridSpawnCount;
#endif

/// <summary>
/// Use to get all NetworkObjects owned by a client
/// Ownership to Objects Table Format:
Expand Down Expand Up @@ -1313,11 +1319,17 @@
if (networkObject.HasGhost)
{
networkObject.NetworkObjectBridge.UpdateGhostOwner(NetworkManager, ownerClientId);
#if UNITY_EDITOR
HybridSpawnCount++;

Check warning on line 1323 in com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs#L1323

Added line #L1323 was not covered by tests
#endif
}
#endif

SpawnedObjects.Add(networkObject.NetworkObjectId, networkObject);
SpawnedObjectsList.Add(networkObject);
#if UNITY_EDITOR
SpawnCount++;
#endif
networkObject.ApplyNetworkParenting();

NetworkObject.CheckOrphanChildren();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#if UNITY_EDITOR
using System.Collections;
using NUnit.Framework;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine;
using UnityEngine.TestTools;

namespace Unity.Netcode.RuntimeTests
{
/// <summary>
/// Validates the spawn counts the editor analytics reads from <see cref="NetworkSpawnManager"/>:<br />
/// - Every peer counts each instance it spawns locally, whether it is the authority or received the spawn.<br />
/// - Hybrid instances are also counted on their own.<br />
/// </summary>
[TestFixture(HostOrServer.Host)]
#if UNIFIED_NETCODE
[TestFixture(HostOrServer.UnifiedHost)]
Comment thread
NoelStephensUnity marked this conversation as resolved.
#endif
internal class SpawnCountAnalyticsTests : NetcodeIntegrationTest
{
private const int k_DefaultInstances = 3;
private const int k_NgoInstances = 2;

protected override int NumberOfClients => 2;

// Hybrid during a unified test pass.
private GameObject m_DefaultPrefab;
private GameObject m_NgoPrefab;

public SpawnCountAnalyticsTests(HostOrServer hostOrServer) : base(hostOrServer)
{
}

#if UNIFIED_NETCODE
protected override bool UseUnifiedTests()
{
return true;
}
#endif

protected override void OnServerAndClientsCreated()
{
m_DefaultPrefab = CreateNetworkObjectPrefab("SpawnCountDefault");
#if UNIFIED_NETCODE
m_NgoPrefab = CreateNetworkObjectPrefab("SpawnCountNgo", false);
#else
m_NgoPrefab = CreateNetworkObjectPrefab("SpawnCountNgo");
#endif
base.OnServerAndClientsCreated();
}

[UnityTest]
public IEnumerator SpawnsAreCountedOnEveryPeer()
{
// Player objects are already spawned, so only the increase is checked.
var spawnCounts = new int[m_NetworkManagers.Length];
var hybridSpawnCounts = new int[m_NetworkManagers.Length];
for (int i = 0; i < m_NetworkManagers.Length; i++)
{
spawnCounts[i] = m_NetworkManagers[i].SpawnManager.SpawnCount;
hybridSpawnCounts[i] = m_NetworkManagers[i].SpawnManager.HybridSpawnCount;
}

var authority = GetAuthorityNetworkManager();
var instances = SpawnObjects(m_DefaultPrefab, authority, k_DefaultInstances);
instances.AddRange(SpawnObjects(m_NgoPrefab, authority, k_NgoInstances));
foreach (var instance in instances)
{
yield return WaitForSpawnedOnAllOrTimeOut(instance.GetComponent<NetworkObject>());
AssertOnTimeout($"Failed to spawn {instance.name} on all clients!");
}

var expectedHybridSpawns = 0;
#if UNIFIED_NETCODE
if (m_UseUnifiedTests)
{
expectedHybridSpawns = k_DefaultInstances;
}
#endif
for (int i = 0; i < m_NetworkManagers.Length; i++)
{
var spawnManager = m_NetworkManagers[i].SpawnManager;
var clientId = m_NetworkManagers[i].LocalClientId;
Assert.AreEqual(k_DefaultInstances + k_NgoInstances, spawnManager.SpawnCount - spawnCounts[i], $"Client-{clientId} counted the wrong number of spawns!");
Assert.AreEqual(expectedHybridSpawns, spawnManager.HybridSpawnCount - hybridSpawnCounts[i], $"Client-{clientId} counted the wrong number of hybrid spawns!");
}
}
}
}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading