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

Resolved How to do drag and drop without right mouse button?

Discussion in 'Scripting' started by Gamadrila, May 5, 2022.

  1. Gamadrila

    Gamadrila

    Joined:
    Sep 5, 2021
    Posts:
    142
    For drag and drop I use the "event trigger" system. How to make drag and drop work only with the left mouse button? This code does not help much if I press both mouse buttons.

    Code (CSharp):
    1. if (!Input.GetMouseButton(1) && Input.GetMouseButton(0))
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Code (CSharp):
    1. public void OnBeginDrag(PointerEventData eventData)
    2.     {
    3.         if (eventData.button == PointerEventData.InputButton.Left)
    4.         {
    5.             //Do stuff
    6.         }
    7.     }
    This is what I did to solve the problem. Basically, it checks if the Left button is what is clicked and will execute the code if it is.

    Note, this is using the IBeginDragHandler, IEndDragHandler, and IDragHandler interfaces.
     
    Gamadrila likes this.
  3. Gamadrila

    Gamadrila

    Joined:
    Sep 5, 2021
    Posts:
    142
    Thanks for your reply. This method worked. It turns out through the "event trigger" to do drag and drop is not very reliable.