Qemelly(けめる)のプログラム備忘録

Unity / AtCoderについて書きます

Image等のuGUIをドラッグドロップ後に取得したいとき

IEndDragHandlerを実装する。

private Vector2 _startPosition;

public void OnEndDrag(PointerEventData eventData)
{
    transform.position = _startPosition;

    var rayResult = new List<RaycastResult>();
    eventData.position = Input.mousePosition;
    EventSystem.current.RaycastAll(eventData, rayResult);

    if (rayResult.Count == 0)
    {
        Debug.Log("None");
        return;
    }

    foreach (var res in rayResult)
    {
        res.gameObject.TryGetComponent(out IFooInterface foo);
        foo?.DoSomething();
    }
}