XRPhysicsRaycaster.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. namespace XRTool.WorldUI
  5. {
  6. /// <summary>
  7. /// Simple event system using physics raycasts.
  8. /// </summary>
  9. [AddComponentMenu("Event/XR Physics Raycaster")]
  10. [RequireComponent(typeof(Camera))]
  11. public class XRPhysicsRaycaster : PhysicsRaycaster
  12. {
  13. private RaycastResult minResult;
  14. private PhysicsRayConf layerConf;
  15. public PhysicsRayConf LayerConf { get => layerConf; set => layerConf = value; }
  16. protected override void Start()
  17. {
  18. base.Start();
  19. LayerConf = Resources.Load<PhysicsRayConf>("PhysicsRayConf");
  20. eventCamera.cullingMask = LayerConf.eventMask;
  21. eventMask = LayerConf.eventMask;
  22. }
  23. public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList)
  24. {
  25. if (resultAppendList != null && resultAppendList.Count > 0)
  26. {
  27. float minDis = float.MaxValue;
  28. int minIndex = 0;
  29. for (int i = 0; i < resultAppendList.Count; i++)
  30. {
  31. RaycastResult result = resultAppendList[i];
  32. if (result.distance < minDis)
  33. {
  34. minDis = result.distance;
  35. minIndex = i;
  36. }
  37. }
  38. minResult = resultAppendList[minIndex];
  39. resultAppendList.Clear();
  40. resultAppendList.Add(minResult);
  41. base.Raycast(eventData, resultAppendList);
  42. }
  43. else
  44. {
  45. base.Raycast(eventData, resultAppendList);
  46. }
  47. }
  48. }
  49. }