using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; namespace MinorFunction { public class SlotHandle : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IDropHandler { [HideInInspector] public bool isEnter = false; [Tooltip("multipleNumber == 0 不做任何限制可以附加无限多个,multipleNumber > 0 任何限制只能附加 multipleNumber个,multipleNumber = -1 不允许附加")] public int multipleNumber = 0; private DragHandle temporaryDragHandle; public List currentDragHandles = new List(); public UnityEvent onDragEntry; //进入 public UnityEvent onDragExit; //离开 public UnityEvent onDragNotExit; //未离开 public UnityEvent onPointerEnter; //鼠标进入 public UnityEvent onPointerExit; //鼠标离开 private void Awake() { SetMultipleNumber(multipleNumber); } public void OnPointerEnter(PointerEventData eventData) { isEnter = true; onPointerEnter?.Invoke(); } public void OnPointerExit(PointerEventData eventData) { isEnter = false; onPointerExit?.Invoke(); } public void OnDrop(PointerEventData eventData) { if (eventData.pointerDrag != null) { temporaryDragHandle = eventData.pointerDrag.GetComponent(); if (temporaryDragHandle.isOverlap) { return; } ModifyDrag(temporaryDragHandle); } } public void ModifyDrag(DragHandle dragHandle) { temporaryDragHandle = dragHandle; if (!currentDragHandles.Contains(temporaryDragHandle)) { if (multipleNumber == 0) //不做任何限制 { RemoveDragHandle(); ShiftInDragHandle(); } else if (multipleNumber > 0) { if (currentDragHandles.Count < multipleNumber) { RemoveDragHandle(); ShiftInDragHandle(); } } else if (multipleNumber < 0) { return; } } else { onDragNotExit?.Invoke(temporaryDragHandle); } } public void RemoveDragHandle() { if (temporaryDragHandle.currentSlotHandle) { temporaryDragHandle.RemoveSlotHandle(); } } public void ShiftInDragHandle() { Debug.Log("添加"); // temporaryDragHandle.currentSlotHandle = this; // currentDragHandles.Add(temporaryDragHandle); onDragEntry?.Invoke(temporaryDragHandle); } /// /// 设置多选数量 /// /// number == 0 不做任何限制可以附加无限多个,number > 0 任何限制只能附加 number 个,number = -1 不允许附加 public void SetMultipleNumber(int number) { if (number < 0) { multipleNumber = -1; RemoveAllDragHandles(); } else { multipleNumber = number; if (number < currentDragHandles.Count) { for (int i = multipleNumber - 1; i < currentDragHandles.Count; i++) { temporaryDragHandle = currentDragHandles[i]; RemoveDragHandle(); } } } } /// /// 移除全部DragHandle /// public void RemoveAllDragHandles() { for (int i = 0; i < currentDragHandles.Count; i++) { temporaryDragHandle = currentDragHandles[i]; RemoveDragHandle(); } } /// /// 移除指定DragHandle /// /// public void RemoveIndexDragHandles(int index) { temporaryDragHandle = currentDragHandles[index]; RemoveDragHandle(); } /// /// 移除指定DragHandle /// /// public void RemoveIndexDragHandles(DragHandle dragHandle) { temporaryDragHandle = dragHandle; RemoveDragHandle(); } } }