Question by
rraallvv · Nov 13, 2016 at 06:07 PM ·
guieditorwindow
How to pan/scale the contents within a GUI area?
I want to have a zoom effect within a specific area in a EditorWindow, something like a zoomable scrollview.
The folowing snippet does the job for scaling the contents but I can't find a way to handle independently the new clipping rect and the contents origin.
public class EditorZoomArea
{
private static float kEditorWindowTabHeight = 20;
private static Matrix4x4 _prevGuiMatrix;
//Starts the zoomable area
public static Rect Begin(float zoomScale, Rect zoomRect, Vector2 zoomOrigin)
{
GUI.EndGroup(); //End the group that Unity began so we're not bound by the EditorWindow
zoomRect.y += kEditorWindowTabHeight; //Account for the window tab
zoomRect.height -= kEditorWindowTabHeight + 3; //Account for the window tab
Rect clippedArea = zoomRect.ScaleSizeBy(1.0f / zoomScale, zoomRect.center);
//GUI.BeginGroup(clippedArea);
GUI.BeginGroup(new Rect(0.0f, kEditorWindowTabHeight, Screen.width, Screen.height - (kEditorWindowTabHeight + 3)));
_prevGuiMatrix = GUI.matrix;
//Perform scaling
Matrix4x4 translation = Matrix4x4.TRS(-zoomOrigin, Quaternion.identity, Vector3.one);
Matrix4x4 scale = Matrix4x4.Scale(new Vector3(zoomScale, zoomScale, 1.0f));
GUI.matrix = scale * translation;
return clippedArea;
}
//Ends the zoomable area
public static void End()
{
GUI.matrix = _prevGuiMatrix;
GUI.EndGroup();
GUI.BeginGroup(new Rect(0.0f, kEditorWindowTabHeight, Screen.width, Screen.height - (kEditorWindowTabHeight + 3)));
}
}
Is there a way to do this, maybe using a scroll view or two nested clipping rects?
Comment
Your answer