0
\$\begingroup\$

In my game, I assigned all of the blocks to be 2D sprites so that they could be easily converted into prefabs: enter image description here

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;
    }
}
\$\endgroup\$
2
\$\begingroup\$

Make sure that:

  1. You have a Collider2D(BoxCollider2D etc.) added.
  2. Physics2DRaycaster added to your camera.
  3. EventSystem in your scene.
  4. Nothing is blocking the Raycast to this SpriteRenderer.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

[RequireComponent(typeof(Collider2D))]
public class DraggableSpriteRenderer : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
{
    [SerializeField] private SpriteRenderer _spriteRenderer;
    public SpriteRenderer _SpriteRenderer => this._spriteRenderer;

    public void OnDrag(PointerEventData eventData)
    {
        Debug.Log("OnDrag");

        Vector3 movement = new Vector3(
            x: eventData.delta.x * Time.deltaTime,
            y: eventData.delta.y * Time.deltaTime
        );

        this.transform.localPosition += movement;
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        Debug.Log("OnBeginDrag");
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        Debug.Log("OnEndDrag");
    }

#if UNITY_EDITOR
    private void Reset()
    {
        this._spriteRenderer = this.GetComponent<SpriteRenderer>();
    }
#endif
}

IDragHandler, IBeginDragHandler, IEndDragHandler

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ How can a raycast be blocked? \$\endgroup\$ – kayra yorulmaz Jul 31 at 10:19
  • \$\begingroup\$ @kayrayorulmaz have you tried all of the steps before this? It can be blocked if there are objects in front of it that also receive raycast hit. \$\endgroup\$ – Candid Moon _Max_ Jul 31 at 10:23
  • \$\begingroup\$ I have placed all of the intractable objects on layer 1, which is a layer higher than any other object in the scene. However, the blocks still don't move at all. All of the previous steps have also been taken. \$\endgroup\$ – kayra yorulmaz Jul 31 at 10:32
  • \$\begingroup\$ @kayrayorulmaz do you have SpriteRenderers inside Canvas? I am trying to reproduce this scenario. \$\endgroup\$ – Candid Moon _Max_ Jul 31 at 10:49
  • \$\begingroup\$ @kayrayorulmaz I have tried to recreate it, so there are 3 scenarios. 1. It's just an object with Transform - works fine. 2. It's an object with Transform but inside Canvas - works fine. 3. It's a SpriteRenderer with RectTransform inside Canvas - it doesn't draw anything because Canvas doesn't recognise SpriteRenderer. After adding Image - works fine. I have posted an edit with my code that I have used for testing this. \$\endgroup\$ – Candid Moon _Max_ Jul 31 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.