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

Moving scrollview by mousedrag

Discussion in 'Scripting' started by Runemark, Aug 7, 2014.

  1. Runemark

    Runemark

    Joined:
    May 23, 2013
    Posts:
    244
    I was working on my node based editor extension. I really wanted to move my workspace by mouse drag, something like in the mecanim animator. Since I didn't find a complete solution, I decided to share my script with you, maybe somebody needs it. :)

    Code (CSharp):
    1.  
    2.     Vector2 _scrollPosition = Vector2.zero;
    3.     Vector2 _lastMousePos = new Vector2(-999f, -999f);
    4.  
    5.     void OnGUI ()
    6.     {
    7.         Rect scrollViewRect = new Rect(0,0, 400, 400);
    8.  
    9.         // Scroll view
    10.         _scrollPosition = GUI.BeginScrollView(scrollViewRect, _scrollPosition, new Rect(0, 0, 2000, 2000));
    11.             /* Your code here */
    12.         GUI.EndScrollView();
    13.        
    14.         /* Move by mouse drag */
    15.  
    16.         // Check if the mouse is above our scrollview.
    17.         if (scrollViewRect.Contains(Event.current.mousePosition))
    18.         {
    19.             // Only move if we are hold down the middle (wheel) mouse button, and the mouse is moving.
    20.             if (Event.current.button == 2 && Event.current.type == EventType.mouseDrag)
    21.             {
    22.                 // Current position
    23.                 Vector2 currPos = Event.current.mousePosition;
    24.  
    25.                 // Only move if the distance between the last mouse position and the current is less than 50.
    26.                 // Without this it jumps during the drag.
    27.                 if(Vector2.Distance(currPos, _lastMousePos) < 50)
    28.                 {
    29.                     // Calculate the delta x and y.
    30.                     float x = _lastMousePos.x - currPos.x;
    31.                     float y = _lastMousePos.y - currPos.y;
    32.  
    33.                     // Add the delta moves to the scroll position.
    34.                     _scrollPosition.x += x;
    35.                     _scrollPosition.y += y;
    36.                     Event.current.Use();
    37.                 }
    38.                 // Set the last mouse position to the current mouse position.
    39.                 _lastMousePos = currPos;
    40.             }          
    41.         }
    42.     }
     
    liaochenhan and xgalia like this.
  2. xgalia

    xgalia

    Joined:
    May 19, 2014
    Posts:
    2
    Good, exactly what I needed for the class diagram tool I make. Still polishing it ..
     

    Attached Files:

    softrare likes this.
  3. starfiend

    starfiend

    Joined:
    Sep 4, 2016
    Posts:
    2
    I've improved on the OP's code:

    Code (CSharp):
    1. if(scrollViewRect.Contains(Event.current.mousePosition)){
    2.     if(Event.current.button == 2 && Event.current.type == EventType.MouseDrag){
    3.         _scrollPosition += -Event.current.delta;
    4.         Event.current.Use();
    5.     }
    6. }
    The former way it was presented, the ScrollView would jump around if you moved the mouse too fast, or if you clicked a small distance away from your previous click. There's also no need to calculate your own delta. Removing these factors makes it a lot cleaner and without any jumping around.
     
    PatrickReynolds likes this.