Skip to content

Commit e854216

Browse files
committed
fix: support Unity 2022.2 object lookup compatibility
1 parent 2a6be79 commit e854216

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

MCPForUnity/Runtime/Helpers/UnityFindObjectsCompat.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
namespace MCPForUnity.Runtime.Helpers
55
{
66
/// <summary>
7-
/// Version-compatible wrappers for the Object.FindObjectsOfType / FindObjectsByType family,
8-
/// which changed across Unity 2022 → 6.0 → 6.5:
9-
/// Pre-2022.2 : FindObjectsOfType / FindObjectOfType
10-
/// 2022.2–6.4 : FindObjectsByType(sortMode) / FindFirstObjectByType
11-
/// 6.5+ : FindObjectsByType() (no sort param) / FindAnyObjectByType
7+
/// Version-compatible wrappers for the Object.FindObjectsOfType / FindObjectsByType family.
8+
/// Unity 2022.2 editor versions do not reliably expose the newer APIs, so we only
9+
/// switch to them once 2022.3+ symbols are available.
1210
/// </summary>
1311
public static class UnityFindObjectsCompat
1412
{
@@ -17,10 +15,12 @@ public static T[] FindAll<T>() where T : UObject
1715
{
1816
#if UNITY_6000_5_OR_NEWER
1917
return UObject.FindObjectsByType<T>();
20-
#elif UNITY_2022_2_OR_NEWER
18+
#elif UNITY_2022_3_OR_NEWER
2119
return UObject.FindObjectsByType<T>(UnityEngine.FindObjectsSortMode.None);
2220
#else
21+
#pragma warning disable 618
2322
return UObject.FindObjectsOfType<T>();
23+
#pragma warning restore 618
2424
#endif
2525
}
2626

@@ -29,10 +29,12 @@ public static UObject[] FindAll(Type type)
2929
{
3030
#if UNITY_6000_5_OR_NEWER
3131
return UObject.FindObjectsByType(type, UnityEngine.FindObjectsInactive.Exclude);
32-
#elif UNITY_2022_2_OR_NEWER
32+
#elif UNITY_2022_3_OR_NEWER
3333
return UObject.FindObjectsByType(type, UnityEngine.FindObjectsSortMode.None);
3434
#else
35+
#pragma warning disable 618
3536
return UObject.FindObjectsOfType(type);
37+
#pragma warning restore 618
3638
#endif
3739
}
3840

@@ -47,17 +49,21 @@ public static UObject[] FindAll(Type type, bool includeInactive)
4749
includeInactive ? UnityEngine.FindObjectsInactive.Include : UnityEngine.FindObjectsInactive.Exclude,
4850
UnityEngine.FindObjectsSortMode.None);
4951
#else
52+
#pragma warning disable 618
5053
return UObject.FindObjectsOfType(type, includeInactive);
54+
#pragma warning restore 618
5155
#endif
5256
}
5357

5458
/// <summary>Find any single object of the given runtime type (no ordering guarantee).</summary>
5559
public static UObject FindAny(Type type)
5660
{
57-
#if UNITY_2022_2_OR_NEWER
61+
#if UNITY_2022_3_OR_NEWER
5862
return UObject.FindAnyObjectByType(type);
5963
#else
64+
#pragma warning disable 618
6065
return UObject.FindObjectOfType(type);
66+
#pragma warning restore 618
6167
#endif
6268
}
6369
}

0 commit comments

Comments
 (0)