In my game, I assigned all of the blocks to be 2D sprites so that they could be easily converted into prefabs:
However, when designing the drag and drop scripts, I noticed that all of the drag and drop handlers such as IBeginDragHandler, IEndDragHandler, IDragHandler, etc. weren't working at all. I later noticed that these were intended to be used by UI Images.
Now I am left wondering what I can do to make these 2D Sprites be usable by these functions for UI Images. Do I have any other options other than to start from scratch by making new UI Images?
Here's the small amount of code needed to create a reproducible example:
Public class DragDrop : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
{
[SerializeField]
private Canvas canvas;
private CanvasGroup canvasGroup;
private RectTransform rectTransform;
private void Awake()
{
rectTransform = GetComponent<RectTransform>();
canvasGroup = GetComponent<CanvasGroup>();
}
public void OnBeginDrag(PointerEventData eventData)
{
canvasGroup.alpha = 0.6f;
canvasGroup.blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData)
{
rectTransform.anchoredPosition += eventData.delta / canvas.scaleFactor;
}
public void OnEndDrag(PointerEventData eventData)
{
canvasGroup.alpha = 1f;
canvasGroup.blocksRaycasts = true;
}
}