Search Unity

Resolved Drag and Drop on Display 2

Discussion in 'Scripting' started by unitynoob24, Jan 17, 2022.

  1. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
    Hey guys,

    I'm currently working on a pretty straight forward multi display application. Basically there are two screens, the main screen (display 1) is the main experience. It has its own camera and canvas. Then I have another camera with target display set to display 2, as well as a canvas set to screen space - overlay; although I have also tried using a screen space - camera canvas as well.

    I have it working with either of those canvas types perfectly fine within the editor. But once I do a build to test, the drag and drop doesn't seem to work properly. Basically, when I click a draggable object to drag it, it just vanishes from the screen. When I inspect the X and Y of the object I'm dragging, it seems like it is taking into account the size of both screens together? Do I need to be subtracting the main canvas / screen size or something from my positioning logic?

    This is all I'm doing on drag:
    Code (csharp):
    1.  
    2. public void OnDrag(PointerEventData eventData){
    3.             transform.position = eventData.position;
    4.             vectorOutputTMP.text = transform.position.ToString();
    5. }
    6.  
    Thanks guys!

    EDIT: solved! I overlooked this helper method on the Display class, and it is doing exactly what I need for this.
    Code (csharp):
    1.  
    2. transform.position = Display.RelativeMouseAt(eventData.position);
    3.  
     
    Last edited: Jan 17, 2022