Hello, I just have a question about making my GUI relative to any resolution. I have this code right now that it draws a small texture in the middle of the screen, a background and another small texture following the mouse (I also test the Contains function of the Rect structure):
// Screen factor calculated from the native resolution (768)
float factor = m_fNativeVerticalResolution / Screen.height;
// This line is supposed to fit the GUI correctly when changing resolutions....
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(1/factor, 1/factor, 1));
// We calculate the relative resolution
float swidth = Screen.width * factor;
float sheight = Screen.height * factor;
// We first draw the background
Rect background_rect = new Rect(0, 0, swidth, sheight);
GUI.DrawTexture(background_rect, m_Background);
// Then we draw a small rectangle on the center of the screen
float central_rect_w = 50 * factor;
float central_rect_h = 50 * factor;
float central_rect_x = swidth * 0.5f - (central_rect_w * 0.5f);
float central_rect_y = sheight * 0.5f - (central_rect_h * 0.5f);
Rect central_rect = new Rect(central_rect_x, central_rect_y, central_rect_w, central_rect_h);
GUI.DrawTexture(central_rect, m_BlackBox);
Vector2 mp = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
mp *= factor;
// we test the Contains function. The mouse position should be relative to the current resolution
if (background_rect.Contains(mp))
{
// We draw a rectangle following the mouse
float w = swidth * 0.1f;
float h = sheight* 0.1f;
GUI.DrawTexture(new Rect(mp.x - w * 0.5f, mp.y - h * 0.5f, w, h), m_BlackBox);
}
The thing is that this code works well in any resolution. But what I don’t get is why do I have to use the factor value to fit the sizes of all the Textures for different resolutions? Shouldn’t be enough using the GUI.matrix code line? The same happens with the mouse position: I have to convert them to the current resolution multiplying by the factor value. Can somebody kill my doubts?