CurvedUIPhysicsRaycaster.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.EventSystems;
  4. using UnityEngine.UI;
  5. using System.Collections.Generic;
  6. namespace CurvedUI
  7. {
  8. /// <summary>
  9. /// Raycaster used for interactions with 3D objects.
  10. /// </summary>
  11. public class CurvedUIPhysicsRaycaster : PhysicsRaycaster {
  12. #region VARIABLES AND SETTINGS
  13. //settings
  14. [SerializeField]
  15. protected LayerMask eventMask = -1; //Which layers should we raycast on?
  16. //[SerializeField]
  17. //protected int sortOrder = 20;
  18. //variables
  19. RaycastHit hitInfo;
  20. RaycastResult result;
  21. #endregion
  22. #region CONSTRUCTOR
  23. protected CurvedUIPhysicsRaycaster() { }
  24. #endregion
  25. #region RAYCASTING
  26. public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList)
  27. {
  28. //check if we have camera from which to cast a ray
  29. if (CurvedUIInputModule.Instance == null || CurvedUIInputModule.Instance.EventCamera == null)
  30. return;
  31. if (Physics.Raycast(CurvedUIInputModule.Instance.GetEventRay(), out hitInfo, float.PositiveInfinity, CompoundEventMask))
  32. {
  33. if (hitInfo.collider.GetComponentInParent<CurvedUISettings>()) return; //a canvas is hit - these raycastsResults are handled by CurvedUIRaycasters
  34. result = new RaycastResult
  35. {
  36. gameObject = hitInfo.collider.gameObject,
  37. module = this,
  38. distance = hitInfo.distance,
  39. index = resultAppendList.Count,
  40. worldPosition = hitInfo.point,
  41. worldNormal = hitInfo.normal,
  42. };
  43. resultAppendList.Add(result);
  44. }
  45. //Debug.Log("CUIPhysRaycaster: " + resultAppendList.Count);
  46. }
  47. #endregion
  48. #region SETTERS AND GETTERS
  49. /// <summary>
  50. /// This Component's event mask + eventCamera's event mask.
  51. /// </summary>
  52. public int CompoundEventMask {
  53. get { return (eventCamera != null) ? eventCamera.cullingMask & eventMask : -1; }
  54. }
  55. /// <summary>
  56. /// Layer mask used to filter raycasts. Combined with eventCamera's culling mask.
  57. /// </summary>
  58. public LayerMask EventMask {
  59. get { return eventMask; }
  60. set { eventMask = value; }
  61. }
  62. /// <summary>
  63. /// Camera used to process events
  64. /// </summary>
  65. public override Camera eventCamera {
  66. get { return CurvedUIInputModule.Instance? CurvedUIInputModule.Instance.EventCamera : null; }
  67. }
  68. public virtual int Depth {
  69. get { return (eventCamera != null) ? (int)eventCamera.depth : 0xFFFFFF; }
  70. }
  71. //public override int sortOrderPriority {
  72. // get { return sortOrder; }
  73. //}
  74. #endregion
  75. }
  76. }