Не работает OnDrag

У меня есть канвас, на котором находится инвентарь и еще пару элементов. Инвентарь заполняется при подборе предмета, и с помощью OnDrag я хочу их перетаскивать. Но после добавления интерфейса он не реагирует на нажатие. При чем остальные UI типа лейблов реагируют нормально (кроме слайдера). EventSystem есть на сцене, GraphicRaycaster на канвасе есть, на картинках на предметах в инвентаре галочка Raycast target стоит. В чем может быть проблема?

public class ItemDragger : MonoBehaviour, IDragHandler, IEndDragHandler, IBeginDragHandler
{
private GraphicRaycaster _raycaster;
private PointerEventData _pointerEventData;
private EventSystem _eventSystem;

private InventoryCell _draggingCell;

private void Awake()
{
    _raycaster = GetComponent<GraphicRaycaster>();
    _eventSystem = GetComponent<EventSystem>();
}
public void OnBeginDrag(PointerEventData eventData)
{
    Debug.Log(GetInventoryCell().Item.Name);
}
public void OnDrag(PointerEventData eventData)
{
                    
}
public void OnEndDrag(PointerEventData eventData)
{
            
}
private InventoryCell GetInventoryCell()
{
    InventoryCell inventoryCell = null;
    List<RaycastResult> results = new List<RaycastResult>();

    _pointerEventData = new PointerEventData(_eventSystem);
    _pointerEventData.position = Input.mousePosition;

    _raycaster.Raycast(_pointerEventData, results);

    results.ForEach(result =>
    {
        if (result.gameObject.TryGetComponent<InventoryCell>(out InventoryCell 
        cell))
        inventoryCell = cell;
    });

    return inventoryCell;
    }
}

копоненты на канвас

иерархия канваса

иерархия префаба

все обьекты в иерархии имеют картинку с одинаковым компонентом image

EventSystem


Ответы (1 шт):

Автор решения: Yaroslav
[DisallowMultipleComponent]

public class CellDrag : MonoBehaviour {

    public static CellDrag Main; 

    private void Awake () {
        CellDrag = this;
    }

    public void SetDelegates (Cell cell) {
        cell.DragBegining = DragBegining;
        cell.Draged = Draged;
        cell.DragEnding = DragEnding;
    }

    private void DragBegining (Cell cell, Vector2 position) {
        
    }

    private void Draged (Cell cell, Vector2 position) {
        
    }

    private void DragEnding (Cell cell, Vector2 position) {
        
    }
}

[DisallowMultipleComponent]

public class Cell : MonoBehaviour, IDragHandler, IEndDragHandler, IBeginDragHandler {

    public void Action<Ceil, Vector2> DragBegining;
    public void Action<Ceil, Vector2> Draged;
    public void Action<Ceil, Vector2> DragEnding;
    [SerializeField] private Image _icon;

    public void SetInteractable (bool interactable) {
        if (_icon != null)
            _icon.raycastTarget = interactable;
    }

    private void Start () {
        if (CellDrag.Main != null)
            CellDrag.Main.SetDelegates(this);
    }

    public void OnBeginDrag (PointerEventData eventData) {
        Debug.Log("OnBeginDrag");
        if (DragBegining != null)
            DragBegining(this, eventData.position);
    }

    public void OnDrag (PointerEventData eventData) {
        Debug.Log("OnDrag");
        if (Draged != null)
            Draged(this, eventData.position);
    }

    public void OnEndDrag (PointerEventData eventData) {
        Debug.Log("OnEndDrag");
        if (DragEnding != null)
            DragEnding(this, eventData.position);
    }
}
→ Ссылка