You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.3 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. public class BagItem : Dragable
  6. {
  7. [SerializeField]
  8. protected LogicBlock logicElement;
  9. protected LogicTrayUI tray;
  10. protected RectTransform trayRect;
  11. private bool isHovered;
  12. public override void OnBeginDrag(PointerEventData eventData)
  13. {
  14. base.OnBeginDrag(eventData);
  15. tray = FindObjectOfType<LogicTrayUI>();
  16. if (tray != null)
  17. trayRect = (tray.transform as RectTransform);
  18. }
  19. public override void OnDrag(PointerEventData data)
  20. {
  21. base.OnDrag(data);
  22. DoTrayLogic(data);
  23. }
  24. protected virtual void DoTrayLogic(PointerEventData data)
  25. {
  26. if (trayRect == null)
  27. return;
  28. var rt = m_DraggingIcon.GetComponent<RectTransform>();
  29. if (GetWorldSpaceRect(trayRect).Overlaps(GetWorldSpaceRect(rt)))
  30. {
  31. if (!isHovered)
  32. {
  33. tray.OnHoverStart(this, rt);
  34. isHovered = true;
  35. }
  36. tray.OnHoverUpdate(this, rt);
  37. }else if (isHovered)
  38. {
  39. isHovered = false;
  40. tray.OnHoverEnd(this, rt);
  41. }
  42. }
  43. public override void OnEndDrag(PointerEventData eventData)
  44. {
  45. base.OnEndDrag(eventData);
  46. if (isHovered)
  47. {
  48. isHovered = false;
  49. //tray.OnHoverEnd(this, rt);
  50. }
  51. }
  52. private void drawRect(RectTransform transform,Color color,bool global = true)
  53. {
  54. Rect rect = transform.rect;
  55. if (global)
  56. rect = GetWorldSpaceRect(transform);
  57. Debug.DrawLine(new Vector3(rect.min.x,rect.max.y,0), new Vector3(rect.max.x, rect.max.y, 0), color);
  58. Debug.DrawLine(new Vector3(rect.max.x, rect.max.y, 0), new Vector3(rect.max.x, rect.min.y, 0), color);
  59. Debug.DrawLine(new Vector3(rect.max.x, rect.min.y, 0), new Vector3(rect.min.x, rect.min.y, 0), color);
  60. Debug.DrawLine(new Vector3(rect.min.x, rect.min.y, 0), new Vector3(rect.min.x, rect.max.y, 0), color);
  61. }
  62. Rect GetWorldSpaceRect(RectTransform rt)
  63. {
  64. var r = rt.rect;
  65. Vector2 centre = rt.TransformPoint(r.center);
  66. r.size = rt.TransformVector(r.size);
  67. r.center = centre;
  68. return r;
  69. }
  70. }