Hi there guys, I will try to explain as well as I can.
I have 2 cameras. the one is the main background and the 2nd generates a square window, used to display a graph. on the 2nd graphCamera I have 2 scripts, one that set the normalized settings for the camera and one that generates numbered ticks for the graph’s x and y axis. here is the main part of the script for the camera viewport settings:
if (isSquare)
{
aspect = (float)Screen.width / (float)Screen.height;
height = squareHeight;
width = height / aspect;
normalizedRect = new Rect(leftEdge, bottomEdge, width, height);
camera.rect = normalizedRect;
}
else
{
normalizedRect = new Rect(leftEdge, bottomEdge, width, height);
camera.rect = normalizedRect;
}
…and here is the code for generating the ticks…
void Update()
{
xTicksIntervals = Screen.width * buildWindow.width / nrOfTicksX;
yTicksIntervals = Screen.height * buildWindow.height / nrOfTicksY;
//sets the x and y-position for the ticks on the x-axis
lowTickX = camera.ViewportToScreenPoint(new Vector2(0.025F, 0.475F));
//sets the x and y-position for the ticks on the y-axis
lowTickY = camera.ViewportToScreenPoint(new Vector2(0.475F, 0.015F));
}
void OnGUI()
{
for (int i = 0; i <= nrOfTicksX; i++)
{
GUI.Label(new Rect(lowTickX.x + i * xTicksIntervals, lowTickX.y, 15, 15), xTicks[i].ToString());
GUI.Label(new Rect(lowTickY.x, lowTickY.y + i * yTicksIntervals, 15, 15), yTicks[i].ToString());
}
}
This actually works in a way, but when I resize the window, the total width of all the ticks is slightly wider than the viewport. Is there a better or more reliable way of doing this. All I actually need is the width of the viewport in pixels devided by the number of ticks…
Thanks in advance