- Home /
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 :)
Please note: I am not asking for scripts
Answer by FakeBerenger · Nov 08, 2012 at 03:33 AM
Use fraction of the Screen's size for you're rect. Like : Rect(Screen.width .1f, Screen.height .1f, 100f, 100f)
Answer by JMC17 · Oct 07, 2013 at 10:05 AM
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!
Answer by Zac-Oberg · Mar 11, 2020 at 02:38 AM
Inevitably somebody is going to stumble upon this thread to make a button that sticks to the corner of the screen:
Create a new script
Copy and paste this entire snippet
Attach it to an empty gameobject
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-(leftright*5),(Screen.height*(updown/50)),width,height), "Button text!")) { ButtonPressed(); }; } void ButtonPressed() { Debug.Log("Button was pressed"); } }
Your answer
Follow this Question
Related Questions
Windowed game not centered 0 Answers
positioning a gameobject 10 persent of screen 3 Answers
Best way to move elements around screen [uGUI] 0 Answers
GUI Label Size and Position Changes With Resolution 2 Answers
Pause Menu Screen Resolution 0 Answers