Skip to content

Commit 834bcdf

Browse files
chore: update usage of deprecated FindObjectsByType<T>(FindObjectsSortMode) and enum FindObjectSortMode (#3857)
* update Funneling all find object related calls into a single static method in order to simplify the current, and any future, updates/changes to FindObjectsByType. * update Making FindObjects internal. * update Adjusted to explicit handling of the update within test project script to avoid having to make FindObjects public. * update Adding [MethodImpl(MethodImplOptions.AggressiveInlining)] attribute to the T[] FindObjectsByType<T>() method. * style Removing unused using directives. Removing CR. * Update com.unity.netcode.gameobjects/Runtime/Core/FindObjects.cs Co-authored-by: Emma <emma.mcmillan@unity3d.com> * update Adding same condition define, NGO_FINDOBJECTS_NOSORTING, to the testproject.manualtests assembly define. * 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. * update Adding sort by identifier to two locations that require sorting by identifier. * style Added missing whitespace * update increasing the 6000.5 version from 5.0a6 to 5.0a7 as it appears trunk is currently 5.0a6 (which doesn't have the deprecation merged into it yet). * update updating minimum for deprecated fix to 6000.5.0a8. * update This "should" fix the Rust server issue... I think? (Tested local instance on 6000.5.0a7 and it passes everything) * update adding change log entry * update Re-targeting 6000.5.0a7 as the version of unity with the FindObjectsByType changes. * update Moving changelog entry into the unreleased section. --------- Co-authored-by: Emma <emma.mcmillan@unity3d.com>
1 parent 1b658bd commit 834bcdf

27 files changed

+151
-195
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1414

1515
### Changed
1616

17+
- Updating usage of deprecated `FindObjectsByType(FindObjectsSortMode)` and enum `FindObjectSortMode` in 6000.4 and 6000.5. (#3857)
1718

1819
### Deprecated
1920

com.unity.netcode.gameobjects/Editor/NetworkManagerHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private static void ScenesInBuildActiveSceneCheck()
9292
var activeScene = SceneManager.GetActiveScene();
9393
var isSceneInBuildSettings = scenesList.Count((c) => c.path == activeScene.path) == 1;
9494
#if UNITY_2023_1_OR_NEWER
95-
var networkManager = Object.FindFirstObjectByType<NetworkManager>();
95+
var networkManager = Object.FindAnyObjectByType<NetworkManager>();
9696
#else
9797
var networkManager = Object.FindObjectOfType<NetworkManager>();
9898
#endif
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#if NGO_FINDOBJECTS_NOSORTING
2+
using System;
3+
#endif
4+
using System.Runtime.CompilerServices;
5+
using Object = UnityEngine.Object;
6+
7+
namespace Unity.Netcode
8+
{
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>
16+
internal static class FindObjects
17+
{
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>
25+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
26+
public static T[] ByType<T>(bool includeInactive = false, bool orderByIdentifier = false) where T : Object
27+
{
28+
var inactive = includeInactive ? UnityEngine.FindObjectsInactive.Include : UnityEngine.FindObjectsInactive.Exclude;
29+
#if NGO_FINDOBJECTS_NOSORTING
30+
var results = Object.FindObjectsByType<T>(inactive);
31+
if (orderByIdentifier)
32+
{
33+
Array.Sort(results, (a, b) => a.GetEntityId().CompareTo(b.GetEntityId()));
34+
}
35+
#else
36+
var results = Object.FindObjectsByType<T>(inactive, orderByIdentifier ? UnityEngine.FindObjectsSortMode.InstanceID : UnityEngine.FindObjectsSortMode.None);
37+
#endif
38+
return results;
39+
}
40+
}
41+
}

com.unity.netcode.gameobjects/Runtime/Core/FindObjects.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2729,12 +2729,7 @@ internal void PopulateScenePlacedObjects(Scene sceneToFilterBy, bool clearSceneP
27292729
{
27302730
ScenePlacedObjects.Clear();
27312731
}
2732-
2733-
#if UNITY_2023_1_OR_NEWER
2734-
var networkObjects = UnityEngine.Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.InstanceID);
2735-
#else
2736-
var networkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>();
2737-
#endif
2732+
var networkObjects = FindObjects.ByType<NetworkObject>();
27382733

27392734
// Just add every NetworkObject found that isn't already in the list
27402735
// 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 & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,7 @@ internal void AddDespawnedInSceneNetworkObjects()
368368
{
369369
m_DespawnedInSceneObjectsSync.Clear();
370370
// Find all active and non-active in-scene placed NetworkObjects
371-
#if UNITY_2023_1_OR_NEWER
372-
var inSceneNetworkObjects = UnityEngine.Object.FindObjectsByType<NetworkObject>(UnityEngine.FindObjectsInactive.Include, UnityEngine.FindObjectsSortMode.InstanceID).Where((c) => c.NetworkManager == m_NetworkManager);
373-
#else
374-
var inSceneNetworkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>(includeInactive: true).Where((c) => c.NetworkManager == m_NetworkManager);
375-
376-
#endif
371+
var inSceneNetworkObjects = FindObjects.ByType<NetworkObject>(true, true).Where((c) => c.NetworkManager == m_NetworkManager);
377372
foreach (var sobj in inSceneNetworkObjects)
378373
{
379374
if (sobj.IsSceneObject.HasValue && sobj.IsSceneObject.Value && !sobj.IsSpawned)
@@ -917,11 +912,7 @@ internal void ReadClientReSynchronizationData(FastBufferReader reader)
917912

918913
if (networkObjectsToRemove.Length > 0)
919914
{
920-
#if UNITY_2023_1_OR_NEWER
921-
var networkObjects = UnityEngine.Object.FindObjectsByType<NetworkObject>(UnityEngine.FindObjectsSortMode.InstanceID);
922-
#else
923-
var networkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>();
924-
#endif
915+
var networkObjects = FindObjects.ByType<NetworkObject>(orderByIdentifier: true);
925916
var networkObjectIdToNetworkObject = new Dictionary<ulong, NetworkObject>();
926917
foreach (var networkObject in networkObjects)
927918
{
@@ -1049,14 +1040,8 @@ private void DeserializeDespawnedInScenePlacedNetworkObjects()
10491040
var objectRelativeScene = m_NetworkManager.SceneManager.ScenesLoaded[localSceneHandle];
10501041

10511042
// Find all active and non-active in-scene placed NetworkObjects
1052-
#if UNITY_2023_1_OR_NEWER
1053-
var inSceneNetworkObjects = UnityEngine.Object.FindObjectsByType<NetworkObject>(UnityEngine.FindObjectsInactive.Include, UnityEngine.FindObjectsSortMode.InstanceID).Where((c) =>
1054-
c.GetSceneOriginHandle() == localSceneHandle && (c.IsSceneObject != false)).ToList();
1055-
#else
1056-
var inSceneNetworkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>(includeInactive: true).Where((c) =>
1043+
var inSceneNetworkObjects = FindObjects.ByType<NetworkObject>(true, true).Where((c) =>
10571044
c.GetSceneOriginHandle() == localSceneHandle && (c.IsSceneObject != false)).ToList();
1058-
#endif
1059-
10601045

10611046
foreach (var inSceneObject in inSceneNetworkObjects)
10621047
{

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

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,11 +1369,7 @@ internal void DespawnObject(NetworkObject networkObject, bool destroyObject = fa
13691369
// Makes scene objects ready to be reused
13701370
internal void ServerResetShudownStateForSceneObjects()
13711371
{
1372-
#if UNITY_2023_1_OR_NEWER
1373-
var networkObjects = UnityEngine.Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.InstanceID).Where((c) => c.IsSceneObject != null && c.IsSceneObject == true);
1374-
#else
1375-
var networkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>().Where((c) => c.IsSceneObject != null && c.IsSceneObject == true);
1376-
#endif
1372+
var networkObjects = FindObjects.ByType<NetworkObject>(orderByIdentifier: true).Where((c) => c.IsSceneObject != null && c.IsSceneObject == true);
13771373
foreach (var sobj in networkObjects)
13781374
{
13791375
sobj.IsSpawned = false;
@@ -1404,11 +1400,7 @@ internal void ServerDestroySpawnedSceneObjects()
14041400

14051401
internal void DespawnAndDestroyNetworkObjects()
14061402
{
1407-
#if UNITY_2023_1_OR_NEWER
1408-
var networkObjects = UnityEngine.Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.InstanceID);
1409-
#else
1410-
var networkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>();
1411-
#endif
1403+
var networkObjects = FindObjects.ByType<NetworkObject>(orderByIdentifier: true);
14121404

14131405
foreach (var networkObject in networkObjects)
14141406
{
@@ -1463,11 +1455,7 @@ internal void DespawnAndDestroyNetworkObjects()
14631455

14641456
internal void DestroySceneObjects()
14651457
{
1466-
#if UNITY_2023_1_OR_NEWER
1467-
var networkObjects = UnityEngine.Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.InstanceID);
1468-
#else
1469-
var networkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>();
1470-
#endif
1458+
var networkObjects = FindObjects.ByType<NetworkObject>(orderByIdentifier: true);
14711459

14721460
for (int i = 0; i < networkObjects.Length; i++)
14731461
{
@@ -1498,11 +1486,7 @@ internal void DestroySceneObjects()
14981486

14991487
internal void ServerSpawnSceneObjectsOnStartSweep()
15001488
{
1501-
#if UNITY_2023_1_OR_NEWER
1502-
var networkObjects = UnityEngine.Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.InstanceID);
1503-
#else
1504-
var networkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>();
1505-
#endif
1489+
var networkObjects = FindObjects.ByType<NetworkObject>(orderByIdentifier: true);
15061490
var networkObjectsToSpawn = new List<NetworkObject>();
15071491
for (int i = 0; i < networkObjects.Length; i++)
15081492
{

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@
8787
"name": "Unity",
8888
"expression": "6000.5.0a1",
8989
"define": "SCENE_MANAGEMENT_SCENE_HANDLE_MUST_USE_ULONG"
90+
},
91+
{
92+
"name": "Unity",
93+
"expression": "[6000.4.0b5,6000.5.0a1)",
94+
"define": "NGO_FINDOBJECTS_NOSORTING"
95+
},
96+
{
97+
"name": "Unity",
98+
"expression": "6000.5.0a7",
99+
"define": "NGO_FINDOBJECTS_NOSORTING"
90100
}
91101
],
92102
"noEngineReferences": false

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

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

279279
private T GetComponentForClient<T>(ulong clientId) where T : NetworkBehaviour
280280
{
281-
#if UNITY_2023_1_OR_NEWER
282-
var componentsToFind = Object.FindObjectsByType<T>(FindObjectsSortMode.InstanceID);
283-
#else
284-
var componentsToFind = Object.FindObjectsOfType<T>();
285-
#endif
286-
281+
var componentsToFind = FindObjects.ByType<T>();
287282
foreach (var component in componentsToFind)
288283
{
289284
if (component.IsSpawned && component.NetworkManager.LocalClientId == clientId)
@@ -761,11 +756,7 @@ bool HaveAllClientsSpawned()
761756
{
762757
var found1 = false;
763758
var found2 = false;
764-
#if UNITY_2023_1_OR_NEWER
765-
var deferredMessageTestRpcComponents = Object.FindObjectsByType<DeferredMessageTestRpcComponent>(FindObjectsSortMode.None);
766-
#else
767-
var deferredMessageTestRpcComponents = Object.FindObjectsOfType<DeferredMessageTestRpcComponent>();
768-
#endif
759+
var deferredMessageTestRpcComponents = FindObjects.ByType<DeferredMessageTestRpcComponent>();
769760

770761
foreach (var component in deferredMessageTestRpcComponents)
771762
{

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

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,7 @@ public IEnumerator MyFirstIntegationTest()
2929
{
3030
// Check the condition for this test and automatically handle varying processing
3131
// environments and conditions
32-
#if UNITY_2023_1_OR_NEWER
33-
yield return WaitForConditionOrTimeOut(() =>
34-
Object.FindObjectsByType<NetworkVisibilityComponent>(FindObjectsSortMode.None).Where(
35-
(c) => c.IsSpawned).Count() == 2);
36-
#else
37-
yield return WaitForConditionOrTimeOut(() =>
38-
Object.FindObjectsOfType<NetworkVisibilityComponent>().Where(
39-
(c) => c.IsSpawned).Count() == 2);
40-
#endif
32+
yield return WaitForConditionOrTimeOut(() => FindObjects.ByType<NetworkVisibilityComponent>().Where((c) => c.IsSpawned).Count() == 2);
4133
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Timed out waiting for instances " +
4234
"to be detected!");
4335
}
@@ -69,16 +61,7 @@ public IEnumerator MyFirstIntegationTest()
6961
{
7062
// Check the condition for this test and automatically handle varying processing
7163
// environments and conditions
72-
#if UNITY_2023_1_OR_NEWER
73-
yield return WaitForConditionOrTimeOut(() =>
74-
Object.FindObjectsByType<NetworkVisibilityComponent>(FindObjectsSortMode.None).Where(
75-
(c) => c.IsSpawned).Count() == 2);
76-
#else
77-
yield return WaitForConditionOrTimeOut(() =>
78-
Object.FindObjectsOfType<NetworkVisibilityComponent>().Where(
79-
(c) => c.IsSpawned).Count() == 2);
80-
#endif
81-
64+
yield return WaitForConditionOrTimeOut(() => FindObjects.ByType<NetworkVisibilityComponent>().Where((c) => c.IsSpawned).Count() == 2);
8265
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Timed out waiting for instances " +
8366
"to be detected!");
8467
}

0 commit comments

Comments
 (0)