fix: This fixes the issue with recent 2023 update where FindObjectsOfType was renamed to FindObjectsByType (#2320)

* update
Adding the 2023.1.a21 FindObjectsByType fix for NGO using the UNITY_2023_1_OR_NEWER define.
Modified a few of the FindObjectsSortMode for areas that looked like they might require their results ordered by InstanceID.
This commit is contained in:
Noel Stephens 2022-11-23 11:40:36 -06:00 коммит произвёл GitHub
Родитель d48c373a0c
Коммит 8ee79ab2a3
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
19 изменённых файлов: 166 добавлений и 9 удалений

Просмотреть файл

@ -65,7 +65,11 @@ namespace Unity.Netcode.Editor
var scenesList = EditorBuildSettings.scenes.ToList();
var activeScene = SceneManager.GetActiveScene();
var isSceneInBuildSettings = scenesList.Count((c) => c.path == activeScene.path) == 1;
#if UNITY_2023_1_OR_NEWER
var networkManager = Object.FindFirstObjectByType<NetworkManager>();
#else
var networkManager = Object.FindObjectOfType<NetworkManager>();
#endif
if (!isSceneInBuildSettings && networkManager != null)
{
if (networkManager.NetworkConfig != null && networkManager.NetworkConfig.EnableSceneManagement)

Просмотреть файл

@ -2018,7 +2018,11 @@ namespace Unity.Netcode
ScenePlacedObjects.Clear();
}
#if UNITY_2023_1_OR_NEWER
var networkObjects = UnityEngine.Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.InstanceID);
#else
var networkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>();
#endif
// Just add every NetworkObject found that isn't already in the list
// With additive scenes, we can have multiple in-scene placed NetworkObjects with the same GlobalObjectIdHash value

Просмотреть файл

@ -269,7 +269,12 @@ namespace Unity.Netcode
{
m_DespawnedInSceneObjectsSync.Clear();
// Find all active and non-active in-scene placed NetworkObjects
#if UNITY_2023_1_OR_NEWER
var inSceneNetworkObjects = UnityEngine.Object.FindObjectsByType<NetworkObject>(UnityEngine.FindObjectsInactive.Include, UnityEngine.FindObjectsSortMode.InstanceID).Where((c) => c.NetworkManager == m_NetworkManager);
#else
var inSceneNetworkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>(includeInactive: true).Where((c) => c.NetworkManager == m_NetworkManager);
#endif
foreach (var sobj in inSceneNetworkObjects)
{
if (sobj.IsSceneObject.HasValue && sobj.IsSceneObject.Value && !sobj.IsSpawned)
@ -657,7 +662,11 @@ namespace Unity.Netcode
if (networkObjectsToRemove.Length > 0)
{
#if UNITY_2023_1_OR_NEWER
var networkObjects = UnityEngine.Object.FindObjectsByType<NetworkObject>(UnityEngine.FindObjectsSortMode.InstanceID);
#else
var networkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>();
#endif
var networkObjectIdToNetworkObject = new Dictionary<ulong, NetworkObject>();
foreach (var networkObject in networkObjects)
{
@ -785,8 +794,14 @@ namespace Unity.Netcode
var objectRelativeScene = m_NetworkManager.SceneManager.ScenesLoaded[localSceneHandle];
// Find all active and non-active in-scene placed NetworkObjects
#if UNITY_2023_1_OR_NEWER
var inSceneNetworkObjects = UnityEngine.Object.FindObjectsByType<NetworkObject>(UnityEngine.FindObjectsInactive.Include, UnityEngine.FindObjectsSortMode.InstanceID).Where((c) =>
c.GetSceneOriginHandle() == localSceneHandle && (c.IsSceneObject != false)).ToList();
#else
var inSceneNetworkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>(includeInactive: true).Where((c) =>
c.GetSceneOriginHandle() == localSceneHandle && (c.IsSceneObject != false)).ToList();
#endif
foreach (var inSceneObject in inSceneNetworkObjects)
{

Просмотреть файл

@ -658,7 +658,11 @@ namespace Unity.Netcode
// Makes scene objects ready to be reused
internal void ServerResetShudownStateForSceneObjects()
{
#if UNITY_2023_1_OR_NEWER
var networkObjects = UnityEngine.Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.InstanceID).Where((c) => c.IsSceneObject != null && c.IsSceneObject == true);
#else
var networkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>().Where((c) => c.IsSceneObject != null && c.IsSceneObject == true);
#endif
foreach (var sobj in networkObjects)
{
sobj.IsSpawned = false;
@ -689,7 +693,11 @@ namespace Unity.Netcode
internal void DespawnAndDestroyNetworkObjects()
{
#if UNITY_2023_1_OR_NEWER
var networkObjects = UnityEngine.Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.InstanceID);
#else
var networkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>();
#endif
for (int i = 0; i < networkObjects.Length; i++)
{
@ -719,7 +727,11 @@ namespace Unity.Netcode
internal void DestroySceneObjects()
{
#if UNITY_2023_1_OR_NEWER
var networkObjects = UnityEngine.Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.InstanceID);
#else
var networkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>();
#endif
for (int i = 0; i < networkObjects.Length; i++)
{
@ -746,7 +758,11 @@ namespace Unity.Netcode
internal void ServerSpawnSceneObjectsOnStartSweep()
{
#if UNITY_2023_1_OR_NEWER
var networkObjects = UnityEngine.Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.InstanceID);
#else
var networkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>();
#endif
var networkObjectsToSpawn = new List<NetworkObject>();
for (int i = 0; i < networkObjects.Length; i++)

Просмотреть файл

@ -147,7 +147,12 @@ namespace Unity.Netcode.TestHelpers.Runtime
private static void ProcessInSceneObjects(Scene scene, NetworkManager networkManager)
{
// Get all in-scene placed NeworkObjects that were instantiated when this scene loaded
#if UNITY_2023_1_OR_NEWER
var inSceneNetworkObjects = Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.InstanceID).Where((c) => c.IsSceneObject != false && c.GetSceneOriginHandle() == scene.handle);
#else
var inSceneNetworkObjects = Object.FindObjectsOfType<NetworkObject>().Where((c) => c.IsSceneObject != false && c.GetSceneOriginHandle() == scene.handle);
#endif
foreach (var sobj in inSceneNetworkObjects)
{
if (sobj.NetworkManagerOwner != networkManager)

Просмотреть файл

@ -437,8 +437,13 @@ namespace Unity.Netcode.TestHelpers.Runtime
m_PlayerNetworkObjects.Add(networkManager.LocalClientId, new Dictionary<ulong, NetworkObject>());
}
#if UNITY_2023_1_OR_NEWER
// Get all player instances for the current client NetworkManager instance
var clientPlayerClones = Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.None).Where((c) => c.IsPlayerObject && c.OwnerClientId == networkManager.LocalClientId).ToList();
#else
// Get all player instances for the current client NetworkManager instance
var clientPlayerClones = Object.FindObjectsOfType<NetworkObject>().Where((c) => c.IsPlayerObject && c.OwnerClientId == networkManager.LocalClientId).ToList();
#endif
// Add this player instance to each client player entry
foreach (var playerNetworkObject in clientPlayerClones)
{
@ -452,9 +457,13 @@ namespace Unity.Netcode.TestHelpers.Runtime
m_PlayerNetworkObjects[playerNetworkObject.NetworkManager.LocalClientId].Add(networkManager.LocalClientId, playerNetworkObject);
}
}
#if UNITY_2023_1_OR_NEWER
// For late joining clients, add the remaining (if any) cloned versions of each client's player
clientPlayerClones = Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.None).Where((c) => c.IsPlayerObject && c.NetworkManager == networkManager).ToList();
#else
// For late joining clients, add the remaining (if any) cloned versions of each client's player
clientPlayerClones = Object.FindObjectsOfType<NetworkObject>().Where((c) => c.IsPlayerObject && c.NetworkManager == networkManager).ToList();
#endif
foreach (var playerNetworkObject in clientPlayerClones)
{
if (!m_PlayerNetworkObjects[networkManager.LocalClientId].ContainsKey(playerNetworkObject.OwnerClientId))
@ -474,7 +483,11 @@ namespace Unity.Netcode.TestHelpers.Runtime
}
if (m_UseHost)
{
#if UNITY_2023_1_OR_NEWER
var clientSideServerPlayerClones = Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.None).Where((c) => c.IsPlayerObject && c.OwnerClientId == m_ServerNetworkManager.LocalClientId);
#else
var clientSideServerPlayerClones = Object.FindObjectsOfType<NetworkObject>().Where((c) => c.IsPlayerObject && c.OwnerClientId == m_ServerNetworkManager.LocalClientId);
#endif
foreach (var playerNetworkObject in clientSideServerPlayerClones)
{
// When the server is not the host this needs to be done
@ -530,8 +543,13 @@ namespace Unity.Netcode.TestHelpers.Runtime
if (m_UseHost || m_ServerNetworkManager.IsHost)
{
#if UNITY_2023_1_OR_NEWER
// Add the server player instance to all m_ClientSidePlayerNetworkObjects entries
var serverPlayerClones = Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.None).Where((c) => c.IsPlayerObject && c.OwnerClientId == m_ServerNetworkManager.LocalClientId);
#else
// Add the server player instance to all m_ClientSidePlayerNetworkObjects entries
var serverPlayerClones = Object.FindObjectsOfType<NetworkObject>().Where((c) => c.IsPlayerObject && c.OwnerClientId == m_ServerNetworkManager.LocalClientId);
#endif
foreach (var playerNetworkObject in serverPlayerClones)
{
if (!m_PlayerNetworkObjects.ContainsKey(playerNetworkObject.NetworkManager.LocalClientId))
@ -721,7 +739,11 @@ namespace Unity.Netcode.TestHelpers.Runtime
/// </summary>
protected void DestroySceneNetworkObjects()
{
#if UNITY_2023_1_OR_NEWER
var networkObjects = Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.InstanceID);
#else
var networkObjects = Object.FindObjectsOfType<NetworkObject>();
#endif
foreach (var networkObject in networkObjects)
{
// This can sometimes be null depending upon order of operations

Просмотреть файл

@ -45,14 +45,18 @@ namespace Unity.Netcode.RuntimeTests
private EmptyComponent GetObjectForClient(ulong clientId)
{
foreach (var component in Object.FindObjectsOfType<EmptyComponent>())
#if UNITY_2023_1_OR_NEWER
var emptyComponents = Object.FindObjectsByType<EmptyComponent>(FindObjectsSortMode.InstanceID);
#else
var emptyComponents = Object.FindObjectsOfType<EmptyComponent>();
#endif
foreach (var component in emptyComponents)
{
if (component.IsSpawned && component.NetworkManager.LocalClientId == clientId)
{
return component;
}
}
return null;
}

Просмотреть файл

@ -271,14 +271,19 @@ namespace Unity.Netcode.RuntimeTests
private T GetComponentForClient<T>(ulong clientId) where T : NetworkBehaviour
{
foreach (var component in Object.FindObjectsOfType<T>())
#if UNITY_2023_1_OR_NEWER
var componentsToFind = Object.FindObjectsByType<T>(FindObjectsSortMode.InstanceID);
#else
var componentsToFind = Object.FindObjectsOfType<T>();
#endif
foreach (var component in componentsToFind)
{
if (component.IsSpawned && component.NetworkManager.LocalClientId == clientId)
{
return component;
}
}
return null;
}
@ -751,7 +756,13 @@ namespace Unity.Netcode.RuntimeTests
{
var found1 = false;
var found2 = false;
foreach (var component in Object.FindObjectsOfType<DeferredMessageTestRpcComponent>())
#if UNITY_2023_1_OR_NEWER
var deferredMessageTestRpcComponents = Object.FindObjectsByType<DeferredMessageTestRpcComponent>(FindObjectsSortMode.None);
#else
var deferredMessageTestRpcComponents = Object.FindObjectsOfType<DeferredMessageTestRpcComponent>();
#endif
foreach (var component in deferredMessageTestRpcComponents)
{
if (component.IsSpawned && component.NetworkManager.LocalClientId == client.LocalClientId)
{

Просмотреть файл

@ -29,10 +29,15 @@ namespace Unity.Netcode.RuntimeTests
{
// Check the condition for this test and automatically handle varying processing
// environments and conditions
#if UNITY_2023_1_OR_NEWER
yield return WaitForConditionOrTimeOut(() =>
Object.FindObjectsByType<NetworkVisibilityComponent>(FindObjectsSortMode.None).Where(
(c) => c.IsSpawned).Count() == 2);
#else
yield return WaitForConditionOrTimeOut(() =>
Object.FindObjectsOfType<NetworkVisibilityComponent>().Where(
(c) => c.IsSpawned).Count() == 2);
#endif
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Timed out waiting for instances " +
"to be detected!");
}
@ -64,9 +69,15 @@ namespace Unity.Netcode.RuntimeTests
{
// Check the condition for this test and automatically handle varying processing
// environments and conditions
#if UNITY_2023_1_OR_NEWER
yield return WaitForConditionOrTimeOut(() =>
Object.FindObjectsByType<NetworkVisibilityComponent>(FindObjectsSortMode.None).Where(
(c) => c.IsSpawned).Count() == 2);
#else
yield return WaitForConditionOrTimeOut(() =>
Object.FindObjectsOfType<NetworkVisibilityComponent>().Where(
(c) => c.IsSpawned).Count() == 2);
#endif
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Timed out waiting for instances " +
"to be detected!");

Просмотреть файл

@ -89,7 +89,12 @@ namespace Unity.Netcode.RuntimeTests
Assert.IsFalse(serverBehaviour.IsOwnedByServer);
Assert.AreEqual(m_ClientNetworkManagers[0].LocalClientId, serverBehaviour.OwnerClientId);
#if UNITY_2023_1_OR_NEWER
var clientObject = Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.InstanceID).Where((obj) => obj.NetworkManagerOwner == m_ClientNetworkManagers[0]).FirstOrDefault();
#else
var clientObject = Object.FindObjectsOfType<NetworkObject>().Where((obj) => obj.NetworkManagerOwner == m_ClientNetworkManagers[0]).FirstOrDefault();
#endif
Assert.IsNotNull(clientObject);
Assert.IsTrue(clientObject.IsOwner);

Просмотреть файл

@ -186,7 +186,12 @@ namespace Unity.Netcode.RuntimeTests
//Stop, shutdown, and destroy
NetworkManagerHelper.ShutdownNetworkManager();
#if UNITY_2023_1_OR_NEWER
var networkObjects = UnityEngine.Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.InstanceID).ToList();
#else
var networkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>().ToList();
#endif
var networkObjectsList = networkObjects.Where(c => c.name.Contains(k_PrefabObjectName));
foreach (var networkObject in networkObjectsList)
{

Просмотреть файл

@ -48,7 +48,12 @@ namespace Unity.Netcode.RuntimeTests
[UnityTest]
public IEnumerator HiddenObjectsTest()
{
#if UNITY_2023_1_OR_NEWER
yield return WaitForConditionOrTimeOut(() => Object.FindObjectsByType<NetworkVisibilityComponent>(FindObjectsSortMode.None).Where((c) => c.IsSpawned).Count() == 2);
#else
yield return WaitForConditionOrTimeOut(() => Object.FindObjectsOfType<NetworkVisibilityComponent>().Where((c) => c.IsSpawned).Count() == 2);
#endif
Assert.IsFalse(s_GlobalTimeoutHelper.TimedOut, "Timed out waiting for the visible object count to equal 2!");
}
}

Просмотреть файл

@ -10,7 +10,11 @@ public class NetworkManagerMonitor : MonoBehaviour
// Start is called before the first frame update
private void Start()
{
#if UNITY_2023_1_OR_NEWER
var networkManagerInstances = FindObjectsByType<NetworkManager>(FindObjectsSortMode.InstanceID);
#else
var networkManagerInstances = FindObjectsOfType<NetworkManager>();
#endif
foreach (var instance in networkManagerInstances)
{
if (instance.IsListening)

Просмотреть файл

@ -67,7 +67,12 @@ namespace TestProject.RuntimeTests
serverObj.GetComponent<NetworkObject>().NetworkManagerOwner = m_ServerNetworkManager;
serverObj.GetComponent<NetworkObject>().Spawn();
#if UNITY_2023_1_OR_NEWER
var objs = Object.FindObjectsByType<AddressableTestScript>(FindObjectsSortMode.InstanceID);
#else
var objs = Object.FindObjectsOfType<AddressableTestScript>();
#endif
// Prefabs loaded by addressables actually don't show up in this search.
// Unlike other tests that make prefabs programmatically, those aren't added to the scene until they're instantiated
Assert.AreEqual(1, objs.Length);
@ -80,7 +85,11 @@ namespace TestProject.RuntimeTests
{
// Since it's not added, after the CreateObjectMessage is received, it's not spawned yet
// Verify that to be the case as a precondition.
#if UNITY_2023_1_OR_NEWER
objs = Object.FindObjectsByType<AddressableTestScript>(FindObjectsSortMode.InstanceID);
#else
objs = Object.FindObjectsOfType<AddressableTestScript>();
#endif
Assert.AreEqual(1, objs.Length);
yield return new WaitUntil(() => Time.realtimeSinceStartup - startTime >= m_ClientNetworkManagers[0].NetworkConfig.SpawnTimeout - 0.25);
foreach (var client in m_ClientNetworkManagers)
@ -89,7 +98,11 @@ namespace TestProject.RuntimeTests
}
}
#if UNITY_2023_1_OR_NEWER
objs = Object.FindObjectsByType<AddressableTestScript>(FindObjectsSortMode.InstanceID);
#else
objs = Object.FindObjectsOfType<AddressableTestScript>();
#endif
Assert.AreEqual(NumberOfClients + 1, objs.Length);
foreach (var obj in objs)
{

Просмотреть файл

@ -79,7 +79,12 @@ namespace TestProject.RuntimeTests
m_ServerNetworkManager = null;
m_ClientNetworkManagers = null;
#if UNITY_2023_1_OR_NEWER
var networkObjects = Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.InstanceID);
#else
var networkObjects = Object.FindObjectsOfType<NetworkObject>();
#endif
foreach (var netObject in networkObjects)
{
Object.DestroyImmediate(netObject);

Просмотреть файл

@ -182,7 +182,13 @@ namespace Unity.Netcode.MultiprocessRuntimeTests
yield return new ExecuteStepInContext(StepExecutionContext.Clients, nbObjectsBytes =>
{
var nbObjectsParam = BitConverter.ToInt32(nbObjectsBytes, 0);
#if UNITY_2023_1_OR_NEWER
Assert.That(Object.FindObjectsByType<OneNetVar>(FindObjectsSortMode.None).Length, Is.EqualTo(nbObjectsParam + 1), "Wrong number of spawned objects client side"); // +1 for the prefab to spawn
#else
Assert.That(Object.FindObjectsOfType(typeof(OneNetVar)).Length, Is.EqualTo(nbObjectsParam + 1), "Wrong number of spawned objects client side"); // +1 for the prefab to spawn
#endif
}, paramToPass: BitConverter.GetBytes(nbObjects));
yield return new ExecuteStepInContext(StepExecutionContext.Server, bytes =>
{

Просмотреть файл

@ -53,8 +53,11 @@ namespace TestProject.RuntimeTests
{
return false;
}
#if UNITY_2023_1_OR_NEWER
m_NetworkObjectTestComponent = Object.FindFirstObjectByType<NetworkObjectTestComponent>();
#else
m_NetworkObjectTestComponent = Object.FindObjectOfType<NetworkObjectTestComponent>();
#endif
if (m_NetworkObjectTestComponent == null)
{
return false;

Просмотреть файл

@ -88,7 +88,13 @@ namespace TestProject.RuntimeTests
const int expectedNetworkObjects = numClients + 2; // +2 = one for prefab, one for server.
const int maxFrames = 240;
var doubleCheckTime = Time.realtimeSinceStartup + 5.0f;
while (Object.FindObjectsOfType<NetworkObject>().Length != expectedNetworkObjects)
#if UNITY_2023_1_OR_NEWER
var networkObjects = Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.InstanceID);
#else
var networkObjects = Object.FindObjectsOfType<NetworkObject>();
#endif
while (networkObjects.Length != expectedNetworkObjects)
{
if (Time.frameCount > maxFrames)
{
@ -102,6 +108,11 @@ namespace TestProject.RuntimeTests
}
var nextFrameNumber = Time.frameCount + 1;
yield return new WaitUntil(() => Time.frameCount >= nextFrameNumber);
#if UNITY_2023_1_OR_NEWER
networkObjects = Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.InstanceID);
#else
networkObjects = Object.FindObjectsOfType<NetworkObject>();
#endif
}
serverObject.GetComponent<NetworkVariableInitOnNetworkSpawn>().Variable.Value = NetworkVariableInitOnNetworkSpawn.ExpectedSpawnValueOnClient;

Просмотреть файл

@ -49,7 +49,11 @@ namespace TestProject.RuntimeTests
yield return NetcodeIntegrationTest.WaitForConditionOrTimeOut(() => m_TestScene.IsValid() && m_TestScene.isLoaded, timeoutHelper);
Assert.False(timeoutHelper.TimedOut, "Timed out waiting for scene to load!");
#if UNITY_2023_1_OR_NEWER
var loadedInSceneObject = Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.InstanceID).Where((c) => c.name == k_SceneObjectName).FirstOrDefault();
#else
var loadedInSceneObject = Object.FindObjectsOfType<NetworkObject>().Where((c) => c.name == k_SceneObjectName).FirstOrDefault();
#endif
Assert.IsNotNull(loadedInSceneObject, $"Failed to find {k_SceneObjectName} before starting client!");
@ -66,7 +70,11 @@ namespace TestProject.RuntimeTests
yield return m_DefaultWaitForTick;
// Find the same object
#if UNITY_2023_1_OR_NEWER
loadedInSceneObject = Object.FindObjectsByType<NetworkObject>(FindObjectsSortMode.InstanceID).Where((c) => c.name == k_SceneObjectName).FirstOrDefault();
#else
loadedInSceneObject = Object.FindObjectsOfType<NetworkObject>().Where((c) => c.name == k_SceneObjectName).FirstOrDefault();
#endif
// Verify it still exists
Assert.IsNotNull(loadedInSceneObject, $"Failed to find {k_SceneObjectName} after starting client!");