- Home /
Question by
DrCubeMan · Jun 19, 2020 at 10:29 AM ·
editormouseeditorwindowvector2editor window
Correct behavior of the mouse wheel when zooming in a 2D area (Unity Editor)
Hi, i am programming a graph (like the shader graph) in UnityEditor and wanted to do the zooming with the mouse.
There is a Vector2D offset so you can move around in the graph. There is a float scale that holds the current scale and goes from 10f to 200f.
Nodes for example are displayed like this:
public void Draw(Vector2 offset, GUIStyle fontSizeNode, float scale)
{
float x = positionNode.x;
float y = positionNode.y;
//Draw Node
GUI.DrawTexture(ScaleRect(x, y, width, 32f, scale, offset), nodeTopStyle.normal.background);
y += 32f ;
GUI.DrawTexture(ScaleRect(x, y, width, 110f, scale, offset), nodeMidStyle.normal.background);
y += 110f;
GUI.DrawTexture(ScaleRect(x, y, width, 29f, scale, offset), nodeBottomStyle.normal.background);
}
Rect ScaleRect(float x, float y, float width, float hight, float scale, Vector2 offset)
{
x = x / 100f * scale + offset.x;
y = y / 100f * scale + offset.y;
width = width / 100f * scale;
hight = hight / 100f * scale;
return new Rect(x, y, width, hight);
}
So if I use the mouse wheel, I should zoom to the position where the mouse is. Actually the offset should only be adjusted slightly but it is kind of difficult. Does anyone have an idea?
if (e.type == EventType.ScrollWheel)
{
if (e.delta.y < 0)
{
// offset += e.mousePosition / 100f * (100f - scale) ;
scale += 5f;
Repaint();
}
else
{
if (scale <= 11f)
return;
scale -= 5f;
Repaint();
}
}
Comment