- Home /
Zooming and scrolling into GUI
Hi there.
I have a big problem and don't know how to solve it. Maybe some1 can help me out here.
Basically what I want is to zoom into a GUI texture and being able to scroll to the left, right up and down. The problem is, that when I'm "zoomed in", I am able to scroll outside of the image and see the black background. Is there a way, to limit the scrolling to a specific point?
Here is what I am doing code wise:
void FixedUpdate()
{
if (Input.touches.Length == 1 && Input.GetTouch(0).phase == TouchPhase.Began)
{
pos.x = Input.touches[0].position.x;
pos.y = Input.touches[0].position.y;
}
if (Input.touches.Length == 2 && Input.GetTouch(1).phase == TouchPhase.Began)
{
pos.y = Input.touches[1].position.y;
}
//move
if (Input.touches.Length == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
if (Input.touches[0].position.x > pos.x)
{
if (scale.x == 1.2f && matrixTransform.x < 91)
matrixTransform += new Vector3(Input.touches[0].deltaPosition.x / 13, 0, 0);
if (scale.x >= 1.4f && matrixTransform.x < 166)
matrixTransform += new Vector3(Input.touches[0].deltaPosition.x / 13, 0, 0);
if (scale.x >= 1.6f && matrixTransform.x < 241)
matrixTransform += new Vector3(Input.touches[0].deltaPosition.x / 13, 0, 0);
if (scale.x >= 1.8f && matrixTransform.x < 316)
matrixTransform += new Vector3(Input.touches[0].deltaPosition.x / 13, 0, 0);
if (scale.x >= 2.0f && matrixTransform.x < 406)
matrixTransform += new Vector3(Input.touches[0].deltaPosition.x / 13, 0, 0);
}
if (Input.touches[0].position.x < pos.x)
{
if (scale.x == 1.2f && matrixTransform.x > -89)
matrixTransform += new Vector3(Input.touches[0].deltaPosition.x / 13, 0, 0);
if (scale.x >= 1.4f && matrixTransform.x > -164)
matrixTransform += new Vector3(Input.touches[0].deltaPosition.x / 13, 0, 0);
if (scale.x >= 1.6f && matrixTransform.x > -259)
matrixTransform += new Vector3(Input.touches[0].deltaPosition.x / 13, 0, 0);
if (scale.x >= 1.8f && matrixTransform.x > -329)
matrixTransform += new Vector3(Input.touches[0].deltaPosition.x / 13, 0, 0);
if (scale.x >= 2.0f && matrixTransform.x > -404)
matrixTransform += new Vector3(Input.touches[0].deltaPosition.x / 13, 0, 0);
}
if (Input.GetTouch(0).position.y < pos.y)
{
if (scale.x == 1.2f && matrixTransform.y < 46)
matrixTransform += new Vector3(0, 2, 0);
if (scale.x >= 1.4f && matrixTransform.y < 91)
matrixTransform += new Vector3(0, 2, 0);
if (scale.x >= 1.6f && matrixTransform.y < 151)
matrixTransform += new Vector3(0, 2, 0);
if (scale.x >= 1.8f && matrixTransform.y < 196)
matrixTransform += new Vector3(0, 2, 0);
if (scale.x >= 2.0f && matrixTransform.y < 241)
matrixTransform += new Vector3(0, 2, 0);
}
if (Input.GetTouch(0).position.y > pos.y)
{
if (scale.x >= 1.2f && matrixTransform.y > -59)
matrixTransform -= new Vector3(0, 2, 0);
if (scale.x >= 1.4f && matrixTransform.y > -104)
matrixTransform -= new Vector3(0, 2, 0);
if (scale.x >= 1.6f && matrixTransform.y > -149)
matrixTransform -= new Vector3(0, 2, 0);
if (scale.x >= 1.8f && matrixTransform.y > -194)
matrixTransform -= new Vector3(0, 2, 0);
if (scale.x >= 2.0f && matrixTransform.y > -239)
matrixTransform -= new Vector3(0, 2, 0);
}
}
//scale
if (Input.touches.Length == 2 && Input.GetTouch(1).phase == TouchPhase.Moved)
{
if ((Input.GetTouch(1).position.y > pos.y || Input.GetTouch(1).position.x < pos.x) && scale.x < 2.0f)
{
scale += new Vector2(0.1f, 0.1f);
}
if ((Input.GetTouch(1).position.y < pos.y || Input.GetTouch(1).position.x > pos.x) && scale.x > 1.0f)
{
scale = new Vector2(1.0f, 1.0f);
if (scale.x == 1.0f)
matrixTransform = new Vector3(1, 1, 1);
}
}
}
}
And for scaling the GUI Matrix Im doing:
GUI.matrix = Matrix4x4.TRS(matrixTransform, Quaternion.identity, new Vector3(Screen.width / cGlobalVariables.nativeHorizontalResolution, Screen.height / cGlobalVariables.nativeVerticalResolution, 1)); pivotPoint = new Vector2(Screen.width / 2, Screen.height / 2); GUIUtility.ScaleAroundPivot(scale, pivotPoint);
Now I zoom y with steps of 2. So I am able to limit the values for scrolling out of the image. But I want to zoom in with the touch deltaposition like on the x axes.
I hope my problem is clear enough to get help from you guys.
Thanks for any help.
Danny