Skip to content

Commit d134e89

Browse files
committed
reverting change to move to a separate PR
1 parent f0a7755 commit d134e89

File tree

4 files changed

+85
-105
lines changed

4 files changed

+85
-105
lines changed

com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using MLAPI.Serialization;
1414
using MLAPI.Serialization.Pooled;
1515
using MLAPI.Transports;
16+
using Unity.Profiling;
1617

1718
namespace MLAPI
1819
{
@@ -296,6 +297,11 @@ protected NetworkBehaviour GetNetworkBehaviour(ushort behaviourId)
296297
/// </summary>
297298
public ulong OwnerClientId => NetworkObject.OwnerClientId;
298299

300+
/// <summary>
301+
/// Stores the network tick at the NetworkBehaviourUpdate time
302+
/// This allows sending NetworkVariables not more often than once per network tick, regardless of the update rate
303+
/// </summary>
304+
public static ushort CurrentTick { get; private set; }
299305

300306
/// <summary>
301307
/// Gets called when message handlers are ready to be registered and the network is setup
@@ -347,6 +353,7 @@ public virtual void OnNetworkObjectParentChanged(NetworkObject parentNetworkObje
347353
private readonly List<NetworkChannel> m_ChannelsForNetworkVariableGroups = new List<NetworkChannel>();
348354
internal readonly List<INetworkVariable> NetworkVariableFields = new List<INetworkVariable>();
349355

356+
private static HashSet<NetworkObject> s_Touched = new HashSet<NetworkObject>();
350357
private static Dictionary<Type, FieldInfo[]> s_FieldTypes = new Dictionary<Type, FieldInfo[]>();
351358

352359
private static FieldInfo[] GetFieldInfoForType(Type type)
@@ -439,6 +446,83 @@ internal void InitializeVariables()
439446
}
440447
}
441448

449+
#if DEVELOPMENT_BUILD || UNITY_EDITOR
450+
private static ProfilerMarker s_NetworkBehaviourUpdate = new ProfilerMarker($"{nameof(NetworkBehaviour)}.{nameof(NetworkBehaviourUpdate)}");
451+
#endif
452+
453+
internal static void NetworkBehaviourUpdate(NetworkManager networkManager)
454+
{
455+
// Do not execute NetworkBehaviourUpdate more than once per network tick
456+
ushort tick = networkManager.NetworkTickSystem.GetTick();
457+
if (tick == CurrentTick)
458+
{
459+
return;
460+
}
461+
462+
CurrentTick = tick;
463+
464+
#if DEVELOPMENT_BUILD || UNITY_EDITOR
465+
s_NetworkBehaviourUpdate.Begin();
466+
#endif
467+
try
468+
{
469+
if (networkManager.IsServer)
470+
{
471+
s_Touched.Clear();
472+
for (int i = 0; i < networkManager.ConnectedClientsList.Count; i++)
473+
{
474+
var client = networkManager.ConnectedClientsList[i];
475+
var spawnedObjs = networkManager.SpawnManager.SpawnedObjectsList;
476+
s_Touched.UnionWith(spawnedObjs);
477+
foreach (var sobj in spawnedObjs)
478+
{
479+
// Sync just the variables for just the objects this client sees
480+
for (int k = 0; k < sobj.ChildNetworkBehaviours.Count; k++)
481+
{
482+
sobj.ChildNetworkBehaviours[k].VariableUpdate(client.ClientId);
483+
}
484+
}
485+
}
486+
487+
// Now, reset all the no-longer-dirty variables
488+
foreach (var sobj in s_Touched)
489+
{
490+
for (int k = 0; k < sobj.ChildNetworkBehaviours.Count; k++)
491+
{
492+
sobj.ChildNetworkBehaviours[k].PostNetworkVariableWrite();
493+
}
494+
}
495+
}
496+
else
497+
{
498+
// when client updates the sever, it tells it about all its objects
499+
foreach (var sobj in networkManager.SpawnManager.SpawnedObjectsList)
500+
{
501+
for (int k = 0; k < sobj.ChildNetworkBehaviours.Count; k++)
502+
{
503+
sobj.ChildNetworkBehaviours[k].VariableUpdate(networkManager.ServerClientId);
504+
}
505+
}
506+
507+
// Now, reset all the no-longer-dirty variables
508+
foreach (var sobj in networkManager.SpawnManager.SpawnedObjectsList)
509+
{
510+
for (int k = 0; k < sobj.ChildNetworkBehaviours.Count; k++)
511+
{
512+
sobj.ChildNetworkBehaviours[k].PostNetworkVariableWrite();
513+
}
514+
}
515+
}
516+
}
517+
finally
518+
{
519+
#if DEVELOPMENT_BUILD || UNITY_EDITOR
520+
s_NetworkBehaviourUpdate.End();
521+
#endif
522+
}
523+
}
524+
525+
442526
internal void PreNetworkVariableWrite()
443527
{
444528
// reset our "which variables got written" data

com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviourUpdater.cs

Lines changed: 0 additions & 93 deletions
This file was deleted.

com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviourUpdater.cs.meta

Lines changed: 0 additions & 3 deletions
This file was deleted.

com.unity.multiplayer.mlapi/Runtime/Core/NetworkManager.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public class NetworkManager : MonoBehaviour, INetworkUpdateSystem, IProfilableTr
5959
internal NetworkTickSystem NetworkTickSystem { get; private set; }
6060

6161
internal SnapshotSystem SnapshotSystem { get; private set; }
62-
internal NetworkBehaviourUpdater BehaviourUpdater { get; private set; }
6362

6463
private NetworkPrefabHandler m_PrefabHandler;
6564
public NetworkPrefabHandler PrefabHandler
@@ -349,8 +348,6 @@ private void Initialize(bool server)
349348

350349
SceneManager = new NetworkSceneManager(this);
351350

352-
BehaviourUpdater = new NetworkBehaviourUpdater();
353-
354351
// Only create this if it's not already set (like in test cases)
355352
MessageHandler ??= CreateMessageHandler();
356353

@@ -847,11 +844,6 @@ public void Shutdown()
847844
CustomMessagingManager = null;
848845
}
849846

850-
if (BehaviourUpdater != null)
851-
{
852-
BehaviourUpdater = null;
853-
}
854-
855847
//The Transport is set during Init time, thus it is possible for the Transport to be null
856848
NetworkConfig?.NetworkTransport?.Shutdown();
857849
}
@@ -939,7 +931,7 @@ private void OnNetworkPreUpdate()
939931
if (NetworkConfig.EnableNetworkVariable)
940932
{
941933
// Do NetworkVariable updates
942-
BehaviourUpdater.NetworkBehaviourUpdate(this);
934+
NetworkBehaviour.NetworkBehaviourUpdate(this);
943935
}
944936

945937
if (!IsServer && NetworkConfig.EnableMessageBuffering)

0 commit comments

Comments
 (0)