Skip to content

Commit 49cc8c1

Browse files
committed
Simplify changes
1 parent a113a7a commit 49cc8c1

File tree

4 files changed

+73
-114
lines changed

4 files changed

+73
-114
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,11 @@ public NetworkObject NetworkObject
614614
/// </summary>
615615
public ushort NetworkBehaviourId { get; internal set; }
616616

617+
/// <summary>
618+
/// Internally caches the Id of this behaviour in a NetworkObject. Makes look-up faster
619+
/// </summary>
620+
internal ushort NetworkBehaviourIdCache = 0;
621+
617622
/// <summary>
618623
/// Returns the NetworkBehaviour with a given BehaviourId for the current NetworkObject.
619624
/// </summary>

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

Lines changed: 50 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,8 @@ private bool InternalHasAuthority()
11191119
}
11201120

11211121
/// <summary>
1122-
/// The NetworkManager that is responsible for this NetworkObject instance.
1122+
/// The NetworkManager that owns this NetworkObject.
1123+
/// This property controls where this NetworkObject belongs.
11231124
/// This property is null by default currently, which means that the above NetworkManager getter will return the Singleton.
11241125
/// In the future this is the path where alternative NetworkManagers should be injected for running multi NetworkManagers
11251126
/// </summary>
@@ -1770,9 +1771,6 @@ internal void SpawnInternal(bool destroyWithScene, ulong ownerClientId, bool pla
17701771
{
17711772
if (NetworkManagerOwner == null)
17721773
{
1773-
#if TEST_NO_SINGLETON
1774-
Debug.LogError("NetworkObject has no owner client! setting as singleton owner");
1775-
#endif
17761774
NetworkManagerOwner = NetworkManager.Singleton;
17771775
}
17781776
if (!NetworkManager.IsListening)
@@ -1929,7 +1927,7 @@ public NetworkObject InstantiateAndSpawn(NetworkManager networkManager, ulong ow
19291927
return null;
19301928
}
19311929

1932-
return networkManager.SpawnManager.InstantiateAndSpawnNoParameterChecks(this, ownerClientId, destroyWithScene, isPlayerObject, forceOverride, position, rotation, networkManager);
1930+
return networkManager.SpawnManager.InstantiateAndSpawnNoParameterChecks(this, ownerClientId, destroyWithScene, isPlayerObject, forceOverride, position, rotation);
19331931
}
19341932

19351933
/// <summary>
@@ -2060,13 +2058,6 @@ internal void InvokeOwnershipChanged(ulong previous, ulong next)
20602058

20612059
internal void InvokeBehaviourOnNetworkObjectParentChanged(NetworkObject parentNetworkObject)
20622060
{
2063-
if (NetworkManagerOwner == null)
2064-
{
2065-
#if TEST_NO_SINGLETON
2066-
Debug.LogError("NetworkManagerOwner should be set! Setting owner to NetworkManager.Singleton");
2067-
#endif
2068-
NetworkManagerOwner = NetworkManager.Singleton;
2069-
}
20702061
for (int i = 0; i < ChildNetworkBehaviours.Count; i++)
20712062
{
20722063
// Invoke internal notification
@@ -2295,13 +2286,6 @@ private void OnTransformParentChanged()
22952286

22962287
if (!IsSpawned)
22972288
{
2298-
if (NetworkManagerOwner == null)
2299-
{
2300-
#if TEST_NO_SINGLETON
2301-
Debug.LogError("NetworkManagerOwner should be set! Setting owner to NetworkManager.Singleton");
2302-
#endif
2303-
NetworkManagerOwner = NetworkManager;
2304-
}
23052289
AuthorityAppliedParenting = false;
23062290
// and we are removing the parent, then go ahead and allow parenting to occur
23072291
if (transform.parent == null)
@@ -2550,7 +2534,7 @@ internal static void CheckOrphanChildren()
25502534

25512535
internal void InvokeBehaviourNetworkPreSpawn()
25522536
{
2553-
var networkManager = NetworkManagerOwner;
2537+
var networkManager = NetworkManager;
25542538
for (int i = 0; i < ChildNetworkBehaviours.Count; i++)
25552539
{
25562540
if (ChildNetworkBehaviours[i].gameObject.activeInHierarchy)
@@ -2633,68 +2617,53 @@ internal List<NetworkBehaviour> ChildNetworkBehaviours
26332617
{
26342618
get
26352619
{
2636-
if (m_ChildNetworkBehaviours == null)
2620+
if (m_ChildNetworkBehaviours != null)
26372621
{
2638-
m_ChildNetworkBehaviours = BuildChildBehavioursList();
2622+
return m_ChildNetworkBehaviours;
26392623
}
26402624

2641-
return m_ChildNetworkBehaviours;
2642-
}
2643-
}
2644-
2645-
private List<NetworkBehaviour> BuildChildBehavioursList()
2646-
{
2647-
if (NetworkManagerOwner == null)
2648-
{
2649-
#if TEST_NO_SINGLETON
2650-
Debug.LogError("NetworkManagerOwner should be set! Setting owner to NetworkManager.Singleton");
2651-
#endif
2652-
NetworkManagerOwner = NetworkManager.Singleton;
2653-
}
2654-
2655-
var networkBehaviours = GetComponentsInChildren<NetworkBehaviour>(true);
2656-
var childBehaviours = new List<NetworkBehaviour>(networkBehaviours.Length);
2657-
2658-
foreach (var behaviour in networkBehaviours)
2659-
{
2660-
// Find the first parent NetworkObject of this child
2661-
// if it's not ourselves, this childBehaviour belongs to a different NetworkObject.
2662-
var networkObj = behaviour.GetComponentInParent<NetworkObject>();
2663-
if (networkObj != this)
2625+
m_ChildNetworkBehaviours = new List<NetworkBehaviour>();
2626+
var networkBehaviours = GetComponentsInChildren<NetworkBehaviour>(true);
2627+
for (int i = 0; i < networkBehaviours.Length; i++)
26642628
{
2665-
continue;
2666-
}
2629+
// Find the first parent NetworkObject of this child
2630+
// if it's not ourselves, this childBehaviour belongs to a different NetworkObject.
2631+
var networkObj = networkBehaviours[i].GetComponentInParent<NetworkObject>();
2632+
if (networkObj != this)
2633+
{
2634+
continue;
2635+
}
26672636

2668-
// Set ourselves as the NetworkObject that this behaviour belongs to and add it to the child list
2669-
var nextIndex = childBehaviours.Count;
2670-
childBehaviours.Add(behaviour);
2671-
behaviour.SetNetworkObject(this, (ushort)nextIndex);
2637+
// Set ourselves as the NetworkObject that this behaviour belongs to and add it to the child list
2638+
var nextIndex = (ushort)m_ChildNetworkBehaviours.Count;
2639+
networkBehaviours[i].SetNetworkObject(this, nextIndex);
2640+
m_ChildNetworkBehaviours.Add(networkBehaviours[i]);
26722641

2673-
var type = behaviour.GetType();
2674-
if (type == typeof(NetworkTransform) || type.IsAssignableFrom(typeof(NetworkTransform)) || type.IsSubclassOf(typeof(NetworkTransform)))
2675-
{
2676-
if (NetworkTransforms == null)
2642+
var type = networkBehaviours[i].GetType();
2643+
if (type == typeof(NetworkTransform) || type.IsInstanceOfType(typeof(NetworkTransform)) || type.IsSubclassOf(typeof(NetworkTransform)))
26772644
{
2678-
NetworkTransforms = new List<NetworkTransform>();
2645+
if (NetworkTransforms == null)
2646+
{
2647+
NetworkTransforms = new List<NetworkTransform>();
2648+
}
2649+
var networkTransform = networkBehaviours[i] as NetworkTransform;
2650+
networkTransform.IsNested = i != 0 && networkTransform.gameObject != gameObject;
2651+
NetworkTransforms.Add(networkTransform);
26792652
}
2680-
var networkTransform = behaviour as NetworkTransform;
2681-
networkTransform.IsNested = networkTransform.gameObject != gameObject;
2682-
NetworkTransforms.Add(networkTransform);
2683-
}
26842653
#if COM_UNITY_MODULES_PHYSICS || COM_UNITY_MODULES_PHYSICS2D
2685-
else if (type.IsSubclassOf(typeof(NetworkRigidbodyBase)))
2686-
{
2687-
if (NetworkRigidbodies == null)
2654+
else if (type.IsSubclassOf(typeof(NetworkRigidbodyBase)))
26882655
{
2689-
NetworkRigidbodies = new List<NetworkRigidbodyBase>();
2656+
if (NetworkRigidbodies == null)
2657+
{
2658+
NetworkRigidbodies = new List<NetworkRigidbodyBase>();
2659+
}
2660+
NetworkRigidbodies.Add(networkBehaviours[i] as NetworkRigidbodyBase);
26902661
}
2691-
NetworkRigidbodies.Add(behaviour as NetworkRigidbodyBase);
2692-
}
26932662
#endif
2694-
}
2663+
}
26952664

2696-
childBehaviours.TrimExcess();
2697-
return childBehaviours;
2665+
return m_ChildNetworkBehaviours;
2666+
}
26982667
}
26992668

27002669
/// <summary>
@@ -2775,24 +2744,26 @@ internal static void VerifyParentingStatus()
27752744
/// </returns>
27762745
public ushort GetNetworkBehaviourOrderIndex(NetworkBehaviour instance)
27772746
{
2778-
if (!IsSpawned)
2747+
// read the cached index, and verify it first
2748+
if (instance.NetworkBehaviourIdCache < ChildNetworkBehaviours.Count)
27792749
{
2780-
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
2750+
if (ChildNetworkBehaviours[instance.NetworkBehaviourIdCache] == instance)
27812751
{
2782-
Debug.LogWarning($"{nameof(NetworkObject)} is not spawned yet. Cannot get index of NetworkBehaviour.");
2752+
return instance.NetworkBehaviourIdCache;
27832753
}
2784-
return 0;
2754+
2755+
// invalid cached id reset
2756+
instance.NetworkBehaviourIdCache = default;
27852757
}
27862758

2787-
// read the cached index, and verify it first
2788-
if (instance.NetworkBehaviourId < ChildNetworkBehaviours.Count)
2759+
for (ushort i = 0; i < ChildNetworkBehaviours.Count; i++)
27892760
{
2790-
if (ChildNetworkBehaviours[instance.NetworkBehaviourId] == instance)
2761+
if (ChildNetworkBehaviours[i] == instance)
27912762
{
2792-
return instance.NetworkBehaviourId;
2763+
// cache the id, for next query
2764+
instance.NetworkBehaviourIdCache = i;
2765+
return i;
27932766
}
2794-
2795-
Debug.LogError("Network behaviour at index has changed. This should not be possible.");
27962767
}
27972768

27982769
return 0;
@@ -2805,15 +2776,6 @@ public ushort GetNetworkBehaviourOrderIndex(NetworkBehaviour instance)
28052776
/// <returns>The <see cref="NetworkBehaviour"/> at the ordered index value or null if it does not exist.</returns>
28062777
public NetworkBehaviour GetNetworkBehaviourAtOrderIndex(ushort index)
28072778
{
2808-
if (!IsSpawned)
2809-
{
2810-
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
2811-
{
2812-
Debug.LogWarning($"{nameof(NetworkObject)} is not spawned yet. Cannot get NetworkBehaviour at index.");
2813-
}
2814-
return null;
2815-
}
2816-
28172779
if (index >= ChildNetworkBehaviours.Count)
28182780
{
28192781
if (NetworkLog.CurrentLogLevel <= LogLevel.Error)

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

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ public NetworkObject InstantiateAndSpawn(NetworkObject networkPrefab, ulong owne
760760
/// <summary>
761761
/// !!! Does not perform any parameter checks prior to attempting to instantiate and spawn the NetworkObject !!!
762762
/// </summary>
763-
internal NetworkObject InstantiateAndSpawnNoParameterChecks(NetworkObject networkPrefab, ulong ownerClientId = NetworkManager.ServerClientId, bool destroyWithScene = false, bool isPlayerObject = false, bool forceOverride = false, Vector3 position = default, Quaternion rotation = default, NetworkManager networkManager = null)
763+
internal NetworkObject InstantiateAndSpawnNoParameterChecks(NetworkObject networkPrefab, ulong ownerClientId = NetworkManager.ServerClientId, bool destroyWithScene = false, bool isPlayerObject = false, bool forceOverride = false, Vector3 position = default, Quaternion rotation = default)
764764
{
765765
NetworkObject networkObject;
766766
// - Host and clients always instantiate the override if one exists.
@@ -782,8 +782,6 @@ internal NetworkObject InstantiateAndSpawnNoParameterChecks(NetworkObject networ
782782
Debug.LogError($"Failed to instantiate and spawn {networkPrefab.name}!");
783783
return null;
784784
}
785-
786-
networkObject.NetworkManagerOwner = NetworkManager;
787785
networkObject.IsPlayerObject = isPlayerObject;
788786
networkObject.transform.SetPositionAndRotation(position, rotation);
789787
// If spawning as a player, then invoke SpawnAsPlayerObject
@@ -1051,7 +1049,6 @@ internal void SpawnNetworkObjectLocally(NetworkObject networkObject, ulong netwo
10511049
Debug.LogError("Spawning NetworkObjects with nested NetworkObjects is only supported for scene objects. Child NetworkObjects will not be spawned over the network!");
10521050
}
10531051
}
1054-
10551052
// Invoke NetworkBehaviour.OnPreSpawn methods
10561053
networkObject.NetworkManagerOwner = NetworkManager;
10571054
networkObject.InvokeBehaviourNetworkPreSpawn();
@@ -1473,25 +1470,24 @@ internal void ServerSpawnSceneObjectsOnStartSweep()
14731470
var networkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>();
14741471
#endif
14751472
var networkObjectsToSpawn = new List<NetworkObject>();
1476-
foreach (var networkObject in networkObjects)
1473+
for (int i = 0; i < networkObjects.Length; i++)
14771474
{
1478-
if (networkObject.NetworkManager != NetworkManager)
1479-
{
1480-
continue;
1481-
}
1482-
// This used to be two loops.
1483-
// The first added all NetworkObjects to a list and the second spawned all NetworkObjects in the list.
1484-
// Now, a parent will set its children's IsSceneObject value when spawned, so we check for null or for true.
1485-
if (networkObject.IsSceneObject == null || (networkObject.IsSceneObject.HasValue && networkObject.IsSceneObject.Value))
1475+
if (networkObjects[i].NetworkManager == NetworkManager)
14861476
{
1487-
var ownerId = networkObject.OwnerClientId;
1488-
if (NetworkManager.DistributedAuthorityMode)
1477+
// This used to be two loops.
1478+
// The first added all NetworkObjects to a list and the second spawned all NetworkObjects in the list.
1479+
// Now, a parent will set its children's IsSceneObject value when spawned, so we check for null or for true.
1480+
if (networkObjects[i].IsSceneObject == null || (networkObjects[i].IsSceneObject.HasValue && networkObjects[i].IsSceneObject.Value))
14891481
{
1490-
ownerId = NetworkManager.LocalClientId;
1491-
}
1482+
var ownerId = networkObjects[i].OwnerClientId;
1483+
if (NetworkManager.DistributedAuthorityMode)
1484+
{
1485+
ownerId = NetworkManager.LocalClientId;
1486+
}
14921487

1493-
SpawnNetworkObjectLocally(networkObject, GetNetworkObjectId(), true, false, ownerId, true);
1494-
networkObjectsToSpawn.Add(networkObject);
1488+
SpawnNetworkObjectLocally(networkObjects[i], GetNetworkObjectId(), true, false, ownerId, true);
1489+
networkObjectsToSpawn.Add(networkObjects[i]);
1490+
}
14951491
}
14961492
}
14971493

com.unity.netcode.gameobjects/Tests/Runtime/TestHelpers/NetcodeIntegrationTest.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2560,14 +2560,10 @@ public static void SimulateOneFrame()
25602560
{
25612561
var method = obj.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
25622562
method?.Invoke(obj, new object[] { });
2563-
2564-
if (obj.IsSpawned)
2563+
foreach (var behaviour in obj.ChildNetworkBehaviours)
25652564
{
2566-
foreach (var behaviour in obj.ChildNetworkBehaviours)
2567-
{
2568-
var behaviourMethod = behaviour.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
2569-
behaviourMethod?.Invoke(behaviour, new object[] { });
2570-
}
2565+
var behaviourMethod = behaviour.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
2566+
behaviourMethod?.Invoke(behaviour, new object[] { });
25712567
}
25722568
}
25732569
}

0 commit comments

Comments
 (0)