RayInteractable.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using UnityEngine;
  2. using UnityEngine.Assertions;
  3. namespace Rokid.UXR.Interaction
  4. {
  5. public class RayInteractable : PointerInteractable<RayInteractor, RayInteractable>
  6. {
  7. [SerializeField, Interface(typeof(ISurface))]
  8. private MonoBehaviour _surface;
  9. public ISurface Surface { get; private set; }
  10. [SerializeField, Optional, Interface(typeof(ISurface))]
  11. private MonoBehaviour _selectSurface = null;
  12. private ISurface SelectSurface;
  13. [HideInInspector, SerializeField, Optional, Interface(typeof(IMovementProvider))]
  14. private MonoBehaviour _movementProvider;
  15. private IMovementProvider MovementProvider { get; set; }
  16. [HideInInspector, SerializeField, Optional]
  17. private int _tiebreakerScore = 0;
  18. #region Properties
  19. public int TiebreakerScore
  20. {
  21. get
  22. {
  23. return _tiebreakerScore;
  24. }
  25. set
  26. {
  27. _tiebreakerScore = value;
  28. }
  29. }
  30. #endregion
  31. protected override void Awake()
  32. {
  33. base.Awake();
  34. Surface = _surface as ISurface;
  35. SelectSurface = _selectSurface as ISurface;
  36. MovementProvider = _movementProvider as IMovementProvider;
  37. }
  38. protected override void Start()
  39. {
  40. Assert.IsNotNull(Surface);
  41. if (_selectSurface != null)
  42. {
  43. Assert.IsNotNull(SelectSurface);
  44. }
  45. else
  46. {
  47. SelectSurface = Surface;
  48. _selectSurface = SelectSurface as MonoBehaviour;
  49. }
  50. }
  51. /// <summary>
  52. /// 射线和平面检测的关键逻辑
  53. /// </summary>
  54. /// <param name="ray"></param>
  55. /// <param name="hit"></param>
  56. /// <param name="maxDistance"></param>
  57. /// <param name="selectSurface"></param>
  58. /// <returns></returns>
  59. public bool Raycast(Ray ray, out SurfaceHit hit, in float maxDistance, bool selectSurface)
  60. {
  61. hit = new SurfaceHit();
  62. ISurface surface = selectSurface ? SelectSurface : Surface;
  63. return surface.Raycast(ray, out hit, maxDistance);
  64. }
  65. public IMovement GenerateMovement(in Pose to, in Pose source)
  66. {
  67. if (MovementProvider == null)
  68. {
  69. return null;
  70. }
  71. IMovement movement = MovementProvider.CreateMovement();
  72. movement.StopAndSetPose(source);
  73. movement.MoveTo(to);
  74. return movement;
  75. }
  76. }
  77. }