Editor window pan and zoom

Hello!

How would you go about to achieve this effect in editor window with the gui?
Link to gif (https://i.gyazo.com/9dcdaf5087923a9a1ed9f3c72e807853.gif)

Use the IPointerDownHandler and IDragHandler interfaces on a script attached to the window that you want to drag. These interfaces will enable callbacks to OnPointerDown() and OnDrag() respectively. In OnPointerDown() you should read the current window position and then in OnDrag() update the position of the window, based on its original position, by the amount that the mouse moves. Here is the framework of what you will have:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;

public class DragPanel : MonoBehaviour, IPointerDownHandler, IDragHandler
{
    private RectTransform canvasRectTransform;
    private RectTransform panelRectTransform;
    private Vector2 LastCursorPosition;

    void Awake ()
    {
        // Get the transforms of the window here so you can access the window position
    }

    public void OnPointerDown (PointerEventData data) 
    {
        // Get and save the initial window position
    }

    public void OnDrag (PointerEventData data)
    {
         // Update the window position based on movement of the mouse
    }
}
2 Likes

I hope we are thinking the same thing as I meant draging around a texture inside a window and not draging the window itself.

Martin Ecker wrote a good article (with code) on zooming and panning editor windows: Unity Editor Window Zooming.

1 Like

Thank you.