Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to achieve zoom functionality on an editor window using UI Toolkit?

Discussion in 'UI Toolkit' started by AbsoluteTundra, Apr 15, 2020.

  1. AbsoluteTundra

    AbsoluteTundra

    Joined:
    Sep 25, 2013
    Posts:
    7
    Hey All,
    How would I achieve the ability to allow users to zoom in & out for my node-based editor similar to ShaderGraph?

    I was thinking about changing the scale of every element in my root visual element hierarchy but there might be a better approach I'm not aware of.

    Thanks for the help!
     
  2. unity_oFPBwvdIXjEL4w

    unity_oFPBwvdIXjEL4w

    Joined:
    Dec 18, 2017
    Posts:
    1
    You can achieve a global zoom by setting your root element's transform.scale property directly. This will affect the scale of all of its children. For example:

    public class ZoomableWindow : EditorWindow
    {
    [MenuItem("Window/ZoomableWindow")]
    static void Init()
    {
    GetWindow(typeof(ZoomableWindow)).Show();
    }

    void Awake()
    {
    var root = new VisualElement();
    var resetButton = new Button {text = "Reset Zoom"};
    resetButton.RegisterCallback<ClickEvent>(ev => root.transform.scale = Vector3.one);
    var zoomButton = new Button {text = "Zoom x 1.5"};
    zoomButton.RegisterCallback<ClickEvent>(ev => root.transform.scale *= 1.5f);
    root.Add(resetButton);
    root.Add(zoomButton);
    rootVisualElement.Add(root);
    }
    }
     
  3. AbsoluteTundra

    AbsoluteTundra

    Joined:
    Sep 25, 2013
    Posts:
    7
    Yeah that worked for me thanks!