Skip to content

Commit a5983f7

Browse files
update
Added more precise Unity engine versions to asmdef files to assure earlier versions of 6000.4 and 6000.5 still use the previous API. Renamed FindObjects.FindObjectsByType to FindObjects.ByType Updated FindObject.ByType to be able to sort by identifier and include inactive objects.
1 parent 9a5d04b commit a5983f7

20 files changed

+85
-56
lines changed
Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,41 @@
1+
#if NGO_FINDOBJECTS_NOSORTING
2+
using System;
3+
#endif
14
using System.Runtime.CompilerServices;
2-
using UnityEngine;
5+
using Object = UnityEngine.Object;
36

47
namespace Unity.Netcode
58
{
9+
/// <summary>
10+
/// Helper class to handle the variations of FindObjectsByType.
11+
/// </summary>
12+
/// <remarks>
13+
/// It is intentional that we do not include the UnityEngine namespace in order to avoid
14+
/// over-complicatd define wrapping between versions that do or don't support FindObjectsSortMode.
15+
/// </remarks>
616
internal static class FindObjects
717
{
18+
/// <summary>
19+
/// Replaces <see cref="Object.FindObjectsByType"/> to have one place where these changes are applied.
20+
/// </summary>
21+
/// <typeparam name="T"></typeparam>
22+
/// <param name="includeInactive">When true, inactive objects will be included.</param>
23+
/// <param name="orderByIdentifier">When true, the array returned will be sorted by identifier.</param>
24+
/// <returns>Resulst as an <see cref="Array"/> of type T</returns>
825
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9-
public static T[] FindObjectsByType<T>() where T : Object
26+
public static T[] ByType<T>(bool includeInactive = false, bool orderByIdentifier = false) where T : Object
1027
{
28+
var inactive = includeInactive ? UnityEngine.FindObjectsInactive.Include : UnityEngine.FindObjectsInactive.Exclude;
1129
#if NGO_FINDOBJECTS_NOSORTING
12-
var results = Object.FindObjectsByType<T>();
13-
#elif UNITY_2023_1_OR_NEWER
14-
var results = Object.FindObjectsByType<T>(FindObjectsSortMode.None);
30+
var results = Object.FindObjectsByType<T>(inactive);
31+
if (orderByIdentifier)
32+
{
33+
Array.Sort(results, (a, b) => a.GetEntityId().CompareTo(b.GetEntityId()));
34+
}
1535
#else
16-
var results = Object.FindObjectsOfType<T>();
36+
var results = Object.FindObjectsByType<T>(inactive, orderByIdentifier ? UnityEngine.FindObjectsSortMode.InstanceID : UnityEngine.FindObjectsSortMode.None);
1737
#endif
1838
return results;
1939
}
20-
2140
}
2241
}

com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2729,7 +2729,7 @@ internal void PopulateScenePlacedObjects(Scene sceneToFilterBy, bool clearSceneP
27292729
{
27302730
ScenePlacedObjects.Clear();
27312731
}
2732-
var networkObjects = FindObjects.FindObjectsByType<NetworkObject>();
2732+
var networkObjects = FindObjects.ByType<NetworkObject>();
27332733

27342734
// Just add every NetworkObject found that isn't already in the list
27352735
// With additive scenes, we can have multiple in-scene placed NetworkObjects with the same GlobalObjectIdHash value

com.unity.netcode.gameobjects/Runtime/SceneManagement/SceneEventData.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ internal void AddDespawnedInSceneNetworkObjects()
368368
{
369369
m_DespawnedInSceneObjectsSync.Clear();
370370
// Find all active and non-active in-scene placed NetworkObjects
371-
var inSceneNetworkObjects = FindObjects.FindObjectsByType<NetworkObject>().Where((c) => c.NetworkManager == m_NetworkManager);
371+
var inSceneNetworkObjects = FindObjects.ByType<NetworkObject>(true, true).Where((c) => c.NetworkManager == m_NetworkManager);
372372
foreach (var sobj in inSceneNetworkObjects)
373373
{
374374
if (sobj.IsSceneObject.HasValue && sobj.IsSceneObject.Value && !sobj.IsSpawned)
@@ -912,7 +912,7 @@ internal void ReadClientReSynchronizationData(FastBufferReader reader)
912912

913913
if (networkObjectsToRemove.Length > 0)
914914
{
915-
var networkObjects = FindObjects.FindObjectsByType<NetworkObject>();
915+
var networkObjects = FindObjects.ByType<NetworkObject>();
916916
var networkObjectIdToNetworkObject = new Dictionary<ulong, NetworkObject>();
917917
foreach (var networkObject in networkObjects)
918918
{
@@ -1040,7 +1040,7 @@ private void DeserializeDespawnedInScenePlacedNetworkObjects()
10401040
var objectRelativeScene = m_NetworkManager.SceneManager.ScenesLoaded[localSceneHandle];
10411041

10421042
// Find all active and non-active in-scene placed NetworkObjects
1043-
var inSceneNetworkObjects = FindObjects.FindObjectsByType<NetworkObject>().Where((c) =>
1043+
var inSceneNetworkObjects = FindObjects.ByType<NetworkObject>(true, true).Where((c) =>
10441044
c.GetSceneOriginHandle() == localSceneHandle && (c.IsSceneObject != false)).ToList();
10451045

10461046
foreach (var inSceneObject in inSceneNetworkObjects)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,7 @@ internal void DespawnObject(NetworkObject networkObject, bool destroyObject = fa
13691369
// Makes scene objects ready to be reused
13701370
internal void ServerResetShudownStateForSceneObjects()
13711371
{
1372-
var networkObjects = FindObjects.FindObjectsByType<NetworkObject>().Where((c) => c.IsSceneObject != null && c.IsSceneObject == true);
1372+
var networkObjects = FindObjects.ByType<NetworkObject>().Where((c) => c.IsSceneObject != null && c.IsSceneObject == true);
13731373
foreach (var sobj in networkObjects)
13741374
{
13751375
sobj.IsSpawned = false;
@@ -1400,7 +1400,7 @@ internal void ServerDestroySpawnedSceneObjects()
14001400

14011401
internal void DespawnAndDestroyNetworkObjects()
14021402
{
1403-
var networkObjects = FindObjects.FindObjectsByType<NetworkObject>();
1403+
var networkObjects = FindObjects.ByType<NetworkObject>();
14041404

14051405
for (int i = 0; i < networkObjects.Length; i++)
14061406
{
@@ -1448,7 +1448,7 @@ internal void DespawnAndDestroyNetworkObjects()
14481448

14491449
internal void DestroySceneObjects()
14501450
{
1451-
var networkObjects = FindObjects.FindObjectsByType<NetworkObject>();
1451+
var networkObjects = FindObjects.ByType<NetworkObject>();
14521452

14531453
for (int i = 0; i < networkObjects.Length; i++)
14541454
{
@@ -1479,7 +1479,7 @@ internal void DestroySceneObjects()
14791479

14801480
internal void ServerSpawnSceneObjectsOnStartSweep()
14811481
{
1482-
var networkObjects = FindObjects.FindObjectsByType<NetworkObject>();
1482+
var networkObjects = FindObjects.ByType<NetworkObject>();
14831483
var networkObjectsToSpawn = new List<NetworkObject>();
14841484
for (int i = 0; i < networkObjects.Length; i++)
14851485
{

com.unity.netcode.gameobjects/Runtime/Unity.Netcode.Runtime.asmdef

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,12 @@
9090
},
9191
{
9292
"name": "Unity",
93-
"expression": "6000.4.0b5",
93+
"expression": "[6000.4.0b5,6000.5.0a1)",
94+
"define": "NGO_FINDOBJECTS_NOSORTING"
95+
},
96+
{
97+
"name": "Unity",
98+
"expression": "6000.5.0a6",
9499
"define": "NGO_FINDOBJECTS_NOSORTING"
95100
}
96101
],

com.unity.netcode.gameobjects/Tests/Runtime/DeferredMessagingTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ private void SpawnClients(bool clearTestDeferredMessageManagerCallFlags = true)
278278

279279
private T GetComponentForClient<T>(ulong clientId) where T : NetworkBehaviour
280280
{
281-
var componentsToFind = FindObjects.FindObjectsByType<T>();
281+
var componentsToFind = FindObjects.ByType<T>();
282282
foreach (var component in componentsToFind)
283283
{
284284
if (component.IsSpawned && component.NetworkManager.LocalClientId == clientId)
@@ -756,7 +756,7 @@ bool HaveAllClientsSpawned()
756756
{
757757
var found1 = false;
758758
var found2 = false;
759-
var deferredMessageTestRpcComponents = FindObjects.FindObjectsByType<DeferredMessageTestRpcComponent>();
759+
var deferredMessageTestRpcComponents = FindObjects.ByType<DeferredMessageTestRpcComponent>();
760760

761761
foreach (var component in deferredMessageTestRpcComponents)
762762
{

com.unity.netcode.gameobjects/Tests/Runtime/IntegrationTestExamples.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public IEnumerator MyFirstIntegationTest()
2929
{
3030
// Check the condition for this test and automatically handle varying processing
3131
// environments and conditions
32-
yield return WaitForConditionOrTimeOut(() => FindObjects.FindObjectsByType<NetworkVisibilityComponent>().Where((c) => c.IsSpawned).Count() == 2);
32+
yield return WaitForConditionOrTimeOut(() => FindObjects.ByType<NetworkVisibilityComponent>().Where((c) => c.IsSpawned).Count() == 2);
3333
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Timed out waiting for instances " +
3434
"to be detected!");
3535
}
@@ -61,7 +61,7 @@ public IEnumerator MyFirstIntegationTest()
6161
{
6262
// Check the condition for this test and automatically handle varying processing
6363
// environments and conditions
64-
yield return WaitForConditionOrTimeOut(() => FindObjects.FindObjectsByType<NetworkVisibilityComponent>().Where((c) => c.IsSpawned).Count() == 2);
64+
yield return WaitForConditionOrTimeOut(() => FindObjects.ByType<NetworkVisibilityComponent>().Where((c) => c.IsSpawned).Count() == 2);
6565
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Timed out waiting for instances " +
6666
"to be detected!");
6767
}

com.unity.netcode.gameobjects/Tests/Runtime/NetworkObject/NetworkObjectNetworkClientOwnedObjectsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public IEnumerator WhenOwnershipIsChanged_OwnershipValuesUpdateCorrectly()
7575
Assert.IsFalse(serverBehaviour.IsOwnedByServer);
7676
Assert.AreEqual(m_ClientNetworkManagers[0].LocalClientId, serverBehaviour.OwnerClientId);
7777

78-
var clientObject = FindObjects.FindObjectsByType<NetworkObject>().Where((obj) => obj.NetworkManagerOwner == m_ClientNetworkManagers[0]).FirstOrDefault();
78+
var clientObject = FindObjects.ByType<NetworkObject>().Where((obj) => obj.NetworkManagerOwner == m_ClientNetworkManagers[0]).FirstOrDefault();
7979

8080
Assert.IsNotNull(clientObject);
8181
Assert.IsTrue(clientObject.IsOwner);

com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransformAnticipationTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public AnticipatedNetworkTransform GetTestComponent()
8989

9090
public AnticipatedNetworkTransform GetServerComponent()
9191
{
92-
var anticipatedNetworkTransforms = FindObjects.FindObjectsByType<AnticipatedNetworkTransform>();
92+
var anticipatedNetworkTransforms = FindObjects.ByType<AnticipatedNetworkTransform>();
9393
foreach (var obj in anticipatedNetworkTransforms)
9494
{
9595
if (obj.NetworkManager == m_ServerNetworkManager && obj.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId)
@@ -103,7 +103,7 @@ public AnticipatedNetworkTransform GetServerComponent()
103103

104104
public AnticipatedNetworkTransform GetOtherClientComponent()
105105
{
106-
var anticipatedNetworkTransforms = FindObjects.FindObjectsByType<AnticipatedNetworkTransform>();
106+
var anticipatedNetworkTransforms = FindObjects.ByType<AnticipatedNetworkTransform>();
107107
foreach (var obj in anticipatedNetworkTransforms)
108108
{
109109
if (obj.NetworkManager == m_ClientNetworkManagers[1] && obj.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId)

com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariable/NetworkVariableAnticipationTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public NetworkVariableAnticipationComponent GetTestComponent()
7878

7979
public NetworkVariableAnticipationComponent GetServerComponent()
8080
{
81-
var objects = FindObjects.FindObjectsByType<NetworkVariableAnticipationComponent>();
81+
var objects = FindObjects.ByType<NetworkVariableAnticipationComponent>();
8282
foreach (var obj in objects)
8383
{
8484
if (obj.NetworkManager == m_ServerNetworkManager && obj.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId)
@@ -92,7 +92,7 @@ public NetworkVariableAnticipationComponent GetServerComponent()
9292

9393
public NetworkVariableAnticipationComponent GetOtherClientComponent()
9494
{
95-
var objects = FindObjects.FindObjectsByType<NetworkVariableAnticipationComponent>();
95+
var objects = FindObjects.ByType<NetworkVariableAnticipationComponent>();
9696
foreach (var obj in objects)
9797
{
9898
if (obj.NetworkManager == m_ClientNetworkManagers[1] && obj.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId)

0 commit comments

Comments
 (0)