Skip to content

Commit 82c3057

Browse files
fix
When building the NetworkObject distribution table, don't add a NetworkObject to the table if the target client is not an observer.
1 parent 8b29700 commit 82c3057

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,7 +1815,7 @@ internal void Shutdown()
18151815
/// </summary>
18161816
/// <param name="objectByTypeAndOwner">the table to populate</param>
18171817
/// <param name="objectTypeCount">the total number of the specific object type to distribute</param>
1818-
internal void GetObjectDistribution(ref Dictionary<uint, Dictionary<ulong, List<NetworkObject>>> objectByTypeAndOwner, ref Dictionary<uint, int> objectTypeCount)
1818+
internal void GetObjectDistribution(ulong clientId, ref Dictionary<uint, Dictionary<ulong, List<NetworkObject>>> objectByTypeAndOwner, ref Dictionary<uint, int> objectTypeCount)
18191819
{
18201820
// DANGO-TODO-MVP: Remove this once the service handles object distribution
18211821
var onlyIncludeOwnedObjects = NetworkManager.CMBServiceConnection;
@@ -1844,6 +1844,11 @@ internal void GetObjectDistribution(ref Dictionary<uint, Dictionary<ulong, List<
18441844
// At this point we only allow things marked with the distributable permission and is not locked to be distributed
18451845
if (networkObject.IsOwnershipDistributable && !networkObject.IsOwnershipLocked)
18461846
{
1847+
// Don't include anything that is not visible to the new client
1848+
if (!networkObject.Observers.Contains(clientId))
1849+
{
1850+
continue;
1851+
}
18471852

18481853
// We have to check if it is an in-scene placed NetworkObject and if it is get the source prefab asset GlobalObjectIdHash value of the in-scene placed instance
18491854
// since all in-scene placed instances use unique GlobalObjectIdHash values.
@@ -1913,7 +1918,7 @@ internal void DistributeNetworkObjects(ulong clientId)
19131918
var objectTypeCount = new Dictionary<uint, int>();
19141919

19151920
// Get all spawned objects by type and then by client owner that are spawned and can be distributed
1916-
GetObjectDistribution(ref distributedNetworkObjects, ref objectTypeCount);
1921+
GetObjectDistribution(clientId, ref distributedNetworkObjects, ref objectTypeCount);
19171922

19181923
var clientCount = NetworkManager.ConnectedClientsIds.Count;
19191924

@@ -1951,7 +1956,6 @@ internal void DistributeNetworkObjects(ulong clientId)
19511956

19521957
var maxDistributeCount = Mathf.Max(ownerList.Value.Count - objPerClient, 1);
19531958
var distributed = 0;
1954-
19551959
// For now when we have more players then distributed NetworkObjects that
19561960
// a specific client owns, just assign half of the NetworkObjects to the new client
19571961
var offsetCount = Mathf.Max((int)Math.Round((float)(ownerList.Value.Count / objPerClient)), 1);
@@ -1964,11 +1968,6 @@ internal void DistributeNetworkObjects(ulong clientId)
19641968
{
19651969
if ((i % offsetCount) == 0)
19661970
{
1967-
while (!ownerList.Value[i].Observers.Contains(clientId))
1968-
{
1969-
i++;
1970-
}
1971-
19721971
var children = ownerList.Value[i].GetComponentsInChildren<NetworkObject>();
19731972
// Since the ownerList.Value[i] has to be distributable, then transfer all child NetworkObjects
19741973
// with the same owner clientId and are marked as distributable also to the same client to keep
@@ -2011,7 +2010,7 @@ internal void DistributeNetworkObjects(ulong clientId)
20112010
var builder = new StringBuilder();
20122011
distributedNetworkObjects.Clear();
20132012
objectTypeCount.Clear();
2014-
GetObjectDistribution(ref distributedNetworkObjects, ref objectTypeCount);
2013+
GetObjectDistribution(clientId, ref distributedNetworkObjects, ref objectTypeCount);
20152014
builder.AppendLine($"Client Relative Distributed Object Count: (distribution follows)");
20162015
// Cycle through each prefab type
20172016
foreach (var objectTypeEntry in distributedNetworkObjects)
@@ -2026,7 +2025,6 @@ internal void DistributeNetworkObjects(ulong clientId)
20262025
}
20272026
Debug.Log(builder.ToString());
20282027
}
2029-
20302028
}
20312029

20322030
internal struct DeferredDespawnObject

0 commit comments

Comments
 (0)