XRPhysicsRaycaster.cs 1.8 KB

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