85 lines
3.2 KiB
C#
85 lines
3.2 KiB
C#
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class InventoryItemControl : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
|
|
{
|
|
[HideInInspector] public InventorySlot ParentSlot;
|
|
|
|
private Transform _dragTransform;
|
|
private CanvasGroup _canvasGroup;
|
|
|
|
private void Awake()
|
|
{
|
|
_canvasGroup = GetComponent<CanvasGroup>();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
_dragTransform = GameManager.Instance.InGameUI.DragCanvas;
|
|
}
|
|
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|
{
|
|
// 시작할 때 부모 슬롯을 아예 스크립트 통째로 저장!
|
|
ParentSlot = GetComponentInParent<InventorySlot>();
|
|
|
|
if (ParentSlot == null || ParentSlot.currentItem == null)
|
|
{
|
|
// eventData.pointerDrag를 null로 만들면 이후 OnDrag, OnEndDrag도 호출안됨
|
|
eventData.pointerDrag = null;
|
|
return;
|
|
}
|
|
|
|
transform.SetParent(_dragTransform);
|
|
_canvasGroup.blocksRaycasts = false;
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
transform.position = eventData.position; // 마우스 위치 추적
|
|
}
|
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
{
|
|
_canvasGroup.blocksRaycasts = true;
|
|
|
|
// 마우스 아래에 슬롯이 없어서 제자리로 복귀해야 하는 상황인지 판별
|
|
if (transform.parent == _dragTransform)
|
|
{
|
|
// eventData.pointerEnter는 드래그가 끝난 지점에 마우스가 가리키고 있는 오브젝트 (이전까지 _canvasGroup.blocksRaycasts = false; 였으므로 자신 제외)
|
|
// 가르키는 오브젝트가 없거나, UI 레이어가 아니라면 버린것으로 간주
|
|
bool isDroppedOutsideUI = eventData.pointerEnter == null || eventData.pointerEnter.layer != LayerMask.NameToLayer("ControlUI");
|
|
|
|
if (isDroppedOutsideUI)
|
|
{
|
|
// 먼저 원래 부모 슬롯으로 복귀시킴 (드래그 전용 캔버스에 남는것을 방지)
|
|
transform.SetParent(ParentSlot.transform);
|
|
transform.localPosition = Vector3.zero;
|
|
|
|
GameManager.Instance.Inventory.DropItemFromSlot(ParentSlot.SlotIndex);
|
|
}
|
|
else
|
|
{
|
|
// UI 위이긴 한데 (예: 인벤토리 빈 여백, 다른 창) 슬롯이 아니라서 드롭 처리가 안 된 경우 -> 제자리 복귀
|
|
transform.SetParent(ParentSlot.transform);
|
|
transform.localPosition = Vector3.zero;
|
|
}
|
|
}
|
|
|
|
//드래그를 취소했을때 툴팁이 다시 나와야됨
|
|
GameObject overObj = eventData.pointerCurrentRaycast.gameObject;
|
|
if (overObj != null)
|
|
{
|
|
// 만약 마우스 아래에 슬롯이 있다면 그 슬롯의 아이템정보로 툴팁 띄우기
|
|
InventorySlot slot = overObj.GetComponentInParent<InventorySlot>();
|
|
if (slot != null)
|
|
{
|
|
if (slot.currentItem != null && slot.currentItem.Data != null)
|
|
{
|
|
GameManager.Instance.InGameUI.Tooltip.ShowTooltip(slot.currentItem, slot.GetComponent<RectTransform>());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|