using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class BagItem : Dragable { [SerializeField] protected LogicBlock logicElement; protected LogicTrayUI tray; protected RectTransform trayRect; private bool isHovered; public override void OnBeginDrag(PointerEventData eventData) { base.OnBeginDrag(eventData); tray = FindObjectOfType(); if (tray != null) trayRect = (tray.transform as RectTransform); } public override void OnDrag(PointerEventData data) { base.OnDrag(data); DoTrayLogic(data); string output = ""; foreach (GameObject hover in data.hovered) output += hover.name + " "; Debug.Log(output); } protected virtual void DoTrayLogic(PointerEventData data) { if (trayRect == null) return; var rt = m_DraggingIcon.GetComponent(); if (GetWorldSpaceRect(trayRect).Overlaps(GetWorldSpaceRect(rt))) { if (!isHovered) { tray.OnHoverStart(this, rt); isHovered = true; } tray.OnHoverUpdate(this, rt); }else if (isHovered) { isHovered = false; tray.OnHoverEnd(this); } } public override void OnEndDrag(PointerEventData eventData) { base.OnEndDrag(eventData); if (isHovered) { isHovered = false; tray.InsertLogicElement(logicElement); tray.OnHoverEnd(this); } } private void drawRect(RectTransform transform,Color color,bool global = true) { Rect rect = transform.rect; if (global) rect = GetWorldSpaceRect(transform); Debug.DrawLine(new Vector3(rect.min.x,rect.max.y,0), new Vector3(rect.max.x, rect.max.y, 0), color); Debug.DrawLine(new Vector3(rect.max.x, rect.max.y, 0), new Vector3(rect.max.x, rect.min.y, 0), color); Debug.DrawLine(new Vector3(rect.max.x, rect.min.y, 0), new Vector3(rect.min.x, rect.min.y, 0), color); Debug.DrawLine(new Vector3(rect.min.x, rect.min.y, 0), new Vector3(rect.min.x, rect.max.y, 0), color); } Rect GetWorldSpaceRect(RectTransform rt) { var r = rt.rect; Vector2 centre = rt.TransformPoint(r.center); r.size = rt.TransformVector(r.size); r.center = centre; return r; } }