CurvedUIPhysicsRaycaster.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 : BaseRaycaster
  12. {
  13. #region VARIABLES AND SETTINGS
  14. [SerializeField]
  15. protected int sortOrder = 20;
  16. //variables
  17. RaycastHit hitInfo;
  18. RaycastResult result;
  19. #endregion
  20. #region CONSTRUCTOR
  21. protected CurvedUIPhysicsRaycaster() { }
  22. #endregion
  23. #region RAYCASTING
  24. public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList)
  25. {
  26. //check if we have camera from which to cast a ray
  27. if (CurvedUIInputModule.Instance == null || CurvedUIInputModule.Instance.EventCamera == null)
  28. return;
  29. if (Physics.Raycast(CurvedUIInputModule.Instance.GetEventRay(), out hitInfo, float.PositiveInfinity, CompoundEventMask))
  30. {
  31. if (hitInfo.collider.GetComponentInParent<CurvedUISettings>()) return; //a canvas is hit - these raycastsResults are handled by CurvedUIRaycasters
  32. result = new RaycastResult
  33. {
  34. gameObject = hitInfo.collider.gameObject,
  35. module = this,
  36. distance = hitInfo.distance,
  37. index = resultAppendList.Count,
  38. worldPosition = hitInfo.point,
  39. worldNormal = hitInfo.normal,
  40. };
  41. resultAppendList.Add(result);
  42. }
  43. //Debug.Log("CUIPhysRaycaster: " + resultAppendList.Count);
  44. }
  45. #endregion
  46. #region SETTERS AND GETTERS
  47. /// <summary>
  48. /// This Component's event mask + eventCamera's event mask.
  49. /// </summary>
  50. public int CompoundEventMask {
  51. get { return (eventCamera != null) ? eventCamera.cullingMask & CurvedUIInputModule.Instance.RaycastLayerMask : -1; }
  52. }
  53. /// <summary>
  54. /// Camera used to process events
  55. /// </summary>
  56. public override Camera eventCamera {
  57. get { return CurvedUIInputModule.Instance? CurvedUIInputModule.Instance.EventCamera : null; }
  58. }
  59. public virtual int Depth {
  60. get { return (eventCamera != null) ? (int)eventCamera.depth : 0xFFFFFF; }
  61. }
  62. public override int sortOrderPriority {
  63. get { return sortOrder; }
  64. }
  65. #endregion
  66. }
  67. }