|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using UnityEngine; |
| 5 | +using System; |
| 6 | + |
| 7 | +namespace Microsoft.MixedReality.Toolkit.Utilities |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// A static utility used to avoid deprecated Find Object functions in favor of replacements introduced in Unity >= 2021.3.18. |
| 11 | + /// </summary> |
| 12 | + public static class FindObjectUtility |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Returns the first object matching the specified type. |
| 16 | + /// </summary> |
| 17 | + /// <remarks> |
| 18 | + /// If Unity >= 2021.3.18, calls FindFirstObjectByType. Otherwise calls FindObjectOfType. |
| 19 | + /// </remarks> |
| 20 | + /// <param name="includeInactive">If true, inactive objects will be included in the search. False by default.</param> |
| 21 | + public static T FindObjectByType<T>(bool includeInactive = false) where T : Component |
| 22 | + { |
| 23 | +#if UNITY_2021_3_18_OR_NEWER |
| 24 | + return UnityEngine.Object.FindFirstObjectByType<T>(includeInactive ? FindObjectsInactive.Include : FindObjectsInactive.Exclude); |
| 25 | +#else |
| 26 | + return UnityEngine.Object.FindObjectOfType<T>(includeInactive); |
| 27 | +#endif |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Returns all objects matching the specified type. |
| 32 | + /// </summary> |
| 33 | + /// <remarks> |
| 34 | + /// If Unity >= 2021.3.18, calls FindObjectsByType. Otherwise calls FindObjectsOfType. |
| 35 | + /// </remarks> |
| 36 | + /// <param name="includeInactive">If true, inactive objects will be included in the search. False by default.</param> |
| 37 | + /// <param name="sort">If false, results will not sorted by InstanceID. True by default.</param> |
| 38 | + public static T[] FindObjectsByType<T>(bool includeInactive = false, bool sort = true) where T : Component |
| 39 | + { |
| 40 | +#if UNITY_2021_3_18_OR_NEWER |
| 41 | + return UnityEngine.Object.FindObjectsByType<T>(includeInactive ? FindObjectsInactive.Include : FindObjectsInactive.Exclude, sort ? FindObjectsSortMode.InstanceID : FindObjectsSortMode.None); |
| 42 | +#else |
| 43 | + return UnityEngine.Object.FindObjectsOfType<T>(includeInactive); |
| 44 | +#endif |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Returns all objects matching the specified type. |
| 49 | + /// </summary> |
| 50 | + /// <remarks> |
| 51 | + /// If Unity >= 2021.3.18, calls FindObjectsByType. Otherwise calls FindObjectsOfType. |
| 52 | + /// </remarks> |
| 53 | + /// <param name="includeInactive">If true, inactive objects will be included in the search. False by default.</param> |
| 54 | + /// <param name="sort">If false, results will not sorted by InstanceID. True by default.</param> |
| 55 | + /// <param name="type">The type to search for.</param> |
| 56 | + public static UnityEngine.Object[] FindObjectsByType(Type type, bool includeInactive = false, bool sort = true) |
| 57 | + { |
| 58 | +#if UNITY_2021_3_18_OR_NEWER |
| 59 | + return UnityEngine.Object.FindObjectsByType(type, includeInactive ? FindObjectsInactive.Include : FindObjectsInactive.Exclude, sort ? FindObjectsSortMode.InstanceID : FindObjectsSortMode.None); |
| 60 | +#else |
| 61 | + return UnityEngine.Object.FindObjectsOfType(type, includeInactive); |
| 62 | +#endif |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments