AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
EventSystemExtensions.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System.Collections.Generic;
5 using UnityEngine;
6 using UnityEngine.EventSystems;
7 
8 namespace HoloToolkit.Unity
9 {
10  public static class EventSystemExtensions
11  {
12  private static readonly List<RaycastResult> RaycastResults = new List<RaycastResult>();
13  private static readonly RaycastResultComparer RaycastResultComparer = new RaycastResultComparer();
14 
20  public static RaycastResult Raycast(this EventSystem eventSystem, PointerEventData pointerEventData, LayerMask[] layerMasks)
21  {
22  eventSystem.RaycastAll(pointerEventData, RaycastResults);
23  return PrioritizeRaycastResult(layerMasks);
24  }
25 
26  private static RaycastResult PrioritizeRaycastResult(LayerMask[] priority)
27  {
29 
30  for (var i = 0; i < RaycastResults.Count; i++)
31  {
32  if (RaycastResults[i].gameObject == null) { continue; }
33 
34  var layerMaskIndex = RaycastResults[i].gameObject.layer.FindLayerListIndex(priority);
35  if (layerMaskIndex == -1) { continue; }
36 
37  var result = new ComparableRaycastResult(RaycastResults[i], layerMaskIndex);
38 
39  if (maxResult.RaycastResult.module == null || RaycastResultComparer.Compare(maxResult, result) < 0)
40  {
41  maxResult = result;
42  }
43  }
44 
45  return maxResult.RaycastResult;
46  }
47  }
48 }
static RaycastResult Raycast(this EventSystem eventSystem, PointerEventData pointerEventData, LayerMask[] layerMasks)
Executes a raycast all and returns the closest element. Fixes the current issue with Unity&#39;s raycast ...