Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Start a drag on forward to another UI Image

Discussion in 'Editor & General Support' started by krupps, Nov 26, 2019.

  1. krupps

    krupps

    Joined:
    Oct 17, 2017
    Posts:
    159
    I have a UI Image and I have a UISelected object ( UI Image ). I want to move the SelectedImage object and forward the drag to it.

    Image has this Script:
    Code (CSharp):
    1.    
    2.     public Image SelectedImage;
    3.  
    4.     public void OnBeginDrag(PointerEventData eventData)
    5.     {
    6.         Debug.Log("Start Drag");
    7.  
    8.         var draggable = eventData.selectedObject.GetComponent<Image>();
    9.  
    10.         SelectedImage.sprite = draggable.sprite;
    11.  
    12.         SelectedImage.transform.position += (Vector3)eventData.delta;
    13.     }
    And the Selected Image has this script
    Code (CSharp):
    1.     public void OnDrag(PointerEventData eventData)
    2.     {
    3.  
    4.         this.transform.position += (Vector3)eventData.delta;
    5.     }
    6.     public void OnDrop(PointerEventData eventData)
    7.     {
    8.         Debug.Log("Dropped!");
    9.  
    10.  
    11.     }
    I thought I could grab the BeginDrag, place the SelectedImage item on top and the user will start dragging since the image is below the mouse.

    Basically, I want to allow the user to drag 3 heroes into a Panel to use for the level. Or is there a way to clone the object and set him has the drag when a user drags from the original Image object?