Ok so in one of my scripts i am drawing a guitexture which is sized width and height based on screen size. But I am also placing it on the screen 10 from the top and ten from the left.
As screen size changes that 10, 10 is not the same. Is there a way to calculate that “offset” based on the screen size?
void OnGUI(){
GUILayout.BeginArea(new Rect(10, 10, _newFrameWidth, _newFrameHeight), frame);
GUI.DrawTexture (new Rect (10, 10, _displayWidth, _newBarHeight), forground);
GUILayout.EndArea();
}
Everything is calculated else where and works perfect. It is only the offset I am having a issue with.
To be exact, the offset of 10 in x and 10 in y direction will still be the same, just in case you go fullscreen it will appear to be wider than before, but still 10 pixels.
Either you take the suggested solution or in order to make it dependant on the screen’s resolution and not from the drawn rectangle (maybe you wanna adjust its size later, which will also affect the offset) you could do the following:
Choose a resolution at which you would like to have an offset of x=10, y=10 (let’s say 1920x1080)
now calculate the offset relative to it : (10f/1920)*Screen.width
In case the offset of both (x and y) should be the same, use that calculation for both offsets as the X offset will not be the same as the Y offset if you calculated it with width for the x-offset and height for the y-offset.