UIDragItem.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using UnityEngine;
  2. using System.Collections;
  3. using LuaInterface;
  4. public class UIDragItem : MonoBehaviour
  5. {
  6. public UnityEngine.Events.UnityAction onBeginDrag;
  7. public UnityEngine.Events.UnityAction onEndDrag;
  8. public RectTransform RT
  9. {
  10. get { return rt; }
  11. }
  12. public int slotIdx = 0;
  13. UIEventTriggerListener triggerListener = null;
  14. RectTransform parentRT = null;
  15. RectTransform rt = null;
  16. RectTransform mCursor;
  17. private void Start()
  18. {
  19. if (this.transform.parent != null)
  20. {
  21. parentRT = this.transform.parent.GetComponent<RectTransform>();
  22. }
  23. rt = GetComponent<RectTransform>();
  24. triggerListener = UIEventTriggerListener.Get(this.gameObject);
  25. triggerListener.onBeginDrag = OnBeginDrag;
  26. triggerListener.onDrag = OnDrag;
  27. triggerListener.onEndDrag = OnEndDrag;
  28. triggerListener.onDrop = OnDrop;
  29. this.lastSlotIdx = slotIdx;
  30. }
  31. void OnBeginDrag()
  32. {
  33. if (onBeginDrag != null)
  34. onBeginDrag();
  35. }
  36. void OnDrag()
  37. {
  38. if (parentRT != null && mCursor != null)
  39. {
  40. Vector2 result = UIEventTriggerListener.ScreenPointToLocalPointInRectangle(parentRT, UIEventTriggerListener.currentEventData);
  41. mCursor.anchoredPosition = result;
  42. }
  43. }
  44. void OnEndDrag()
  45. {
  46. if (onEndDrag != null)
  47. onEndDrag();
  48. rt.anchoredPosition = Vector2.zero;
  49. DebugHelper.LogError("lastSlotIdx:"+lastSlotIdx + " curSlotIdx:"+slotIdx);
  50. }
  51. void OnDrop()
  52. {
  53. GameObject dragObj = UIEventTriggerListener.currentEventData.pointerDrag;
  54. UIDragItem dragItem = dragObj.GetComponent<UIDragItem>();
  55. int temp = dragItem.slotIdx;
  56. dragItem.SetSkillSlot(this.slotIdx);
  57. this.slotIdx = temp;
  58. }
  59. public void RefreshParent()
  60. {
  61. parentRT = this.transform.parent.GetComponent<RectTransform>();
  62. }
  63. int lastSlotIdx = 0;
  64. public int LastSlotIdx
  65. {
  66. get { return lastSlotIdx; }
  67. }
  68. public void SetSkillSlot(int idx)
  69. {
  70. lastSlotIdx = this.slotIdx;
  71. this.slotIdx = idx;
  72. }
  73. public void Reset(int idx)
  74. {
  75. this.lastSlotIdx = idx;
  76. this.slotIdx = idx;
  77. }
  78. public void SetMoveItem(RectTransform cursorRT)
  79. {
  80. if (cursorRT == null) return;
  81. mCursor = cursorRT;
  82. cursorRT.SetParent(this.parentRT);
  83. cursorRT.anchoredPosition = Vector2.zero;
  84. cursorRT.gameObject.SetActive(true);
  85. }
  86. }