123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- 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<DragHandle> currentDragHandles = new List<DragHandle>();
- public UnityEvent<DragHandle> onDragEntry; //进入
- public UnityEvent<DragHandle> onDragExit; //离开
- public UnityEvent<DragHandle> 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<DragHandle>();
- 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);
- }
- /// <summary>
- /// 设置多选数量
- /// </summary>
- /// <param name="number">number == 0 不做任何限制可以附加无限多个,number > 0 任何限制只能附加 number 个,number = -1 不允许附加</param>
- 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();
- }
- }
- }
- }
- /// <summary>
- /// 移除全部DragHandle
- /// </summary>
- public void RemoveAllDragHandles()
- {
- for (int i = 0; i < currentDragHandles.Count; i++)
- {
- temporaryDragHandle = currentDragHandles[i];
- RemoveDragHandle();
- }
- }
- /// <summary>
- /// 移除指定DragHandle
- /// </summary>
- /// <param name="index"></param>
- public void RemoveIndexDragHandles(int index)
- {
- temporaryDragHandle = currentDragHandles[index];
- RemoveDragHandle();
- }
- /// <summary>
- /// 移除指定DragHandle
- /// </summary>
- /// <param name="dragHandle"></param>
- public void RemoveIndexDragHandles(DragHandle dragHandle)
- {
- temporaryDragHandle = dragHandle;
- RemoveDragHandle();
- }
- }
- }
|