| | |
| | | using System;
|
| | | using UnityEngine;
|
| | | using UnityEngine.EventSystems;
|
| | | using System.Collections.Generic;
|
| | |
|
| | | namespace vnxbqy.UI
|
| | | {
|
| | |
| | | public class DragSelectComponentEx : MonoBehaviour
|
| | | {
|
| | | [SerializeField] float m_Sensitive = 10f;
|
| | | [SerializeField] GameObject m_TargetObject;
|
| | |
|
| | | public event Action<int> onDragComplete;
|
| | |
|
| | |
| | | m_StartDrag = true;
|
| | | start_position = Input.mousePosition;
|
| | | isArea = RectTransformUtility.RectangleContainsScreenPoint(this.transform as RectTransform, start_position, CameraManager.uiCamera);
|
| | | if (!IsOverTarget())
|
| | | {
|
| | | m_StartDrag = false;
|
| | | return;
|
| | | }
|
| | | }
|
| | | else if (Input.GetMouseButtonUp(0) && m_StartDrag)
|
| | | {
|
| | |
| | | direction = delta.y > 0 ? 2 : -2; // 2 表示向上滑动,-2 表示向下滑动
|
| | | }
|
| | |
|
| | | if (onDragComplete != null)
|
| | | // 新增目标对象检测条件
|
| | | if (onDragComplete != null && isArea)
|
| | | {
|
| | | if (isArea)
|
| | | {
|
| | | onDragComplete(direction);
|
| | | }
|
| | | onDragComplete(direction);
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | // 新增目标检测方法
|
| | | private bool IsOverTarget()
|
| | | {
|
| | | if (m_TargetObject == null) return true;
|
| | |
|
| | | PointerEventData eventData = new PointerEventData(EventSystem.current)
|
| | | {
|
| | | position = Input.mousePosition
|
| | | };
|
| | |
|
| | | List<RaycastResult> results = new List<RaycastResult>();
|
| | | EventSystem.current.RaycastAll(eventData, results);
|
| | |
|
| | | if (results.Count > 0)
|
| | | {
|
| | | RaycastResult result = results[0];
|
| | | if (result.gameObject == m_TargetObject)
|
| | | {
|
| | | return true;
|
| | | }
|
| | | if (result.gameObject.transform.parent.gameObject == m_TargetObject)
|
| | | {
|
| | | return true;
|
| | | }
|
| | | }
|
| | | return false;
|
| | | }
|
| | |
|
| | | private void OnDisable()
|
| | | {
|
| | | m_StartDrag = false;
|