76 lines
2.4 KiB
C#
76 lines
2.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
public class SkillDragItem : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerDownHandler, IPointerUpHandler
|
|
{
|
|
private Image skillIcon; // 현재 오브젝트의 이미지
|
|
private GameObject dragPreview; // 따라다니는 복사 이미지
|
|
private Canvas canvas; // 최상위 캔버스
|
|
private ScrollRect parentScroll;
|
|
|
|
public int m_skillID { get; set; }
|
|
float clickThreshold = 10f;
|
|
Vector2 pressPos;
|
|
|
|
private void Start()
|
|
{
|
|
skillIcon = GetComponent<Image>();
|
|
canvas = GetComponentInParent<Canvas>();
|
|
parentScroll = GetComponentInParent<ScrollRect>();
|
|
}
|
|
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|
{
|
|
parentScroll?.OnBeginDrag(eventData);
|
|
|
|
// 프리뷰 오브젝트 생성
|
|
dragPreview = new GameObject("DragPreview", typeof(CanvasGroup), typeof(Image));
|
|
dragPreview.transform.SetParent(canvas.transform, false);
|
|
dragPreview.transform.SetAsLastSibling(); // 맨 위로
|
|
|
|
Image img = dragPreview.GetComponent<Image>();
|
|
img.sprite = skillIcon.sprite;
|
|
img.raycastTarget = false; // 이벤트 막지 않게
|
|
//img.SetNativeSize(); // 원래 사이즈
|
|
|
|
// 알파 낮춰서 느낌 주기
|
|
dragPreview.GetComponent<CanvasGroup>().alpha = 0.7f;
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
parentScroll?.OnDrag(eventData);
|
|
|
|
if (!DSUtil.CheckNull(dragPreview))
|
|
{
|
|
// 마우스 따라다니게
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(
|
|
canvas.transform as RectTransform,
|
|
eventData.position,
|
|
eventData.pressEventCamera,
|
|
out Vector2 localPoint
|
|
);
|
|
dragPreview.transform.localPosition = localPoint;
|
|
}
|
|
}
|
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
{
|
|
parentScroll?.OnEndDrag(eventData);
|
|
|
|
if (!DSUtil.CheckNull(dragPreview))
|
|
Destroy(dragPreview); // 삭제
|
|
}
|
|
|
|
public void OnPointerDown(PointerEventData eventData)
|
|
{
|
|
pressPos = eventData.position;
|
|
}
|
|
|
|
public void OnPointerUp(PointerEventData eventData)
|
|
{
|
|
if (Vector2.Distance(pressPos, eventData.position) < clickThreshold)
|
|
GetComponentInParent<SkillCard>().OnClick_SkillUI();
|
|
}
|
|
} |