Skip to content

Commit 7631077

Browse files
committed
move spawn setup into NetworkObject.SetupOnSpawn
1 parent 51fa366 commit 7631077

2 files changed

Lines changed: 76 additions & 66 deletions

File tree

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

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,7 +2001,6 @@ public NetworkObject InstantiateAndSpawn(NetworkManager networkManager, ulong ow
20012001
public void Spawn(bool destroyWithScene = false)
20022002
{
20032003
var clientId = NetworkManager.DistributedAuthorityMode ? NetworkManager.LocalClientId : NetworkManager.ServerClientId;
2004-
m_HasAuthority = NetworkManager.DistributedAuthorityMode ? OwnerClientId == NetworkManager.LocalClientId : NetworkManager.IsServer;
20052004
SpawnInternal(destroyWithScene, clientId, false);
20062005
}
20072006

@@ -2053,6 +2052,72 @@ public void Despawn(bool destroy = true)
20532052
NetworkManagerOwner.SpawnManager.DespawnObject(this, destroy);
20542053
}
20552054

2055+
internal void SetupOnSpawn(ulong networkId, bool sceneObject, bool playerObject, ulong ownerClientId, bool destroyWithScene)
2056+
{
2057+
#pragma warning disable CS0618 // Type or member is obsolete
2058+
// Obsolete with warning means we need the underlying behaviour to keep existing
2059+
// TODO: remove in the 3.x branch
2060+
SetSceneObjectStatus(sceneObject);
2061+
#pragma warning restore CS0618 // Type or member is obsolete
2062+
2063+
// Always check to make sure our scene of origin is properly set for in-scene placed NetworkObjects
2064+
// Note: Always check SceneOriginHandle directly at this specific location.
2065+
if (InScenePlaced && SceneOriginHandle.IsEmpty())
2066+
{
2067+
SceneOrigin = gameObject.scene;
2068+
}
2069+
2070+
NetworkObjectId = networkId;
2071+
2072+
DestroyWithScene = sceneObject || destroyWithScene;
2073+
2074+
IsPlayerObject = playerObject;
2075+
2076+
OwnerClientId = ownerClientId;
2077+
2078+
// When spawned, previous owner is always the first assigned owner
2079+
PreviousOwnerId = ownerClientId;
2080+
2081+
// If this is the player and the client is the owner, then lock ownership by default
2082+
if (NetworkManagerOwner.DistributedAuthorityMode && NetworkManagerOwner.LocalClientId == ownerClientId && playerObject)
2083+
{
2084+
AddOwnershipExtended(OwnershipStatusExtended.Locked);
2085+
}
2086+
2087+
m_HasAuthority = NetworkManagerOwner.DistributedAuthorityMode ? OwnerClientId == NetworkManagerOwner.LocalClientId : NetworkManagerOwner.IsServer;
2088+
IsSpawned = true;
2089+
2090+
// If we are not running in DA mode, this is the server, and the NetworkObject has SpawnWithObservers set,
2091+
// then add all connected clients as observers
2092+
if (!NetworkManagerOwner.DistributedAuthorityMode && NetworkManagerOwner.IsServer && SpawnWithObservers)
2093+
{
2094+
// If running as a server only, then make sure to always add the server's client identifier
2095+
if (!NetworkManagerOwner.IsHost)
2096+
{
2097+
AddObserver(NetworkManagerOwner.LocalClientId);
2098+
}
2099+
2100+
// Add client observers
2101+
for (int i = 0; i < NetworkManagerOwner.ConnectedClientsIds.Count; i++)
2102+
{
2103+
// If CheckObjectVisibility has a callback, then allow that method determine who the observers are.
2104+
if (CheckObjectVisibility != null && !CheckObjectVisibility(NetworkManagerOwner.ConnectedClientsIds[i]))
2105+
{
2106+
continue;
2107+
}
2108+
AddObserver(NetworkManagerOwner.ConnectedClientsIds[i]);
2109+
}
2110+
}
2111+
2112+
// If we are an in-scene placed NetworkObject and our InScenePlacedSourceGlobalObjectIdHash is set
2113+
// then assign this to the PrefabGlobalObjectIdHash
2114+
if (InScenePlaced && InScenePlacedSourceGlobalObjectIdHash != 0)
2115+
{
2116+
PrefabGlobalObjectIdHash = InScenePlacedSourceGlobalObjectIdHash;
2117+
}
2118+
2119+
}
2120+
20562121
internal void ResetOnDespawn()
20572122
{
20582123
// Always clear out the observers list when despawned
@@ -2118,7 +2183,10 @@ internal void InvokeBehaviourOnOwnershipChanged(ulong originalOwnerClientId, ulo
21182183
var isPreviousOwner = originalOwnerClientId == NetworkManagerOwner.LocalClientId;
21192184
var isNewOwner = newOwnerClientId == NetworkManagerOwner.LocalClientId;
21202185

2121-
m_HasAuthority = distributedAuthorityMode ? OwnerClientId == NetworkManagerOwner.LocalClientId : NetworkManagerOwner.IsServer;
2186+
if (distributedAuthorityMode)
2187+
{
2188+
m_HasAuthority = isNewOwner;
2189+
}
21222190

21232191
if (distributedAuthorityMode || isPreviousOwner)
21242192
{

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

Lines changed: 6 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,9 +1295,10 @@ internal bool NonAuthorityLocalSpawn(in NetworkObject.SerializedObject serialize
12951295
}
12961296

12971297
/// <summary>
1298-
/// Handles the all the final setup and spawning needed for
1298+
/// Handles all the final setup and spawning needed for spawning a NetworkObject locally.
12991299
/// </summary>
1300-
/// <returns>boolean indicating whether the spawn succeeded. Internal dev note: THIS IS A CATCH FOR OURSELVES. DON'T PULL OUT</returns>
1300+
/// <returns>boolean indicating whether the spawn succeeded.</returns>
1301+
// Internal dev note: THIS IS A CATCH FOR OURSELVES. DON'T PULL OUT
13011302
internal bool SpawnNetworkObjectLocallyCommon(NetworkObject networkObject, ulong networkId, bool sceneObject, bool playerObject, ulong ownerClientId, bool destroyWithScene)
13021303
{
13031304
// TODO: Replace the following checks with internal Netcode asserts
@@ -1311,7 +1312,7 @@ internal bool SpawnNetworkObjectLocallyCommon(NetworkObject networkObject, ulong
13111312
return false;
13121313
}
13131314

1314-
if (networkId == default)
1315+
if (networkId == 0)
13151316
{
13161317
if (NetworkManager.LogLevel <= LogLevel.Error)
13171318
{
@@ -1320,70 +1321,18 @@ internal bool SpawnNetworkObjectLocallyCommon(NetworkObject networkObject, ulong
13201321
return false;
13211322
}
13221323

1323-
#pragma warning disable CS0618 // Type or member is obsolete
1324-
// Obsolete with warning means we need the underlying behaviour to keep existing
1325-
// TODO: remove in the 3.x branch
1326-
networkObject.SetSceneObjectStatus(sceneObject);
1327-
#pragma warning restore CS0618 // Type or member is obsolete
1324+
networkObject.SetupOnSpawn(networkId, sceneObject, playerObject, ownerClientId, destroyWithScene);
13281325

1329-
// Always check to make sure our scene of origin is properly set for in-scene placed NetworkObjects
1330-
// Note: Always check SceneOriginHandle directly at this specific location.
1331-
if (networkObject.InScenePlaced && networkObject.SceneOriginHandle.IsEmpty())
1332-
{
1333-
networkObject.SceneOrigin = networkObject.gameObject.scene;
1334-
}
1335-
1336-
networkObject.NetworkObjectId = networkId;
1337-
1338-
networkObject.DestroyWithScene = sceneObject || destroyWithScene;
1339-
1340-
networkObject.IsPlayerObject = playerObject;
1341-
1342-
networkObject.OwnerClientId = ownerClientId;
1343-
1344-
// When spawned, previous owner is always the first assigned owner
1345-
networkObject.PreviousOwnerId = ownerClientId;
1346-
1347-
// If this the player and the client is the owner, then lock ownership by default
1348-
if (NetworkManager.DistributedAuthorityMode && NetworkManager.LocalClientId == ownerClientId && playerObject)
1349-
{
1350-
networkObject.AddOwnershipExtended(NetworkObject.OwnershipStatusExtended.Locked);
1351-
}
1352-
1353-
networkObject.IsSpawned = true;
13541326
SpawnedObjects.Add(networkObject.NetworkObjectId, networkObject);
13551327
SpawnedObjectsList.Add(networkObject);
1356-
1357-
// If we are not running in DA mode, this is the server, and the NetworkObject has SpawnWithObservers set,
1358-
// then add all connected clients as observers
1359-
if (!NetworkManager.DistributedAuthorityMode && NetworkManager.IsServer && networkObject.SpawnWithObservers)
1360-
{
1361-
// If running as a server only, then make sure to always add the server's client identifier
1362-
if (!NetworkManager.IsHost)
1363-
{
1364-
networkObject.AddObserver(NetworkManager.LocalClientId);
1365-
}
1366-
1367-
// Add client observers
1368-
for (int i = 0; i < NetworkManager.ConnectedClientsIds.Count; i++)
1369-
{
1370-
// If CheckObjectVisibility has a callback, then allow that method determine who the observers are.
1371-
if (networkObject.CheckObjectVisibility != null && !networkObject.CheckObjectVisibility(NetworkManager.ConnectedClientsIds[i]))
1372-
{
1373-
continue;
1374-
}
1375-
networkObject.AddObserver(NetworkManager.ConnectedClientsIds[i]);
1376-
}
1377-
}
1378-
13791328
networkObject.ApplyNetworkParenting();
1329+
13801330
NetworkObject.CheckOrphanChildren();
13811331

13821332
AddNetworkObjectToSceneChangedUpdates(networkObject);
13831333

13841334
networkObject.InvokeBehaviourNetworkSpawn();
13851335

1386-
13871336
// Only dynamically spawned NetworkObjects are allowed
13881337
if (!sceneObject)
13891338
{
@@ -1395,13 +1344,6 @@ internal bool SpawnNetworkObjectLocallyCommon(NetworkObject networkObject, ulong
13951344
UpdateNetworkClientPlayer(networkObject);
13961345
}
13971346

1398-
// If we are an in-scene placed NetworkObject and our InScenePlacedSourceGlobalObjectIdHash is set
1399-
// then assign this to the PrefabGlobalObjectIdHash
1400-
if (networkObject.InScenePlaced && networkObject.InScenePlacedSourceGlobalObjectIdHash != 0)
1401-
{
1402-
networkObject.PrefabGlobalObjectIdHash = networkObject.InScenePlacedSourceGlobalObjectIdHash;
1403-
}
1404-
14051347
return true;
14061348
}
14071349

0 commit comments

Comments
 (0)