1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using UnityEngine;
- using UnityEngine.EventSystems;
- public class DetectFoldings : MonoBehaviour, IPointerUpHandler, IPointerClickHandler, IPointerDownHandler
- {
- [Header(" Settings ")]
- [SerializeField] private LayerMask detectLayers;
- [SerializeField] private bool playTesting;
- [SerializeField] private bool _includeInactiveInSearch = true;
- Camera mainCamera;
- private void Awake()
- {
- mainCamera = Camera.main;
- }
- public void OnPointerClick(PointerEventData eventData)
- {
-
- }
- public void OnPointerDown(PointerEventData eventData)
- {
-
- }
- /// <summary>
- /// 这里需要加判断 如果结束之类的需要返回
- /// </summary>
- /// <param name="eventData"></param>
- public void OnPointerUp(PointerEventData eventData)
- {
- if (true )
- DetectClosestFolding();
- }
- private void DetectClosestFolding()
- {
- Ray tapRay = mainCamera.ScreenPointToRay(Input.mousePosition);
- Physics.Raycast(tapRay, out RaycastHit hit, 50, detectLayers);
- if (hit.collider == null)
- return;
- Folding[] detectedFoldings = FindObjectsOfType<Folding>(_includeInactiveInSearch);
- int closestFoldingIndex = -1;
- float minDistance = 5000;
- for (int i = 0; i < detectedFoldings.Length; i++)
- {
- Folding currentFolding = detectedFoldings[i];
- float t = Vector3.Distance(hit.point, currentFolding.GetFoldingPosition());
- if(t < minDistance)
- {
- minDistance = t;
- closestFoldingIndex = i;
- }
- }
- if (closestFoldingIndex >= 0)
- {
- Folding colsestFolding = detectedFoldings[closestFoldingIndex];
- if (!colsestFolding.gameObject.activeInHierarchy)
- return;
- Paper currentPaper = colsestFolding.GetComponentInParent<Paper>();
- currentPaper.TryFold(colsestFolding);
- }
- }
- }
|