using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; public enum ItemType { ChuanGanQi, SheXiang, ZHLuDeng, LeiDa, } public class DragItem : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler { public ItemType itemType; [Header("鼠标按下事件")] public UnityEvent _Downevent; [Header("鼠标松开事件")] public UnityEvent _UpEvent; [Header("鼠标拖拽事件")] public UnityEvent _DragEvent; private bool canDrag = false; private Vector3 tempPos; private Vector3 resetPos; private void OnEnable() { resetPos = transform.localPosition; // _UpEvent.AddListener(CheckDragAear); } public void OnDrag(PointerEventData eventData) { if (canDrag) { _DragEvent.Invoke(); this.transform.position = Input.mousePosition; } } public void OnPointerDown(PointerEventData eventData) { canDrag = true; _Downevent.Invoke(); //DragCheck[] checks = GameObject.FindObjectsOfType(); //for (int i = 0; i < checks.Length; i++) //{ // checks[i].Show(); //} } public void OnPointerUp(PointerEventData eventData) { canDrag = false; tempPos = transform.position; transform.localPosition = resetPos; CheckDragAear(); _UpEvent.Invoke(); //DragCheck[] checks = GameObject.FindObjectsOfType(); //for (int i = 0; i < checks.Length; i++) //{ // checks[i].Hide(); //} } private void CheckDragAear() { DragCheck[] checks = GameObject.FindObjectsOfType(); for (int i = 0; i < checks.Length; i++) { Vector3 checkPos= CameraManager.Instance.MainCamera.WorldToScreenPoint(checks[i].transform.position); if (Vector2.Distance(this.tempPos, checkPos) <= 60) { //放置 checks[i].ShowItem(this.itemType); } } } }