| | |
| | | using System;
|
| | | using UnityEngine;
|
| | | using UnityEngine.EventSystems;
|
| | | using System.Collections.Generic;
|
| | |
|
| | | namespace vnxbqy.UI
|
| | | {
|
| | | [DisallowMultipleComponent]
|
| | | public class DragSelectComponentEx : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
|
| | | public class DragSelectComponentEx : MonoBehaviour
|
| | | {
|
| | | [SerializeField] float m_Sensitive = 10f;
|
| | | [SerializeField] GameObject m_TargetObject;
|
| | |
| | | Vector3 start_position = Vector3.zero;
|
| | | bool isArea = false;
|
| | |
|
| | | private void OnDisable()
|
| | | {
|
| | | m_StartDrag = false;
|
| | | }
|
| | |
|
| | | public void OnPointerDown(PointerEventData eventData)
|
| | | private void LateUpdate()
|
| | | {
|
| | | if (Input.touchCount > 1)
|
| | | {
|
| | | m_StartDrag = false;
|
| | | return;
|
| | | }
|
| | |
|
| | | if (Input.GetMouseButtonDown(0))
|
| | | {
|
| | | m_StartDrag = true;
|
| | | start_position = Input.mousePosition;
|
| | | isArea = RectTransformUtility.RectangleContainsScreenPoint(this.transform as RectTransform, start_position, CameraManager.uiCamera);
|
| | | if (!IsOverTarget())
|
| | | {
|
| | | m_StartDrag = false;
|
| | | return;
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | public void OnPointerUp(PointerEventData eventData)
|
| | | {
|
| | | if (Input.GetMouseButtonUp(0) && m_StartDrag)
|
| | | else if (Input.GetMouseButtonUp(0) && m_StartDrag)
|
| | | {
|
| | | var delta = Input.mousePosition - start_position;
|
| | | m_StartDrag = false;
|
| | |
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | // 新增目标检测方法
|
| | | 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;
|
| | | }
|
| | | }
|
| | | } |