概要
using UnityEngine;
using UnityEngine.EventSystems;
public class Example :
MonoBehaviour,
IPointerDownHandler,
IDragHandler
{
[SerializeField] private Canvas m_canvas;
private Vector3 m_offset;
void IPointerDownHandler.OnPointerDown( PointerEventData eventData )
{
RectTransformUtility.ScreenPointToLocalPointInRectangle
(
rect: m_canvas.GetComponent<RectTransform>(),
screenPoint: eventData.position,
cam: m_canvas.worldCamera,
localPoint: out var localPoint
);
m_offset = transform.position - m_canvas.transform.TransformPoint( localPoint );
}
void IDragHandler.OnDrag( PointerEventData eventData )
{
RectTransformUtility.ScreenPointToLocalPointInRectangle
(
rect: m_canvas.GetComponent<RectTransform>(),
screenPoint: eventData.position,
cam: m_canvas.worldCamera,
localPoint: out var localPoint
);
transform.position = m_canvas.transform.TransformPoint( localPoint ) + m_offset;
}
}
参考サイト様