Skip to content

Commit 28bccb2

Browse files
authored
Some minor reworks to use TryGetComponent in every-frame pointer code (#10315)
* Some minor reworks to use TryGetComponent in hot loops * Update to use TryGetComponent
1 parent 1eaa37a commit 28bccb2

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

Assets/MRTK/SDK/Features/UX/Scripts/Cursors/BaseCursor.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,12 @@ public virtual CursorContextEnum CheckCursorContext()
671671
Transform contextCenter = null;
672672
if (TargetedObject)
673673
{
674+
#if UNITY_2019_4_OR_NEWER
675+
if (TargetedObject.TryGetComponent(out CursorContextInfo contextInfo) && contextInfo != null)
676+
#else
674677
var contextInfo = TargetedObject.GetComponent<CursorContextInfo>();
675678
if (contextInfo != null)
679+
#endif
676680
{
677681
cursorAction = contextInfo.CurrentCursorAction;
678682
contextCenter = contextInfo.ObjectCenter;

Assets/MRTK/SDK/Features/UX/Scripts/Pointers/PokePointer.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,13 @@ private bool FindClosestTouchableForLayerMask(LayerMask layerMask, out BaseNearI
224224
Camera mainCam = CameraCache.Main;
225225
for (int i = 0; i < numColliders; ++i)
226226
{
227-
var collider = queryBuffer[i];
228-
var touchable = collider.GetComponent<BaseNearInteractionTouchable>();
229-
if (touchable)
227+
Collider collider = queryBuffer[i];
228+
#if UNITY_2019_4_OR_NEWER
229+
if (collider.TryGetComponent(out BaseNearInteractionTouchable touchable) && touchable != null)
230+
#else
231+
BaseNearInteractionTouchable touchable = collider.GetComponent<BaseNearInteractionTouchable>();
232+
if (touchable != null)
233+
#endif
230234
{
231235
if (IgnoreCollidersNotInFOV && !mainCam.IsInFOVCached(collider))
232236
{

Assets/MRTK/SDK/Features/UX/Scripts/Pointers/SpherePointer.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -469,14 +469,11 @@ public SpherePointerQueryInfo(int bufferSize, float radius, float angle, float m
469469

470470
public bool HasValidGrabbable(Vector3 pointerPosition, Vector3 pointerAxis, bool ignoreCollidersNotInFOV, LRUCache<int, NearInteractionGrabbable> componentCache)
471471
{
472-
Vector3 grabbablePosition = pointerPosition;
473-
NearInteractionGrabbable currentGrabbable = null;
474-
475472
for (int i = 0; i < numColliders; i++)
476473
{
477474
Collider collider = queryBuffer[i];
478-
if (IsColliderValidGrabbable(collider, ignoreCollidersNotInFOV, out currentGrabbable, componentCache)
479-
&& IsColliderPositionValid(collider, pointerPosition, pointerAxis, queryAngle, queryMinDistance, out grabbablePosition))
475+
if (IsColliderValidGrabbable(collider, ignoreCollidersNotInFOV, out NearInteractionGrabbable currentGrabbable, componentCache)
476+
&& IsColliderPositionValid(collider, pointerPosition, pointerAxis, queryAngle, queryMinDistance, out _))
480477
{
481478
if (currentGrabbable != null)
482479
{
@@ -584,17 +581,23 @@ public void TryUpdateQueryBufferForLayerMask(LayerMask layerMask, Vector3 pointe
584581
public bool IsColliderValidGrabbable(Collider collider, bool ignoreCollidersNotInFOV, out NearInteractionGrabbable currentGrabbable, LRUCache<int, NearInteractionGrabbable> componentCache)
585582
{
586583
// Check if the collider has a grabbable component which is valid
584+
bool isValidGrabbable = true;
587585
int instanceId = collider.gameObject.GetInstanceID();
588586
if (!componentCache.TryGetValue(instanceId, out currentGrabbable))
589587
{
590-
currentGrabbable = collider.gameObject.GetComponent<NearInteractionGrabbable>();
588+
#if UNITY_2019_4_OR_NEWER
589+
isValidGrabbable &= collider.TryGetComponent(out currentGrabbable);
590+
#else
591+
currentGrabbable = collider.GetComponent<NearInteractionGrabbable>();
592+
#endif
591593
if (currentGrabbable != null)
592594
{
593595
componentCache.Add(instanceId, currentGrabbable);
594596
}
595597
}
596598

597-
bool isValidGrabbable = (currentGrabbable != null) && !(ignoreBoundsHandlesForQuery && currentGrabbable.IsBoundsHandles);
599+
isValidGrabbable &= (currentGrabbable != null) && !(ignoreBoundsHandlesForQuery && currentGrabbable.IsBoundsHandles);
600+
598601
if (!isValidGrabbable)
599602
{
600603
// Remove it from the cache if the grabbable is no longer valid for the object

0 commit comments

Comments
 (0)