Relative GUI

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?

I don’t know if I understood you correctly, but all I do is place this line in the start of the OnGUI function:

GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (Screen.height / nativeVerticalResolution, Screen.height / nativeVerticalResolution, 1));

Where nativeVerticalResolution is 768 here.
And it rescale everything as I change resolutions :smile:

Hote it helps

Hey thanks for the reply :slight_smile: I thought nobody was going to check this post due to its length :smile:

If you see my first lines in the code I use that piece of code you recomend me. The question is, if you check the next lines I have to use a factor value to resize my GUI textures anyway. and my question is? Does the following code line really work for all cases, coz it seems it does not:

GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (Screen.height / nativeVerticalResolution, Screen.height / nativeVerticalResolution, 1));

Look, after the matrix line, I just code all the gui absolute, like:

GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (Screen.height / nativeVerticalResolution, Screen.height / nativeVerticalResolution, 1)); 
	
	//__________Camera Left_______________
	
	GUI.BeginGroup(new Rect(25,559,306,159));
	
	GUI.Label(Rect(0, 0, 306,159), base);
	
	while(GUI.RepeatButton(Rect(90,8,44, 46), setaCima))
		SendMessage("SpinUp");
		
	while(GUI.RepeatButton(Rect(90, 101, 44, 46), setaBaixo))
		SendMessage("SpinDown");
	
	while(GUI.RepeatButton(Rect(43, 58, 46, 44), setaEsq))
		SendMessage("SpinLeft");
        //And so on...

with no extra calculations or anything…and it works in every resolution…[/code]

And have you tried something like this?

// 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);

If I try this piece of code without the factor value I get screen sizes problems with the texture :frowning:

You mean the texture appears incomplete, or does it get streched?

I have never tried doing it like that, I first tried with only the Matrix line, and never got any problems.

If I don’t use the factor value the texture appears small or really big depending on the resolution. so it seems like the GU.Matrix line code does not help :frowning:

would you mind sending me this texture just so I can test over here? I’ve never experienced this type of problem, nutmaybe is some configuration on the image…don’t know…

my mail is srto.dih@gmail.com