Skip to content

Commit a17194f

Browse files
update
Adding analytics information gathering scripts to handle tracking the network configurations for each session while in play mode. Upon exiting play mode, analytics data can be submitted. The actual submission is yet to be implemented.
1 parent 9804c9b commit a17194f

File tree

3 files changed

+197
-2
lines changed

3 files changed

+197
-2
lines changed

com.unity.netcode.gameobjects/Editor/NetworkManagerHelper.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Unity.Netcode.Editor.Configuration;
44
using UnityEditor;
55
using UnityEngine;
6+
using UnityEngine.Analytics;
67
using UnityEngine.SceneManagement;
78

89
namespace Unity.Netcode.Editor
@@ -224,6 +225,73 @@ public bool NotifyUserOfNestedNetworkManager(NetworkManager networkManager, bool
224225
}
225226
return isParented;
226227
}
228+
229+
private const bool k_EnableAnalyticsLogging = true;
230+
231+
public void UpdateAnalytics()
232+
{
233+
if (!EditorAnalytics.enabled)
234+
{
235+
return;
236+
}
237+
for (int i = 0; i < NetworkManager.RecentSessions.Count; i++)
238+
{
239+
var networkManagerAnalytics = GetNetworkManagerAnalytics(NetworkManager.RecentSessions[i]);
240+
if (k_EnableAnalyticsLogging)
241+
{
242+
networkManagerAnalytics.LogAnalytics(NetworkManager.RecentSessions[i].SessionIndex);
243+
}
244+
}
245+
}
246+
247+
248+
private NetworkManagerAnalytics GetNetworkManagerAnalytics(NetworkManager.NetworkSessionInfo networkSession)
249+
{
250+
var multiplayerSDKInstalled = false;
251+
var multiplayerToolsInstalled = false;
252+
#if MULTIPLAYER_SERVICES_SDK_INSTALLED
253+
multiplayerSDKInstalled = true;
254+
#endif
255+
#if MULTIPLAYER_TOOLS
256+
multiplayerToolsInstalled = true;
257+
#endif
258+
if (!networkSession.SessionStopped)
259+
{
260+
Debug.LogWarning($"Session-{networkSession.SessionIndex} was not considered stopped!");
261+
}
262+
var networkManagerAnalytics = new NetworkManagerAnalytics()
263+
{
264+
NetworkTopology = networkSession.NetworkConfig.NetworkTopology.ToString(),
265+
UsedCMBService = networkSession.UsedCMBService,
266+
NetworkTransport = networkSession.Transport,
267+
IsUsingMultiplayerSDK = multiplayerSDKInstalled,
268+
IsUsingMultiplayerTools = multiplayerToolsInstalled,
269+
PlayerPrefabSet = networkSession.PlayerPrefab,
270+
ConnectionApproval = networkSession.NetworkConfig.ConnectionApproval,
271+
ClientConnectionBufferTimeout = networkSession.NetworkConfig.ClientConnectionBufferTimeout,
272+
EnsureNetworkVariableLengthSafety = networkSession.NetworkConfig.EnsureNetworkVariableLengthSafety,
273+
EnableSceneManagement = networkSession.NetworkConfig.EnableSceneManagement,
274+
LoadSceneTimeOut = networkSession.NetworkConfig.LoadSceneTimeOut,
275+
SpawnTimeout = networkSession.NetworkConfig.SpawnTimeout,
276+
ForceSamePrefabs = networkSession.NetworkConfig.ForceSamePrefabs,
277+
RecycleNetworkIds = networkSession.NetworkConfig.RecycleNetworkIds,
278+
NetworkIdRecycleDelay = networkSession.NetworkConfig.NetworkIdRecycleDelay,
279+
RpcHashSize = networkSession.NetworkConfig.RpcHashSize == HashSize.VarIntFourBytes ? 4 : 8,
280+
EnableTimeResync = networkSession.NetworkConfig.EnableTimeResync,
281+
TimeResyncInterval = networkSession.NetworkConfig.TimeResyncInterval,
282+
TickRate = (int)networkSession.NetworkConfig.TickRate,
283+
#if MULTIPLAYER_TOOLS
284+
NetworkMessageMetrics = networkSession.NetworkConfig.NetworkMessageMetrics,
285+
#else
286+
NetworkMessageMetrics = false,
287+
#endif
288+
NetworkProfilingMetrics = networkSession.NetworkConfig.NetworkProfilingMetrics,
289+
WasClient = networkSession.WasClient,
290+
WasServer = networkSession.WasServer,
291+
SessionDuration = networkSession.SessionEnd - networkSession.SessionStart,
292+
};
293+
return networkManagerAnalytics;
294+
}
227295
}
228296
#endif
229297
}

com.unity.netcode.gameobjects/Runtime/Configuration/NetworkConfig.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,48 @@ public class NetworkConfig
168168
[Tooltip("When enabled (default), the player prefab will automatically be spawned (client-side) upon the client being approved and synchronized.")]
169169
public bool AutoSpawnPlayerPrefabClientSide = true;
170170

171+
#if UNITY_EDITOR
172+
/// <summary>
173+
/// Creates a copy of the current <see cref="NetworkConfig"/>
174+
/// </summary>
175+
/// <returns>a copy of this <see cref="NetworkConfig"/></returns>
176+
internal NetworkConfig Copy()
177+
{
178+
var networkConfig = new NetworkConfig()
179+
{
180+
ProtocolVersion = ProtocolVersion,
181+
NetworkTransport = NetworkTransport,
182+
TickRate = TickRate,
183+
ClientConnectionBufferTimeout = ClientConnectionBufferTimeout,
184+
ConnectionApproval = ConnectionApproval,
185+
EnableTimeResync = EnableTimeResync,
186+
TimeResyncInterval = TimeResyncInterval,
187+
EnsureNetworkVariableLengthSafety = EnsureNetworkVariableLengthSafety,
188+
EnableSceneManagement = EnableSceneManagement,
189+
ForceSamePrefabs = ForceSamePrefabs,
190+
RecycleNetworkIds = RecycleNetworkIds,
191+
NetworkIdRecycleDelay = NetworkIdRecycleDelay,
192+
RpcHashSize = RpcHashSize,
193+
LoadSceneTimeOut = LoadSceneTimeOut,
194+
SpawnTimeout = SpawnTimeout,
195+
EnableNetworkLogs = EnableNetworkLogs,
196+
NetworkTopology = NetworkTopology,
197+
UseCMBService = UseCMBService,
198+
AutoSpawnPlayerPrefabClientSide = AutoSpawnPlayerPrefabClientSide,
199+
#if MULTIPLAYER_TOOLS
200+
NetworkMessageMetrics = NetworkMessageMetrics,
201+
#else
202+
NetworkMessageMetrics = false,
203+
#endif
204+
NetworkProfilingMetrics = NetworkProfilingMetrics,
205+
};
206+
207+
return networkConfig;
208+
}
209+
210+
#endif
211+
212+
171213
#if MULTIPLAYER_TOOLS
172214
/// <summary>
173215
/// Controls whether network messaging metrics will be gathered. (defaults to true)

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

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -914,13 +914,31 @@ internal T Value
914914
#if UNITY_EDITOR
915915
internal static INetworkManagerHelper NetworkManagerHelper;
916916

917+
internal struct NetworkSessionInfo
918+
{
919+
public int SessionIndex;
920+
public bool SessionStopped;
921+
public bool PlayerPrefab;
922+
public bool WasServer;
923+
public bool WasClient;
924+
public float SessionStart;
925+
public float SessionEnd;
926+
public bool UsedCMBService;
927+
public string Transport;
928+
public NetworkConfig NetworkConfig;
929+
}
930+
931+
internal static List<NetworkSessionInfo> RecentSessions = new List<NetworkSessionInfo>();
932+
917933
/// <summary>
918934
/// Interface for NetworkManagerHelper
919935
/// </summary>
920936
internal interface INetworkManagerHelper
921937
{
922938
bool NotifyUserOfNestedNetworkManager(NetworkManager networkManager, bool ignoreNetworkManagerCache = false, bool editorTest = false);
923939
void CheckAndNotifyUserNetworkObjectRemoved(NetworkManager networkManager, bool editorTest = false);
940+
941+
void UpdateAnalytics();
924942
}
925943

926944
internal delegate void ResetNetworkManagerDelegate(NetworkManager manager);
@@ -1018,9 +1036,69 @@ internal void OnValidate()
10181036

10191037
private void ModeChanged(PlayModeStateChange change)
10201038
{
1021-
if (IsListening && change == PlayModeStateChange.ExitingPlayMode)
1039+
if (change == PlayModeStateChange.EnteredPlayMode)
10221040
{
1023-
OnApplicationQuit();
1041+
RecentSessions.Clear();
1042+
}
1043+
1044+
if (change == PlayModeStateChange.ExitingPlayMode)
1045+
{
1046+
try
1047+
{
1048+
EndNetworkSession();
1049+
NetworkManagerHelper?.UpdateAnalytics();
1050+
}
1051+
catch (Exception ex)
1052+
{
1053+
Debug.LogException(ex);
1054+
}
1055+
RecentSessions.Clear();
1056+
1057+
if (IsListening)
1058+
{
1059+
OnApplicationQuit();
1060+
}
1061+
}
1062+
}
1063+
1064+
private void BeginNetworkSession()
1065+
{
1066+
if (!EditorAnalytics.enabled)
1067+
{
1068+
return;
1069+
}
1070+
var newSession = new NetworkSessionInfo()
1071+
{
1072+
SessionIndex = RecentSessions.Count,
1073+
WasClient = IsClient,
1074+
WasServer = IsServer,
1075+
SessionStart = Time.realtimeSinceStartup,
1076+
NetworkConfig = NetworkConfig.Copy(),
1077+
PlayerPrefab = NetworkConfig.PlayerPrefab != null,
1078+
Transport = NetworkConfig.NetworkTransport != null ? NetworkConfig.NetworkTransport.GetType().Name : "None",
1079+
};
1080+
RecentSessions.Add(newSession);
1081+
}
1082+
1083+
private void EndNetworkSession()
1084+
{
1085+
// If analytics is disabled, then exit early
1086+
if (!EditorAnalytics.enabled)
1087+
{
1088+
return;
1089+
}
1090+
if (RecentSessions.Count > 0)
1091+
{
1092+
var lastIndex = RecentSessions.Count - 1;
1093+
var recentSession = RecentSessions[lastIndex];
1094+
if (recentSession.SessionStopped)
1095+
{
1096+
return;
1097+
}
1098+
recentSession.SessionEnd = Time.realtimeSinceStartup;
1099+
recentSession.UsedCMBService = CMBServiceConnection;
1100+
recentSession.SessionStopped = true;
1101+
RecentSessions[lastIndex] = recentSession;
10241102
}
10251103
}
10261104
#endif
@@ -1285,6 +1363,9 @@ internal void Initialize(bool server)
12851363

12861364
NetworkConfig.InitializePrefabs();
12871365
PrefabHandler.RegisterPlayerPrefab();
1366+
#if UNITY_EDITOR
1367+
BeginNetworkSession();
1368+
#endif
12881369
}
12891370

12901371
private enum StartType
@@ -1584,6 +1665,10 @@ private void OnSceneUnloaded(Scene scene)
15841665

15851666
internal void ShutdownInternal()
15861667
{
1668+
#if UNITY_EDITOR
1669+
EndNetworkSession();
1670+
#endif
1671+
15871672
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
15881673
{
15891674
NetworkLog.LogInfo(nameof(ShutdownInternal));

0 commit comments

Comments
 (0)