Skip to content

Commit

Permalink
Add Precompile Statements to Prevent Warnings in Unity 2023 (#11790)
Browse files Browse the repository at this point in the history
  • Loading branch information
marlenaklein-msft authored Oct 26, 2023
1 parent e6a567b commit c4d2873
Show file tree
Hide file tree
Showing 28 changed files with 138 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private static void OnSceneSaved(Scene scene)

private static void CleanupCurrentFacades()
{
foreach (MixedRealityToolkit toolkitInstance in GameObject.FindObjectsOfType<MixedRealityToolkit>())
foreach (MixedRealityToolkit toolkitInstance in FindObjectUtility.FindObjectsByType<MixedRealityToolkit>())
{
DestroyAllChildren(toolkitInstance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ protected override void OnEnable()
}
else
{
foreach (var dbr in FindObjectsOfType<DepthBufferRenderer>())
foreach (var dbr in FindObjectUtility.FindObjectsByType<DepthBufferRenderer>())
{
UnityObjectExtensions.DestroyObject(dbr);
}
Expand Down
5 changes: 3 additions & 2 deletions Assets/MRTK/Core/Inspectors/PropertyDrawers/SceneInfoUtils.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.MixedReality.Toolkit.Utilities;
using Microsoft.MixedReality.Toolkit.SceneSystem;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -346,7 +347,7 @@ private static void RefreshSceneInfoFieldsInOpenScenes()
foreach (Tuple<Type, FieldInfo> typeFieldInfoPair in cachedComponentTypes)
{
FieldInfo fieldInfo = typeFieldInfoPair.Item2;
foreach (Component source in GameObject.FindObjectsOfType(typeFieldInfoPair.Item1))
foreach (Component source in FindObjectUtility.FindObjectsByType(typeFieldInfoPair.Item1))
{
CheckForChangesInField(source, fieldInfo);
}
Expand Down Expand Up @@ -425,4 +426,4 @@ private static void CheckForChangesInField(UnityEngine.Object source, FieldInfo
}
}
}
}
}
6 changes: 6 additions & 0 deletions Assets/MRTK/Core/MRTK.Core.asmdef
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "Microsoft.MixedReality.Toolkit",
"rootNamespace": "",
"references": [
"Microsoft.MixedReality.Toolkit.Async",
"Microsoft.MixedReality.Toolkit.Editor.Utilities",
Expand All @@ -22,6 +23,11 @@
"name": "com.unity.xr.management",
"expression": "",
"define": "XR_MANAGEMENT_ENABLED"
},
{
"name": "Unity",
"expression": "2021.3.18",
"define": "UNITY_2021_3_18_OR_NEWER"
}
],
"noEngineReferences": false
Expand Down
6 changes: 3 additions & 3 deletions Assets/MRTK/Core/Services/MixedRealityToolkit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public MixedRealityToolkitConfigurationProfile ActiveProfile
/// </remarks>
public static void SetProfileBeforeInitialization(MixedRealityToolkitConfigurationProfile profile)
{
MixedRealityToolkit toolkit = FindObjectOfType<MixedRealityToolkit>();
MixedRealityToolkit toolkit = FindObjectUtility.FindObjectByType<MixedRealityToolkit>();
toolkit.activeProfile = profile;
}

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ internal static AsyncCoroutineRunner Instance
{
if (instance == null)
{
instance = FindObjectOfType<AsyncCoroutineRunner>();
#if UNITY_2021_3_18_OR_NEWER
instance = UnityEngine.Object.FindFirstObjectByType<AsyncCoroutineRunner>();
#else
instance = GameObject.FindObjectOfType<AsyncCoroutineRunner>();
#endif
}

// 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.
Expand Down
12 changes: 10 additions & 2 deletions Assets/MRTK/Core/Utilities/Async/MRTK.Async.asmdef
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
{
"name": "Microsoft.MixedReality.Toolkit.Async",
"rootNamespace": "",
"references": [],
"optionalUnityReferences": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": []
"defineConstraints": [],
"versionDefines": [
{
"name": "Unity",
"expression": "2021.3.18",
"define": "UNITY_2021_3_18_OR_NEWER"
}
],
"noEngineReferences": false
}
2 changes: 1 addition & 1 deletion Assets/MRTK/Core/Utilities/CameraCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static Camera Main
Debug.Log("No main camera found. Searching for cameras in the scene.");

// If no main camera was found, try to determine one.
Camera[] cameras = Object.FindObjectsOfType<Camera>();
Camera[] cameras = FindObjectUtility.FindObjectsByType<Camera>();
if (cameras.Length == 0)
{
Debug.LogWarning("No cameras found. Creating a \"MainCamera\".");
Expand Down
65 changes: 65 additions & 0 deletions Assets/MRTK/Core/Utilities/FindObjectUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using UnityEngine;
using System;

namespace Microsoft.MixedReality.Toolkit.Utilities
{
/// <summary>
/// A static utility used to avoid deprecated Find Object functions in favor of replacements introduced in Unity >= 2021.3.18.
/// </summary>
public static class FindObjectUtility
{
/// <summary>
/// Returns the first object matching the specified type.
/// </summary>
/// <remarks>
/// If Unity >= 2021.3.18, calls FindFirstObjectByType. Otherwise calls FindObjectOfType.
/// </remarks>
/// <param name="includeInactive">If true, inactive objects will be included in the search. False by default.</param>
public static T FindObjectByType<T>(bool includeInactive = false) where T : Component
{
#if UNITY_2021_3_18_OR_NEWER
return UnityEngine.Object.FindFirstObjectByType<T>(includeInactive ? FindObjectsInactive.Include : FindObjectsInactive.Exclude);
#else
return UnityEngine.Object.FindObjectOfType<T>(includeInactive);
#endif
}

/// <summary>
/// Returns all objects matching the specified type.
/// </summary>
/// <remarks>
/// If Unity >= 2021.3.18, calls FindObjectsByType. Otherwise calls FindObjectsOfType.
/// </remarks>
/// <param name="includeInactive">If true, inactive objects will be included in the search. False by default.</param>
/// <param name="sort">If false, results will not sorted by InstanceID. True by default.</param>
public static T[] FindObjectsByType<T>(bool includeInactive = false, bool sort = true) where T : Component
{
#if UNITY_2021_3_18_OR_NEWER
return UnityEngine.Object.FindObjectsByType<T>(includeInactive ? FindObjectsInactive.Include : FindObjectsInactive.Exclude, sort ? FindObjectsSortMode.InstanceID : FindObjectsSortMode.None);
#else
return UnityEngine.Object.FindObjectsOfType<T>(includeInactive);
#endif
}

/// <summary>
/// Returns all objects matching the specified type.
/// </summary>
/// <remarks>
/// If Unity >= 2021.3.18, calls FindObjectsByType. Otherwise calls FindObjectsOfType.
/// </remarks>
/// <param name="includeInactive">If true, inactive objects will be included in the search. False by default.</param>
/// <param name="sort">If false, results will not sorted by InstanceID. True by default.</param>
/// <param name="type">The type to search for.</param>
public static UnityEngine.Object[] FindObjectsByType(Type type, bool includeInactive = false, bool sort = true)
{
#if UNITY_2021_3_18_OR_NEWER
return UnityEngine.Object.FindObjectsByType(type, includeInactive ? FindObjectsInactive.Include : FindObjectsInactive.Exclude, sort ? FindObjectsSortMode.InstanceID : FindObjectsSortMode.None);
#else
return UnityEngine.Object.FindObjectsOfType(type, includeInactive);
#endif
}
}
}
11 changes: 11 additions & 0 deletions Assets/MRTK/Core/Utilities/FindObjectUtility.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.MixedReality.Toolkit.Utilities;
using Microsoft.MixedReality.Toolkit.Input;
using System;
using System.IO;
Expand All @@ -26,7 +27,7 @@ public static UserInputRecorder Instance
{
if (instance == null)
{
instance = FindObjectOfType<UserInputRecorder>();
instance = FindObjectUtility.FindObjectByType<UserInputRecorder>();
}
return instance;
}
Expand Down Expand Up @@ -148,4 +149,4 @@ public override void OnDestroy()
base.OnDestroy();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.MixedReality.Toolkit.Utilities;
using UnityEngine;

namespace Microsoft.MixedReality.Toolkit.Examples.Demos.EyeTracking
Expand All @@ -17,7 +18,7 @@ public static StatusText Instance
{
if (_Instance == null)
{
_Instance = FindObjectOfType<StatusText>();
_Instance = FindObjectUtility.FindObjectByType<StatusText>();
}
return _Instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,15 +422,15 @@ private List<Camera> GatherFadeTargetCameras()
{
case CameraFaderTargets.All:
// Add every single camera in all scenes
targetCameras.AddRange(GameObject.FindObjectsOfType<Camera>());
targetCameras.AddRange(FindObjectUtility.FindObjectsByType<Camera>());
break;

case CameraFaderTargets.Main:
targetCameras.Add(CameraCache.Main);
break;

case CameraFaderTargets.UI:
foreach (Canvas canvas in GameObject.FindObjectsOfType<Canvas>())
foreach (Canvas canvas in FindObjectUtility.FindObjectsByType<Canvas>())
{
switch (canvas.renderMode)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public override void Update()

private void SetupInput()
{
cameraRig = GameObject.FindObjectOfType<OVRCameraRig>();
cameraRig = FindObjectUtility.FindObjectsByType<OVRCameraRig>();
if (cameraRig == null)
{
var mainCamera = CameraCache.Main;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ private void MigrateAppBar(BoundingBox boundingBox, BoundsControl boundsControl)
{
// note: this might be expensive for larger scenes but we don't know where the appbar is
// placed in the hierarchy so we have to search the scene for it
AppBar[] appBars = Object.FindObjectsOfType<AppBar>();
AppBar[] appBars = FindObjectUtility.FindObjectsByType<AppBar>();
for (int i = 0; i < appBars.Length; ++i)
{
AppBar appBar = appBars[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public void RemoveAllAnchors()
Debug.LogWarning("[WorldAnchorManager] RemoveAllAnchors called before anchor store is ready.");
}

var anchors = FindObjectsOfType<WorldAnchor>();
var anchors = FindObjectUtility.FindObjectsByType<WorldAnchor>();

if (anchors == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private void InitializeManager()
Initialize<IMixedRealityRaycastProvider>(profile.RaycastProviderType, args: args);


EventSystem[] eventSystems = FindObjectsOfType<EventSystem>();
EventSystem[] eventSystems = FindObjectUtility.FindObjectsByType<EventSystem>();

if (eventSystems.Length == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public override void Initialize()
return;
}

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

if (inputModules.Length == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private void InitializeInternal()
#if UNITY_EDITOR
if (!UnityEditor.EditorApplication.isPlaying)
{
var eventSystems = Object.FindObjectsOfType<EventSystem>();
var eventSystems = FindObjectUtility.FindObjectsByType<EventSystem>();

if (eventSystems.Length == 0)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.MixedReality.Toolkit.Utilities;
using Microsoft.MixedReality.Toolkit.Tests.EditMode.Services;
using NUnit.Framework;
using System.Collections.Generic;
Expand Down Expand Up @@ -476,7 +477,7 @@ public void TestCreateMultipleInstancesInMultipleScenes()
_ = new GameObject("MixedRealityToolkit").AddComponent<MixedRealityToolkit>();
}

MixedRealityToolkit[] instances = GameObject.FindObjectsOfType<MixedRealityToolkit>();
MixedRealityToolkit[] instances = FindObjectUtility.FindObjectsByType<MixedRealityToolkit>();
for (int i = 0; i < instances.Length; i++)
{
MixedRealityToolkit.SetActiveInstance(instances[i]);
Expand Down
4 changes: 2 additions & 2 deletions Assets/MRTK/Tests/PlayModeTests/BaseCursorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,9 @@ public IEnumerator CursorContextScaleRotate()
public IEnumerator CursorScaling()
{
// Finding or initializing necessary objects
Camera cam = GameObject.FindObjectOfType<Camera>();
Camera cam = FindObjectUtility.FindObjectByType<Camera>();

BaseCursor baseCursor = GameObject.FindObjectOfType<BaseCursor>();
BaseCursor baseCursor = FindObjectUtility.FindObjectByType<BaseCursor>();
Assert.IsNotNull(baseCursor);

// Make sure resizing is turned on
Expand Down
5 changes: 3 additions & 2 deletions Assets/MRTK/Tests/PlayModeTests/ChangeActiveProfileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// issue will likely persist for 2018, this issue is worked around by wrapping all
// play mode tests in this check.

using Microsoft.MixedReality.Toolkit.Utilities;
using Microsoft.MixedReality.Toolkit.Boundary;
using NUnit.Framework;
using System.Collections;
Expand Down Expand Up @@ -150,7 +151,7 @@ public IEnumerator VerifyPlayspaceChildren()
int uiRaycastCameraCount = 0;
// Confirm that we have one UIRaycastCamera.
Debug.Log("Validating UIRaycastCamera count.");
Camera[] cameras = GameObject.FindObjectsOfType<Camera>();
Camera[] cameras = FindObjectUtility.FindObjectsByType<Camera>();
foreach (Camera camera in cameras)
{
if ("UIRaycastCamera" == camera.name)
Expand All @@ -177,4 +178,4 @@ public IEnumerator VerifyPlayspaceChildren()
}
}

#endif // !WINDOWS_UWP
#endif // !WINDOWS_UWP
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public IEnumerator TestRiggedHand()
var rightHand = new TestHand(Handedness.Right);
yield return rightHand.Show(Vector3.zero);

RiggedHandVisualizer handVisualizer = GameObject.FindObjectOfType<RiggedHandVisualizer>().GetComponent<RiggedHandVisualizer>();
RiggedHandVisualizer handVisualizer = FindObjectUtility.FindObjectByType<RiggedHandVisualizer>().GetComponent<RiggedHandVisualizer>();

yield return rightHand.SetGesture(ArticulatedHandPose.GestureId.Open);
Assert.IsTrue(handVisualizer.HandRenderer.sharedMaterial.GetFloat(handVisualizer.PinchStrengthMaterialProperty) < 0.5f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public IEnumerator TestNonRootParenting()
{
// Set up the AsyncCoroutineRunner to be parented under the MixedRealityPlayspace,
// to validate that the runner will correctly unparent it.
AsyncCoroutineRunner asyncCoroutineRunner = Object.FindObjectOfType<AsyncCoroutineRunner>();
AsyncCoroutineRunner asyncCoroutineRunner = FindObjectUtility.FindObjectByType<AsyncCoroutineRunner>();
if (asyncCoroutineRunner == null)
{
asyncCoroutineRunner = new GameObject("AsyncCoroutineRunner").AddComponent<AsyncCoroutineRunner>();
Expand Down
Loading

0 comments on commit c4d2873

Please sign in to comment.