DetectFoldings.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using JTSystems;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. public class DetectFoldings : MonoBehaviour, IPointerUpHandler, IPointerClickHandler, IPointerDownHandler
  5. {
  6. [Header(" Settings ")]
  7. [SerializeField] private LayerMask detectLayers;
  8. [SerializeField] private bool playTesting;
  9. [SerializeField] private bool _includeInactiveInSearch = true;
  10. Camera mainCamera;
  11. private void Awake()
  12. {
  13. mainCamera = Camera.main;
  14. }
  15. public void OnPointerClick(PointerEventData eventData)
  16. {
  17. }
  18. public void OnPointerDown(PointerEventData eventData)
  19. {
  20. }
  21. /// <summary>
  22. /// 这里需要加判断 如果结束之类的需要返回
  23. /// </summary>
  24. /// <param name="eventData"></param>
  25. public void OnPointerUp(PointerEventData eventData)
  26. {
  27. if (true )
  28. DetectClosestFolding();
  29. }
  30. private void DetectClosestFolding()
  31. {
  32. Ray tapRay = mainCamera.ScreenPointToRay(Input.mousePosition);
  33. Physics.Raycast(tapRay, out RaycastHit hit, 50, detectLayers);
  34. if (hit.collider == null)
  35. return;
  36. Folding[] detectedFoldings = FindObjectsOfType<Folding>(_includeInactiveInSearch);
  37. int closestFoldingIndex = -1;
  38. float minDistance = 5000;
  39. for (int i = 0; i < detectedFoldings.Length; i++)
  40. {
  41. Folding currentFolding = detectedFoldings[i];
  42. float t = Vector3.Distance(hit.point, currentFolding.GetFoldingPosition());
  43. if(t < minDistance)
  44. {
  45. minDistance = t;
  46. closestFoldingIndex = i;
  47. }
  48. }
  49. if (closestFoldingIndex >= 0)
  50. {
  51. Folding colsestFolding = detectedFoldings[closestFoldingIndex];
  52. if (!colsestFolding.gameObject.activeInHierarchy)
  53. return;
  54. Paper currentPaper = colsestFolding.GetComponentInParent<Paper>();
  55. currentPaper.TryFold(colsestFolding);
  56. }
  57. }
  58. }