Fixed position of GUI content (same for every resolution).

Hi guys, just wondering what the best way to do this is. I want a button on my screen located at a set point. I want the button to appear here despite the screen resolution. Can anyone tell me the best way to do this? Thank you in advance. So far I can print a texture to fit the screen size despite the resolution with:

	function OnGUI()
		{
			GUI.DrawTexture(Rect(0.0f, 0.0f, Screen.width, Screen.height), box1Texture);

		}

But now I need to know how to do it with objects such as buttons and display them at the same area every time. Thanks for any help :slight_smile:

Please note: I am not asking for scripts

Use fraction of the Screen’s size for you’re rect. Like : Rect(Screen.width * .1f, Screen.height * .1f, 100f, 100f)

I understand this is an old thread, hopefully it can still help someone.

If using GUI.Button, this is the way:

void OnGUI(){
GUI.backgroundColor = new Color(0, 0, 0, 0);
if (GUI.Button (new Rect(Screen.width-80,(Screen.height*0)+10,64,32),ButtonTexture)){ButtonPressed();};
}
    
void ButtonPressed(){
Debug.Log("Button was pressed");
}

If not, then you have to create an empty gameObject and fill it with a guiTexture.
Set the transform scale to 0,0,1 ; Set the pixelinset x,y to 0 and width height to those of your image then use transform position as a percentage of the screen, by setting the transform position between 0 and 1 (0,0 bottom left and 1,1 top right)
Then create a Rect based on your image’s size and position and check for touches within it using Touch and Input.

For the GUI to be fixed in size and position it is better to use a pixel offset than a screen percentage.
For screen percentage you would do:
Screen.width*0.9f (and get your image positioned at 90% of the screen width)

But for pixel offset you would have to do:

float sw=Screen.width //(Converting int to float)
float rsw=sw-30 //(30 pixels away from the right side(removing 30 pixels from Screen.width))
float rrsw=rsw/sw //(Dividing the position to get the final percentage of the screen(Converting the modified position to be a value between 0 and 1))

transform.position=new Vector3(rrsw,Screen.height*0.5f,1);

You could also do it for the height too, in the example above I just left it with a screen percentage.

I would leave the complete code but I have none on hand, sorry!

Inevitably somebody is going to stumble upon this thread to make a button that sticks to the corner of the screen:

  1. Create a new script

  2. Copy and paste this entire snippet

  3. Attach it to an empty gameobject

  4. Profit!

    [ExecuteInEditMode]
    

    public class fixedButton : MonoBehaviour
    {
    public float width=100;
    public float height=40;
    public float updown=3;
    public float leftright=25;
    void OnGUI()
    {
    if (GUI.Button (new Rect(Screen.width-(leftright5),(Screen.height(updown/50)),width,height), “Button text!”))
    {
    ButtonPressed();
    };
    }

    void ButtonPressed()
    {
        Debug.Log("Button was pressed");
    }
    

    }