SlotHandle.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.EventSystems;
  6. namespace MinorFunction
  7. {
  8. public class SlotHandle : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IDropHandler
  9. {
  10. [HideInInspector]
  11. public bool isEnter = false;
  12. [Tooltip("multipleNumber == 0 不做任何限制可以附加无限多个,multipleNumber > 0 任何限制只能附加 multipleNumber个,multipleNumber = -1 不允许附加")]
  13. public int multipleNumber = 0;
  14. private DragHandle temporaryDragHandle;
  15. public List<DragHandle> currentDragHandles = new List<DragHandle>();
  16. public UnityEvent<DragHandle> onDragEntry; //进入
  17. public UnityEvent<DragHandle> onDragExit; //离开
  18. public UnityEvent<DragHandle> onDragNotExit; //未离开
  19. public UnityEvent onPointerEnter; //鼠标进入
  20. public UnityEvent onPointerExit; //鼠标离开
  21. private void Awake()
  22. {
  23. SetMultipleNumber(multipleNumber);
  24. }
  25. public void OnPointerEnter(PointerEventData eventData)
  26. {
  27. isEnter = true;
  28. onPointerEnter?.Invoke();
  29. }
  30. public void OnPointerExit(PointerEventData eventData)
  31. {
  32. isEnter = false;
  33. onPointerExit?.Invoke();
  34. }
  35. public void OnDrop(PointerEventData eventData)
  36. {
  37. if (eventData.pointerDrag != null)
  38. {
  39. temporaryDragHandle = eventData.pointerDrag.GetComponent<DragHandle>();
  40. if (temporaryDragHandle.isOverlap)
  41. {
  42. return;
  43. }
  44. ModifyDrag(temporaryDragHandle);
  45. }
  46. }
  47. public void ModifyDrag(DragHandle dragHandle)
  48. {
  49. temporaryDragHandle = dragHandle;
  50. if (!currentDragHandles.Contains(temporaryDragHandle))
  51. {
  52. if (multipleNumber == 0) //不做任何限制
  53. {
  54. RemoveDragHandle();
  55. ShiftInDragHandle();
  56. }
  57. else if (multipleNumber > 0)
  58. {
  59. if (currentDragHandles.Count < multipleNumber)
  60. {
  61. RemoveDragHandle();
  62. ShiftInDragHandle();
  63. }
  64. }
  65. else if (multipleNumber < 0)
  66. {
  67. return;
  68. }
  69. }
  70. else
  71. {
  72. onDragNotExit?.Invoke(temporaryDragHandle);
  73. }
  74. }
  75. public void RemoveDragHandle()
  76. {
  77. if (temporaryDragHandle.currentSlotHandle)
  78. {
  79. temporaryDragHandle.RemoveSlotHandle();
  80. }
  81. }
  82. public void ShiftInDragHandle()
  83. {
  84. Debug.Log("添加");
  85. // temporaryDragHandle.currentSlotHandle = this;
  86. // currentDragHandles.Add(temporaryDragHandle);
  87. onDragEntry?.Invoke(temporaryDragHandle);
  88. }
  89. /// <summary>
  90. /// 设置多选数量
  91. /// </summary>
  92. /// <param name="number">number == 0 不做任何限制可以附加无限多个,number > 0 任何限制只能附加 number 个,number = -1 不允许附加</param>
  93. public void SetMultipleNumber(int number)
  94. {
  95. if (number < 0)
  96. {
  97. multipleNumber = -1;
  98. RemoveAllDragHandles();
  99. }
  100. else
  101. {
  102. multipleNumber = number;
  103. if (number < currentDragHandles.Count)
  104. {
  105. for (int i = multipleNumber - 1; i < currentDragHandles.Count; i++)
  106. {
  107. temporaryDragHandle = currentDragHandles[i];
  108. RemoveDragHandle();
  109. }
  110. }
  111. }
  112. }
  113. /// <summary>
  114. /// 移除全部DragHandle
  115. /// </summary>
  116. public void RemoveAllDragHandles()
  117. {
  118. for (int i = 0; i < currentDragHandles.Count; i++)
  119. {
  120. temporaryDragHandle = currentDragHandles[i];
  121. RemoveDragHandle();
  122. }
  123. }
  124. /// <summary>
  125. /// 移除指定DragHandle
  126. /// </summary>
  127. /// <param name="index"></param>
  128. public void RemoveIndexDragHandles(int index)
  129. {
  130. temporaryDragHandle = currentDragHandles[index];
  131. RemoveDragHandle();
  132. }
  133. /// <summary>
  134. /// 移除指定DragHandle
  135. /// </summary>
  136. /// <param name="dragHandle"></param>
  137. public void RemoveIndexDragHandles(DragHandle dragHandle)
  138. {
  139. temporaryDragHandle = dragHandle;
  140. RemoveDragHandle();
  141. }
  142. }
  143. }