Skip to content

Commit c4d2873

Browse files
Add Precompile Statements to Prevent Warnings in Unity 2023 (#11790)
1 parent e6a567b commit c4d2873

File tree

28 files changed

+138
-38
lines changed

28 files changed

+138
-38
lines changed

Assets/MRTK/Core/Inspectors/MixedRealityToolkitFacadeHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private static void OnSceneSaved(Scene scene)
5959

6060
private static void CleanupCurrentFacades()
6161
{
62-
foreach (MixedRealityToolkit toolkitInstance in GameObject.FindObjectsOfType<MixedRealityToolkit>())
62+
foreach (MixedRealityToolkit toolkitInstance in FindObjectUtility.FindObjectsByType<MixedRealityToolkit>())
6363
{
6464
DestroyAllChildren(toolkitInstance);
6565
}

Assets/MRTK/Core/Inspectors/Profiles/MixedRealityToolkitConfigurationProfileInspector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ protected override void OnEnable()
423423
}
424424
else
425425
{
426-
foreach (var dbr in FindObjectsOfType<DepthBufferRenderer>())
426+
foreach (var dbr in FindObjectUtility.FindObjectsByType<DepthBufferRenderer>())
427427
{
428428
UnityObjectExtensions.DestroyObject(dbr);
429429
}

Assets/MRTK/Core/Inspectors/PropertyDrawers/SceneInfoUtils.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using Microsoft.MixedReality.Toolkit.Utilities;
45
using Microsoft.MixedReality.Toolkit.SceneSystem;
56
using System;
67
using System.Collections.Generic;
@@ -346,7 +347,7 @@ private static void RefreshSceneInfoFieldsInOpenScenes()
346347
foreach (Tuple<Type, FieldInfo> typeFieldInfoPair in cachedComponentTypes)
347348
{
348349
FieldInfo fieldInfo = typeFieldInfoPair.Item2;
349-
foreach (Component source in GameObject.FindObjectsOfType(typeFieldInfoPair.Item1))
350+
foreach (Component source in FindObjectUtility.FindObjectsByType(typeFieldInfoPair.Item1))
350351
{
351352
CheckForChangesInField(source, fieldInfo);
352353
}
@@ -425,4 +426,4 @@ private static void CheckForChangesInField(UnityEngine.Object source, FieldInfo
425426
}
426427
}
427428
}
428-
}
429+
}

Assets/MRTK/Core/MRTK.Core.asmdef

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "Microsoft.MixedReality.Toolkit",
3+
"rootNamespace": "",
34
"references": [
45
"Microsoft.MixedReality.Toolkit.Async",
56
"Microsoft.MixedReality.Toolkit.Editor.Utilities",
@@ -22,6 +23,11 @@
2223
"name": "com.unity.xr.management",
2324
"expression": "",
2425
"define": "XR_MANAGEMENT_ENABLED"
26+
},
27+
{
28+
"name": "Unity",
29+
"expression": "2021.3.18",
30+
"define": "UNITY_2021_3_18_OR_NEWER"
2531
}
2632
],
2733
"noEngineReferences": false

Assets/MRTK/Core/Services/MixedRealityToolkit.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public MixedRealityToolkitConfigurationProfile ActiveProfile
132132
/// </remarks>
133133
public static void SetProfileBeforeInitialization(MixedRealityToolkitConfigurationProfile profile)
134134
{
135-
MixedRealityToolkit toolkit = FindObjectOfType<MixedRealityToolkit>();
135+
MixedRealityToolkit toolkit = FindObjectUtility.FindObjectByType<MixedRealityToolkit>();
136136
toolkit.activeProfile = profile;
137137
}
138138

@@ -586,7 +586,7 @@ private void EnsureMixedRealityRequirements()
586586
bool addedComponents = false;
587587
if (!Application.isPlaying && CameraCache.Main != null)
588588
{
589-
EventSystem[] eventSystems = FindObjectsOfType<EventSystem>();
589+
EventSystem[] eventSystems = FindObjectUtility.FindObjectsByType<EventSystem>();
590590

591591
if (eventSystems.Length == 0)
592592
{
@@ -644,7 +644,7 @@ public static MixedRealityToolkit Instance
644644
//
645645
// To avoid returning null in this case, make sure to search the scene for MRTK.
646646
// We do this only when in editor to avoid any performance cost at runtime.
647-
List<MixedRealityToolkit> mrtks = new List<MixedRealityToolkit>(FindObjectsOfType<MixedRealityToolkit>());
647+
List<MixedRealityToolkit> mrtks = new List<MixedRealityToolkit>(FindObjectUtility.FindObjectsByType<MixedRealityToolkit>());
648648
// Sort the list by instance ID so we get deterministic results when selecting our next active instance
649649
mrtks.Sort(delegate (MixedRealityToolkit i1, MixedRealityToolkit i2) { return i1.GetInstanceID().CompareTo(i2.GetInstanceID()); });
650650

Assets/MRTK/Core/Utilities/Async/Internal/AsyncCoroutineRunner.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ internal static AsyncCoroutineRunner Instance
5151
{
5252
if (instance == null)
5353
{
54-
instance = FindObjectOfType<AsyncCoroutineRunner>();
54+
#if UNITY_2021_3_18_OR_NEWER
55+
instance = UnityEngine.Object.FindFirstObjectByType<AsyncCoroutineRunner>();
56+
#else
57+
instance = GameObject.FindObjectOfType<AsyncCoroutineRunner>();
58+
#endif
5559
}
5660

5761
// FindObjectOfType() only search for objects attached to active GameObjects. The FindObjectOfType(bool includeInactive) variant is not available to Unity 2019.4 and earlier so cannot be used.
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
{
22
"name": "Microsoft.MixedReality.Toolkit.Async",
3+
"rootNamespace": "",
34
"references": [],
4-
"optionalUnityReferences": [],
55
"includePlatforms": [],
66
"excludePlatforms": [],
77
"allowUnsafeCode": false,
88
"overrideReferences": false,
99
"precompiledReferences": [],
1010
"autoReferenced": true,
11-
"defineConstraints": []
11+
"defineConstraints": [],
12+
"versionDefines": [
13+
{
14+
"name": "Unity",
15+
"expression": "2021.3.18",
16+
"define": "UNITY_2021_3_18_OR_NEWER"
17+
}
18+
],
19+
"noEngineReferences": false
1220
}

Assets/MRTK/Core/Utilities/CameraCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static Camera Main
3838
Debug.Log("No main camera found. Searching for cameras in the scene.");
3939

4040
// If no main camera was found, try to determine one.
41-
Camera[] cameras = Object.FindObjectsOfType<Camera>();
41+
Camera[] cameras = FindObjectUtility.FindObjectsByType<Camera>();
4242
if (cameras.Length == 0)
4343
{
4444
Debug.LogWarning("No cameras found. Creating a \"MainCamera\".");
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
}

Assets/MRTK/Core/Utilities/FindObjectUtility.cs.meta

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

Assets/MRTK/Examples/Demos/EyeTracking/DemoVisualizer/Scripts/UserInputRecorder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using Microsoft.MixedReality.Toolkit.Utilities;
45
using Microsoft.MixedReality.Toolkit.Input;
56
using System;
67
using System.IO;
@@ -26,7 +27,7 @@ public static UserInputRecorder Instance
2627
{
2728
if (instance == null)
2829
{
29-
instance = FindObjectOfType<UserInputRecorder>();
30+
instance = FindObjectUtility.FindObjectByType<UserInputRecorder>();
3031
}
3132
return instance;
3233
}
@@ -148,4 +149,4 @@ public override void OnDestroy()
148149
base.OnDestroy();
149150
}
150151
}
151-
}
152+
}

Assets/MRTK/Examples/Demos/EyeTracking/General/Scripts/Utils/StatusText.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using Microsoft.MixedReality.Toolkit.Utilities;
45
using UnityEngine;
56

67
namespace Microsoft.MixedReality.Toolkit.Examples.Demos.EyeTracking
@@ -17,7 +18,7 @@ public static StatusText Instance
1718
{
1819
if (_Instance == null)
1920
{
20-
_Instance = FindObjectOfType<StatusText>();
21+
_Instance = FindObjectUtility.FindObjectByType<StatusText>();
2122
}
2223
return _Instance;
2324
}

Assets/MRTK/Extensions/SceneTransitionService/SceneTransitionService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,15 +422,15 @@ private List<Camera> GatherFadeTargetCameras()
422422
{
423423
case CameraFaderTargets.All:
424424
// Add every single camera in all scenes
425-
targetCameras.AddRange(GameObject.FindObjectsOfType<Camera>());
425+
targetCameras.AddRange(FindObjectUtility.FindObjectsByType<Camera>());
426426
break;
427427

428428
case CameraFaderTargets.Main:
429429
targetCameras.Add(CameraCache.Main);
430430
break;
431431

432432
case CameraFaderTargets.UI:
433-
foreach (Canvas canvas in GameObject.FindObjectsOfType<Canvas>())
433+
foreach (Canvas canvas in FindObjectUtility.FindObjectsByType<Canvas>())
434434
{
435435
switch (canvas.renderMode)
436436
{

Assets/MRTK/Providers/Oculus/XRSDK/OculusXRSDKDeviceManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public override void Update()
207207

208208
private void SetupInput()
209209
{
210-
cameraRig = GameObject.FindObjectOfType<OVRCameraRig>();
210+
cameraRig = FindObjectUtility.FindObjectsByType<OVRCameraRig>();
211211
if (cameraRig == null)
212212
{
213213
var mainCamera = CameraCache.Main;

Assets/MRTK/SDK/Editor/Migration/BoundsControlMigrationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ private void MigrateAppBar(BoundingBox boundingBox, BoundsControl boundsControl)
297297
{
298298
// note: this might be expensive for larger scenes but we don't know where the appbar is
299299
// placed in the hierarchy so we have to search the scene for it
300-
AppBar[] appBars = Object.FindObjectsOfType<AppBar>();
300+
AppBar[] appBars = FindObjectUtility.FindObjectsByType<AppBar>();
301301
for (int i = 0; i < appBars.Length; ++i)
302302
{
303303
AppBar appBar = appBars[i];

Assets/MRTK/SDK/Experimental/Features/Utilities/WorldAnchorManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ public void RemoveAllAnchors()
336336
Debug.LogWarning("[WorldAnchorManager] RemoveAllAnchors called before anchor store is ready.");
337337
}
338338

339-
var anchors = FindObjectsOfType<WorldAnchor>();
339+
var anchors = FindObjectUtility.FindObjectsByType<WorldAnchor>();
340340

341341
if (anchors == null)
342342
{

Assets/MRTK/SDK/Experimental/ServiceManagers/Input/Scripts/InputSystemManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private void InitializeManager()
6666
Initialize<IMixedRealityRaycastProvider>(profile.RaycastProviderType, args: args);
6767

6868

69-
EventSystem[] eventSystems = FindObjectsOfType<EventSystem>();
69+
EventSystem[] eventSystems = FindObjectUtility.FindObjectsByType<EventSystem>();
7070

7171
if (eventSystems.Length == 0)
7272
{

Assets/MRTK/Services/InputSystem/MixedRealityInputSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public override void Initialize()
170170
return;
171171
}
172172

173-
BaseInputModule[] inputModules = UnityEngine.Object.FindObjectsOfType<BaseInputModule>();
173+
BaseInputModule[] inputModules = FindObjectUtility.FindObjectsByType<BaseInputModule>();
174174

175175
if (inputModules.Length == 0)
176176
{

Assets/MRTK/Services/TeleportSystem/MixedRealityTeleportSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private void InitializeInternal()
6060
#if UNITY_EDITOR
6161
if (!UnityEditor.EditorApplication.isPlaying)
6262
{
63-
var eventSystems = Object.FindObjectsOfType<EventSystem>();
63+
var eventSystems = FindObjectUtility.FindObjectsByType<EventSystem>();
6464

6565
if (eventSystems.Length == 0)
6666
{

Assets/MRTK/Tests/EditModeTests/Core/MixedRealityToolkitTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using Microsoft.MixedReality.Toolkit.Utilities;
45
using Microsoft.MixedReality.Toolkit.Tests.EditMode.Services;
56
using NUnit.Framework;
67
using System.Collections.Generic;
@@ -476,7 +477,7 @@ public void TestCreateMultipleInstancesInMultipleScenes()
476477
_ = new GameObject("MixedRealityToolkit").AddComponent<MixedRealityToolkit>();
477478
}
478479

479-
MixedRealityToolkit[] instances = GameObject.FindObjectsOfType<MixedRealityToolkit>();
480+
MixedRealityToolkit[] instances = FindObjectUtility.FindObjectsByType<MixedRealityToolkit>();
480481
for (int i = 0; i < instances.Length; i++)
481482
{
482483
MixedRealityToolkit.SetActiveInstance(instances[i]);

Assets/MRTK/Tests/PlayModeTests/BaseCursorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,9 @@ public IEnumerator CursorContextScaleRotate()
360360
public IEnumerator CursorScaling()
361361
{
362362
// Finding or initializing necessary objects
363-
Camera cam = GameObject.FindObjectOfType<Camera>();
363+
Camera cam = FindObjectUtility.FindObjectByType<Camera>();
364364

365-
BaseCursor baseCursor = GameObject.FindObjectOfType<BaseCursor>();
365+
BaseCursor baseCursor = FindObjectUtility.FindObjectByType<BaseCursor>();
366366
Assert.IsNotNull(baseCursor);
367367

368368
// Make sure resizing is turned on

Assets/MRTK/Tests/PlayModeTests/ChangeActiveProfileTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// issue will likely persist for 2018, this issue is worked around by wrapping all
1111
// play mode tests in this check.
1212

13+
using Microsoft.MixedReality.Toolkit.Utilities;
1314
using Microsoft.MixedReality.Toolkit.Boundary;
1415
using NUnit.Framework;
1516
using System.Collections;
@@ -150,7 +151,7 @@ public IEnumerator VerifyPlayspaceChildren()
150151
int uiRaycastCameraCount = 0;
151152
// Confirm that we have one UIRaycastCamera.
152153
Debug.Log("Validating UIRaycastCamera count.");
153-
Camera[] cameras = GameObject.FindObjectsOfType<Camera>();
154+
Camera[] cameras = FindObjectUtility.FindObjectsByType<Camera>();
154155
foreach (Camera camera in cameras)
155156
{
156157
if ("UIRaycastCamera" == camera.name)
@@ -177,4 +178,4 @@ public IEnumerator VerifyPlayspaceChildren()
177178
}
178179
}
179180

180-
#endif // !WINDOWS_UWP
181+
#endif // !WINDOWS_UWP

Assets/MRTK/Tests/PlayModeTests/Core/Providers/Hands/RiggedHandVisualizerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public IEnumerator TestRiggedHand()
4545
var rightHand = new TestHand(Handedness.Right);
4646
yield return rightHand.Show(Vector3.zero);
4747

48-
RiggedHandVisualizer handVisualizer = GameObject.FindObjectOfType<RiggedHandVisualizer>().GetComponent<RiggedHandVisualizer>();
48+
RiggedHandVisualizer handVisualizer = FindObjectUtility.FindObjectByType<RiggedHandVisualizer>().GetComponent<RiggedHandVisualizer>();
4949

5050
yield return rightHand.SetGesture(ArticulatedHandPose.GestureId.Open);
5151
Assert.IsTrue(handVisualizer.HandRenderer.sharedMaterial.GetFloat(handVisualizer.PinchStrengthMaterialProperty) < 0.5f);

Assets/MRTK/Tests/PlayModeTests/Core/Utilities/Async/AsyncCoroutineRunnerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public IEnumerator TestNonRootParenting()
2929
{
3030
// Set up the AsyncCoroutineRunner to be parented under the MixedRealityPlayspace,
3131
// to validate that the runner will correctly unparent it.
32-
AsyncCoroutineRunner asyncCoroutineRunner = Object.FindObjectOfType<AsyncCoroutineRunner>();
32+
AsyncCoroutineRunner asyncCoroutineRunner = FindObjectUtility.FindObjectByType<AsyncCoroutineRunner>();
3333
if (asyncCoroutineRunner == null)
3434
{
3535
asyncCoroutineRunner = new GameObject("AsyncCoroutineRunner").AddComponent<AsyncCoroutineRunner>();

0 commit comments

Comments
 (0)