using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; namespace LoyalSoftSDK { public class DragUtils : MonoBehaviour,IBeginDragHandler, IDragHandler, IEndDragHandler,IPointerDownHandler,IPointerUpHandler { private RectTransform rectTransform; private RectTransform cavasRectTransform; private Vector2 starPoint; private Vector2 endPoint; private bool isClick=true; public delegate void EndDragHandler(); public EndDragHandler OnEndDragHandler; public delegate void ClickHandler(); public ClickHandler OnClickHandler; public delegate void BeginDragHandler(); public BeginDragHandler OnBeginDragHandler; void Start() { rectTransform = this.gameObject.transform.parent.GetComponent(); cavasRectTransform = UIFormManager.GetInstance().canvasTransform.gameObject.GetComponent(); } public void OnBeginDrag(PointerEventData eventData) { //Debug.Log(eventData.position); if(OnBeginDragHandler!=null) { OnBeginDragHandler.Invoke(); } isClick = false; //Debug.Log("拖拽开始"); } public void OnDrag(PointerEventData eventData) { Vector3 globalMousePos; if (RectTransformUtility.ScreenPointToWorldPointInRectangle (cavasRectTransform, eventData.position, eventData.pressEventCamera, out globalMousePos)) { rectTransform.position = globalMousePos; rectTransform.rotation = cavasRectTransform.rotation; } } public void OnEndDrag(PointerEventData eventData) { //Debug.Log(eventData.position); //Debug.Log("拖拽停止"); if (OnEndDragHandler != null) { OnEndDragHandler.Invoke(); } } public void OnPointerDown(PointerEventData eventData) { //Debug.Log("按下"); // starPoint = eventData.position; isClick = true; } public void OnPointerUp(PointerEventData eventData) { //Debug.Log("抬起"); //endPoint = eventData.position; //float dragDistance = Mathf.Pow(endPoint.x - starPoint.x, 2) + Mathf.Pow(endPoint.y - starPoint.y, 2); //if(dragDistance<=100) //{ //} if(isClick) { if (OnClickHandler != null) { OnClickHandler.Invoke(); } } } // Use this for initialization // Update is called once per frame void Update() { } } }